Windows server Task scheduler start powershell that starts powerBI

Old situation: I have a .bat file that copies some files and then starts a powershell with a given script. This script will boot up PowerBI and will do some query and exporting then from PowerBI.
This works fine when fired up by taskscheduller when a user is logged on and only locked their PC.

Current situation: I’m moving to a Windows server instead of a separate computer with Windows desktop. I copied the task to the server and changed it to “run whether user is logged on or not”.

I noticed if I force “run” on the task scheduler, it runs is some hidden mode (although I did not check the Hidden checkbox) so I don’t see PowerBI get started. I assume it doesn’t, as my powershell script fails further along the line where it needs to find its data in powershell.

log_message "***********************************************"
log_message "Executing PBI_Exporter.ps1 at $($executionDate)"
log_message "Launching Power BI"

$template = "C:\LiveData\Eisen.pbix"
$PBIDesktop = "C:\Program Files\Microsoft Power BI Desktop\bin\PBIDesktop.exe"    


$app = START-PROCESS $PBIDesktop $template -PassThru
log_message "Waiting $($waitoPBD) seconds for PBI to launch"
Start-Sleep -s $waitoPBD

log_message "Assuming PBI is launched and ready now"

$pathtofile = (Get-ChildItem -Path c:\users -Filter msmdsrv.port.txt -Recurse -ErrorAction SilentlyContinue -Force | sort LastWriteTime | select -last 1).FullName
$port = gc $pathtofile
$port = $port -replace '\D',''
$dataSource = "localhost:$port"
$pathtoDataBase_Name = $pathtofile -replace 'msmdsrv.port.txt',''
$Database_Name = Get-ChildItem -Path $pathtoDataBase_Name -Filter *.db.xml -Recurse -ErrorAction SilentlyContinue -Force
$Database_Name = $Database_Name.ToString().Split(".") | select -First 1

I can confirm the powershell script at least gets executed, as I can see the log file getting created, and it runs till the message where it assumes PowerBI is launched. Right after that I get an error Cannot bind argument to parameter ‘Path’ because it is null. so I assume that it’s null because powerBI isn’t executed.

Is there some way to confirm this (maybe some try/catch blocks?) and/or solve this?

Answer

This …

$app = START-PROCESS $PBIDesktop $template -PassThru

Will never run, because you are not calling it in your code anywhere.

Why are you sending a standard PowerShell one-Liner to a variable?

That line is not normal and should be just this…

START-PROCESS $PBIDesktop $template -PassThru

Lastly, when running external exe’s from PowerShell, there are things that must be considered and in place, for them to work. See…

PowerShell: Running Executables

Solve Problems with External Command Lines in PowerShell

Top 5 tips for running external commands in Powershell

Execution of External Commands in PowerShell Done Right

Using Windows PowerShell to run old command line tools (and their
weirdest parameters)

See also about Redirection
powershell-pipe-external-command-output-to-another-external-command

Attribution
Source : Link , Question Author : Dante1986 , Answer Author : postanote

Leave a Comment