Walhalla
Walhalla

Reputation: 396

Powershell Start-Process with Splatting

I want to call a "PS App Deployment Toolkit"-package (Link) from a PowerShell-Script with arguments.

The mentioned "PS App Deployment Toolkit"-package is a powershell-script, which I want to call with parameters. (Call a .ps1 from a .ps1)

I want to use splatting for the parameters.
I want to wait for the script to end.
I want to get the exit-code from the script.

Here is my code, which is not working:

$PSADToolKitInstallWrapper = "C:\Temp\MyPackage\PS-AppDeploy.ps1"

$PSADToolKitParameters = @{
    "DeploymentType" = "Uninstall";
    "DeployMode" = "Interactive";
    "AllowRebootPassThru" = $True;
    "TerminalServerMode" = $False;
    "DisableLogging" = $False;
}        

$InstallStatus = Start-Process -FilePath "PowerShell.exe" -ArgumentList $PSADToolKitInstallWrapper @PSADToolKitParameters -Wait -PassThru

Write-Host "Exit-Code: $( $InstallStatus.ExitCode )"

This Line would work fine, but I want to set the Parameters like in the example above:

$InstallStatus = Start-Process -FilePath "PowerShell.exe" -ArgumentList "$PSADToolKitInstallWrapper","-DeploymentType Install -DeployMode Silent -AllowRebootPassThru -TerminalServerMode" -Wait -PassThru

Could you please assist me to get this working?

Thank you!

Upvotes: 1

Views: 1315

Answers (1)

Bill_Stewart
Bill_Stewart

Reputation: 24515

I don't think you need to try so hard. Why run powershell.exe from inside a PowerShell script? You're already running PowerShell. Just run the command line you want:

$PSADToolKitParameters = @{
  "DeploymentType" = "Uninstall"
  "DeployMode" = "Interactive"
  "AllowRebootPassThru" = $True
  "TerminalServerMode" = $False
  "DisableLogging" = $False
}
C:\Temp\MyPackage\PS-AppDeploy.ps1 @PSADToolKitParameters

If the path and/or filename to the script you want to run contains spaces, then call it with the invocation operator (&) and quote the filename; example:

& "C:\Temp\My Package\PS-AppDeploy.ps1" @PSADToolKitParameters

Checking the results of the script depends on what the script returns. If it returns an output object, then you can simply assign it:

$output = C:\Temp\MyPackage\PS-AppDeploy.ps1 ...

If the script runs an executable that sets an exit code, you check the value of the $LASTEXITCODE variable (this is analogous to the %ERRORLEVEL% dynamic variable in cmd.exe).

Upvotes: 2

Related Questions