TabularEditor icon indicating copy to clipboard operation
TabularEditor copied to clipboard

Execute C# script from comand line using powershell

Open rvgfox opened this issue 1 year ago • 15 comments

Hi @otykier I've a similar Issue with TE2. I've a C# script that works ok using the GUI but I'm not be able to execute it using this power shell script:

# Definir los valores de los parámetros
$ServerName = "My Azure AS"
$DatabaseName = "my DB"
$UserName = "my user"
$PasswordFile = "C:\SYSTEM\UCCE\MyPassword.xml"
$ScriptType ="Csharp"
$ScriptPath = "C:\SYSTEM\UCCE\SetCustomdataRLS.csx"
$TabularEditorPath = "C:\Program Files (x86)\Tabular Editor"
$LogFilePath = "C:\SYSTEM\UCCE\TabularEditorLog.txt"


# Obtener la contraseña del archivo cifrado
$SecurePassword = Get-Content $PasswordFile | ConvertTo-SecureString

$Password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword))

# Crear una cadena de conexión para conectarse a Azure Analysis Services
$ConnectionString = "Provider=MSOLAP;Data Source=$ServerName;Initial Catalog=$DatabaseName;User ID=$UserName;Password=$Password;"


& "$TabularEditorPath\TabularEditor.exe" $ConnectionString -S $ScriptPath

Can you help me? Thanks in advance

Originally posted by @rvgfox in https://github.com/TabularEditor/TabularEditor3/issues/123#issuecomment-1513479978

rvgfox avatar Apr 18 '23 16:04 rvgfox

Moving this issue to the TE2 repo

otykier avatar Apr 19 '23 08:04 otykier

Hi @otykier Some news?

rvgfox avatar Apr 20 '23 07:04 rvgfox

What error message are you seeing?

otykier avatar Apr 20 '23 07:04 otykier

@otykier I do not receive any error message, the only thing is that the csx script that I pass as a parameter is not executed. If I use the line:

& "$TabularEditorPath\TabularEditor.exe" $ConnectionString

Without the script, tabular editor opens connected to the SSAS model...

rvgfox avatar Apr 20 '23 08:04 rvgfox

You’re not instructing the CLI to save the model after script execution. You would have to add the -B switch for that. If that is intentional, how do you know the script has not been exrcuted?

otykier avatar Apr 20 '23 08:04 otykier

Sorry, -D switch is to save the changes back to Analysis Services/Power BI. -B is for saving as a .bim

otykier avatar Apr 20 '23 08:04 otykier

@otykier I'm sure because if I open and run the script using the GUI I can see that it works, but if I use the command line nothing happens. What will be exactly the command line instruction?

rvgfox avatar Apr 20 '23 09:04 rvgfox

What is the purpose of the script? If your script is modifying the model, for example adding new measures, then the command you’re using will appear not to do anything, since TE is just loading the model and running the script, and then exiting.

If you want TE to save the model after the script has executed, you should add -D to the end of the command.

otykier avatar Apr 20 '23 09:04 otykier

The script it's "simple". It's adds an additional condition to the filterexpression in the existing roles. It runs ok directly in the GUI but I need to run it using the commad line from my powershell script.

rvgfox avatar Apr 20 '23 09:04 rvgfox

@otykier I've tried with:

& "$TabularEditorPath\TabularEditor.exe" $ConnectionString -S $ScriptPath -D

And it doesn't works

rvgfox avatar Apr 20 '23 09:04 rvgfox

What does the command line output? If there was an error, you should see it in the output from the command. If you don’t see any output, you may need to change the way you’re calling TabularEditor.exe (sorry, I’m not a PowerShell expert - I usually recommend running the TE2 CLI from a regular window command line).

Maybe this helps: https://stackoverflow.com/a/1742758

otykier avatar Apr 20 '23 10:04 otykier

Another way to try to debug the issue, is if you add a line to your script, Info("hello"); which you should see as output when the script is being executed from CLI.

otykier avatar Apr 20 '23 10:04 otykier

@otykier Can you give me an example of the command line using TE2 CLI?

rvgfox avatar Apr 20 '23 10:04 rvgfox

Ah shoot - only now did I notice that you only specify 1 parameter before the -S switch. You need to specify both the connection string and the database name as two separate parameters (when only the connection string parameter is specified, the command will only work if the endpoint contains exactly 1 database)

In windows CLI, it would look like this:

start /wait tabulareditor.exe "Provider=MSOLAP;Data Source=…" "AdventureWorks" -S "c:\path\to\scriptfile.csx" -D

otykier avatar Apr 20 '23 12:04 otykier

Hi @otykier I've detected that the problem it's in the csx script, but I don't understand why.

My workaround it's:

  1. Open the model with TE3
  2. Apply the changes with the script csx
  3. Save as bim file
  4. Apply the changes to the model using the command line with TE2

rvgfox avatar Apr 24 '23 07:04 rvgfox