Powershell start-process -wait parameter fails in remote script block - powershell

So here's a sample of what I'm trying to do:
Invoke-Command [Connection Info] -ScriptBlock {
param (
[various parameters]
)
Start-Process [some .exe] -Wait
} -ArgumentList [various parameters]
It connects to the other machine just fine, and launches the process fine. The problem is it doesn't wait for the process to complete before moving on. This causes issues. Any ideas?
Quick edit: why does the -Wait parameter fail when running the process remotely?

I ran into this once before, and IIRC, the workaround was:
Invoke-Command [Connection Info] -ScriptBlock {
param (
[various parameters]
)
$process = Start-Process [some .exe] -Wait -Passthru
do {Start-Sleep -Seconds 1 }
until ($Process.HasExited)
} -ArgumentList [various parameters]

This is the problem with Powershell version 3, but not version 2 where -Wait works as it should.
In Powershell 3 .WaitForExit() does the trick for me:
$p = Start-Process [some .exe] -Wait -Passthru
$p.WaitForExit()
if ($p.ExitCode -ne 0) {
throw "failed"
}
Just Start-Sleep until .HasExited - doesn't set .ExitCode, and it's usually good to know how your .exe finished.

You can also work around this by using the System.Diagnostics.Process class. If you do not care about the output you can just use:
Invoke-Command [Connection Info] -ScriptBlock {
$psi = new-object System.Diagnostics.ProcessStartInfo
$psi.FileName = "powershell.exe"
$psi.Arguments = "dir c:\windows\fonts"
$proc = [System.Diagnostics.Process]::Start($psi)
$proc.WaitForExit()
}
If you do care you can do something similar to the following:
Invoke-Command [Connection Info] -ScriptBlock {
$psi = new-object System.Diagnostics.ProcessStartInfo
$psi.FileName = "powershell.exe"
$psi.Arguments = "dir c:\windows\fonts"
$psi.UseShellExecute = $false
$psi.RedirectStandardOutput = $true
$proc = [System.Diagnostics.Process]::Start($psi)
$proc.StandardOutput.ReadToEnd()
}
This will wait for the process to complete and then return the standard output stream.

Related

Wait-Process does not always work with Start-Process

$GAMCheck = invoke-command -ScriptBlock { C:\GAMADV-XTD3\GAM.exe version checkrc }
If ($GAMCheck) {
$current = $GAMCheck.split(":")[19].Trim()
$latest = $GAMCheck.split(":")[21].Trim()
If ($LASTEXITCODE -eq 1) {
Try {
$NeedUpGradeCode = $LASTEXITCODE
$client = new-object System.Net.WebClient
$client.DownloadFile("https://github.com/taers232c/GAMADV-XTD3/releases/download/v$latest/GAMadv-xtd3-$latest-windows-x86_64.msi", "C:\Temp\GAMadv-xtd3-$latest-windows-x86_64.msi")
Start-Process -Filepath "C:\Temp\GAMadv-xtd3-$latest-windows-x86_64.msi" -ArgumentList "/passive" | Wait-Process -Timeout 75
Remove-Item "C:\Temp\GAMadv-xtd3-$latest-windows-x86_64.msi"
$GAMCheck = $null
$GAMCheck = invoke-command -ScriptBlock { C:\GAMADV-XTD3\GAM.exe version checkrc }
$newCurrent = $GAMCheck.split(":")[19].Trim()
$resultsarray = [PSCustomObject]#{
CurrentVersion = $current
LatestVersion = $latest
NeedUpgradeCode = $NeedUpGradeCode
Upgraded = $true
NewCurrent = $newCurrent
AfterUpgradeCode = $LASTEXITCODE
}
}
Catch {
Write-Warning "Problem with site or command. Maybe go to https://github.com/taers232c/GAMADV-XTD3/releases and download the current GAM and then install GAM in C:\GAMADV-XTD3\ again"
}
}
}
lately I have been noticing that the | Wait-process 75 above is causing an error.
If I run the command with out it everything is fine.
Is there another way to wait for the install ?
To launch a process with Start-Process and wait for it to exit, use the -Wait switch.
Piping a Start-Process call to Wait-Process would only work as intended if you included the -PassThru switch, which makes Start-Process - which by default produces no output - emit a System.Diagnostics.Process instance representing the newly launched process, on whose termination Wait-Process then waits.
Note that, surprisingly, the behavior of these two seemingly equivalent approaches is not the same, as discussed in GitHub issue #15555.

Start-Command PowerShell v3.0, does nothing

I am trying to install software (executable) on several servers with various versions of PowerShell.
Normally below code works with no issue on PS4 and up. On PS3 it does not install anything, nor does it produce any errors on the remove server eventviewer. It treated as success by printing out "... -- installation succeeded" and exits. I've googled about and read that perhaps Start-Process is the culprit in PS3.
Begin {
$uncpath="\\remoteserveruncpath\" #"
$exe_parameter1 = "centralserver.com"
$creds = Get-Credential -Message "Password: " -Username "$($env:userdnsdomain)\$($env:username)"
}
Process {
$dnshostname = "server1","server2","server3"
ForEach ($server in $dnshostname) {
Invoke-Command -ComputerName $server -ScriptBlock {
param($server_int,$exe_parameter1_int,$uncpath_int,$creds_int)
(New-Object -ComObject WScript.Network).MapNetworkDrive('Z:',"$($uncpath_int)", $false, "$($creds_int.Username)", "$($creds_int.GetNetworkCredential().Password)")
$arguments = "/param_1=$exe_parameter1_int /param_2=$($server_int.ToLower()) /start-program=1 /S"
If((Start-Process "Z:\installer.exe" -ArgumentList $arguments -Wait -Verb RunAs).ExitCode -ne 0) {
Write-Host "$server_int -- installation succeeded"
} else {
Write-Error "$server_int -- installation failed"
}
} -ArgumentList $server,$exe_parameter1,$uncpath,$creds;
}
}
Any advice? Many thanks!
Without -PassThru, Start-Process produces no output, so accessing .ExitCode effectively returns $null, always.
And since $null -ne 0 is always $true, your code always indicates success.
In order to get the installer command's true exit code, you therefore need to use the following (note the addition of -PassThru):
if ((Start-Process -PassThru 'Z:\installer.exe' -ArgumentList $arguments -Wait -Verb RunAs).ExitCode -ne 0) { ... }

Start-Process and powershell.exe with splatting

I've been trying for a couple of days now to multi-thread a WPF GUI which will run a PS3.0 script once the button has been clicked. I cannot use start-job as that I would have to track (multiple sessions at once), however, I would like to just run the script in a separate process of PS- as if I were to open multiple instances of the script from a shortcut. And be able to just have an open PS window which will track the progress within the script itself.
Expected results would be starting a script in powershell.exe session and passing 3 arguments - 2 strings and 1 boolean value. Which are provided by the user.
So in ISE:
C:\temp\test.ps1 -argumentlist $computername $username $citrixtest
Works fine.
I've spent a few hours scouring through the internet only to find a thread where a start-job was recommended or a way to use a background worker- this is not what I want from the script.
So I would guess the invocation from a button click would be something of the like (some of the things I have tried)
$ComputerName = "testtext1"
$UserName = "testtext2"
$CitrixTest = $True
$command = "c:\temp\test.ps1"
$arg = #{
Computername = "$computername";
Username = "$username";
CitrixTest = "$citrixtest"
}
#$WPFStartButton.Add_Click({
Start-Process powershell -ArgumentList "-noexit -command & {$command} -argumentlist $arg"
#})
Does not pass arguments to test.ps1- it is, however, getting to the "pause" - so the script successfully launches.
Where test.ps1 is
$ComputerName
$UserName
$CitrixTest
pause
Caller:
function Caller {
Param (
$ScriptPath = "c:\temp\test.ps1"
)
$Arguments = #()
$Arguments += "-computername $ComputerName"
$Arguments += "-UserName $UserName"
$Arguments += "-citrixtest $citrixtest"
$StartParams = #{
ArgumentList = "-File ""$ScriptPath""" + $Arguments
}
Start-Process powershell #StartParams
}
Caller
Does not start the script altogether- PS window just closes- possibly a path to .ps1 script not being found.
And a different approach which also nets in the script starts but not passing the arguments
$scriptFile = '"C:\temp\test.ps1"'
[string[]]$argumentList = "-file"
$argumentList += $scriptFile
$argumentlist += $computername
$argumentlist += $UserName
$argumentlist += $CitrixTest
$start_Process_info = New-Object System.Diagnostics.ProcessStartInfo
$start_Process_info.FileName = "$PSHOME\PowerShell.exe"
$start_Process_info.Arguments = $argumentList
$newProcess = New-Object System.Diagnostics.Process
$newProcess.StartInfo = $start_Process_info
$newProcess.Start() | Out-Null
Is there a way to make this work as I want it to? Or should I just dig deeper into runspaces and try with that?
#Bill_Stewart I just realized I did not put the param(args) in my script...
And that's why it would not pull those variables as I would like them to. I will have to check when I'm back in the office if it's just that what I was missing.
Checked on my laptop that's running PS 5.1 and this seems to be working as intended
$testarg = #(
'-File'
"C:\temp\test.ps1"
"$computername"
"$username"
"$citrixtest"
)
Start-Process powershell.exe -ArgumentList $testarg
Where test.ps1 is:
param(
$ComputerName,
$UserName,
$citrixtest
)
$ComputerName
$UserName
$CitrixTest
pause

How do I pass multiple paramaters to PowerShell Start-Job to run a process in the background?

I know there are many examples out there that are similar to mine. I have tried many with no real success. I simplified my code to the basic issue in the hopes that someone could point me in the right direction.
function Send-DBBackupToS3
{
param(
[Parameter(Mandatory=$true)][string]$p1,
[Parameter(Mandatory=$true)][string]$p2,
[Parameter(Mandatory=$true)][string]$p3
)
try
{
Write-Host "starting process..."
$TransferAppExe = $p1
$arguments = '-OnDiskPath', $p2, '-NotificationEmailAddress', $p3
$ps = Start-Process -FilePath $TransferAppExe -ArgumentList $arguments -Wait -PassThru
}
catch
{
# get error record
[Exception]$e = $_
# retrieve information about runtime error
$info = [PSCustomObject]#{
Exception = $e.Exception.Message
Reason = $e.CategoryInfo.Reason
Target = $e.CategoryInfo.TargetName
Script = $e.InvocationInfo.ScriptName
Line = $e.InvocationInfo.ScriptLineNumber
Column = $e.InvocationInfo.OffsetInLine
}
# output information. Post-process collected info, and log info (optional)
$info
}
}
function Start-DBCopyAndTransfer
{
param(
[Parameter(Mandatory)]
[string]$AppPath,
[Parameter(Mandatory)]
[string]$UploadFilePath,
[Parameter(Mandatory)]
[string[]]$EmailAddress
)
Write-Host "calling job..."
Start-Job -Name Send2S3 -ScriptBlock {param($p1, $p2, $p3) Send-DBBackupToS3 -p1 $p1 -p2 $p2 -p3 $p3} -ArgumentList $AppPath,$UploadFilePath,$EmailAddress
}
Clear-Host
Write-Host "calling function..."
Start-DBCopyAndTransfer -AppPath "C:\FileToS3.exe" -UploadFilePath "C:\chron.cti" -EmailAddress "4321#gmail.com"
I am trying to pass into a Start-Process cmdlet the parameters needed to run the .exe.
The results I are as follows:
calling function...
calling job...
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
1 Send2S3 BackgroundJob Running True localhost param($p1, $p2, $p3) S...
PS C:\WINDOWS\system32> Get-Job
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
1 Send2S3 BackgroundJob Failed False localhost param($p1, $p2, $p3) S...
PS C:\WINDOWS\system32>
I never see the Write-Host "starting process..." fire. If I take away the params(Hard code the values) from Send-DBBackupToS3 it works just fine. Thanks for your time!
I hate to answer my own questions...However, if someone runs across this I want them to have the solution.
$func = {
function Send-DBBackupToS3
{
param(
[Parameter(Mandatory=$true)][string]$p1,
[Parameter(Mandatory=$true)][string]$p2,
[Parameter(Mandatory=$true)][string]$p3
)
try
{
Write-Host "starting process..."
$TransferAppExe = $p1
$arguments = '-OnDiskPath', $p2, '-NotificationEmailAddress', $p3
Start-Process -FilePath $TransferAppExe -ArgumentList $arguments -Wait -PassThru
}
catch
{
# get error record
[Exception]$e = $_
# retrieve information about runtime error
$info = [PSCustomObject]#{
Exception = $e.Exception.Message
Reason = $e.CategoryInfo.Reason
Target = $e.CategoryInfo.TargetName
Script = $e.InvocationInfo.ScriptName
Line = $e.InvocationInfo.ScriptLineNumber
Column = $e.InvocationInfo.OffsetInLine
}
# output information. Post-process collected info, and log info (optional)
$info
}
}
}
function Start-DBCopyAndTransfer
{
param(
[Parameter(Mandatory)]
[string]$AppPath,
[Parameter(Mandatory)]
[string]$UploadFilePath,
[Parameter(Mandatory)]
[string[]]$EmailAddress
)
Write-Host "calling job..."
$job = Start-Job -Name Send2S3 -InitializationScript $func -ScriptBlock {param($p1, $p2, $p3) Send-DBBackupToS3 -p1 $p1 -p2 $p2 -p3 $p3} -ArgumentList $AppPath,$UploadFilePath,$EmailAddress
Receive-Job -Job $job
Write-Host ('State: {0}' -f $job.State)
}
Clear-Host
Write-Host "calling function..."
Start-DBCopyAndTransfer -AppPath "C:\FileToS3.exe" -UploadFilePath "C:\chron.cti" -EmailAddress "4321#gmail.com"
You can pass multiple arguments as follows:
Start-Process -FilePath $TransferAppExe -ArgumentList $argument1, $argument2, $argument3 -Wait -PassThru
Or pass a list of arguments:
$arguments = $argument1, $argument2, $argument3
Start-Process -FilePath $TransferAppExe -ArgumentList $arguments -Wait -PassThru
Or pass a single string with all arguments:
Start-Process -FilePath $TransferAppExe -ArgumentList "$argument1 $argument2 $argument3" -Wait -PassThru
Also, you don't need to WaitForExit if you have the -Wait argument, as they do the same thing.

How to get output from powershell process

Here's the code
function RunPowershellAsAdmin($CommandToBeExecuted)
{
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
#$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList "$CommandToBeExecuted" -Verbose
}
}
RunPowershellAsAdmin("& { Import-Module WebAdministration; if(Test-Path 'IIS:\Sites\$Website_Name') { Remove-WebSite -Name '$Website_Name'; } }")
-Verb and -RedirectStandardOutput are not in the same parameter, so i can not use -RedirectStandardOutput to get process output as per answer from this link.
I want to run the process in a hidden window, wait for it to return and get the error, output and exit code.
Is there any other solution?
Thanks in advance.
If you need access to both RunAs in addition to redirecting any of the standard streams, you'll need to use the System.Diagnostics.Process and System.Diagnostics.ProcessStartInfo classes directly. More information on how to handle the redirected stream can be found on MSDN.
$startInfo = new-object System.Diagnostics.ProcessStartInfo
$startInfo.FileName = "powershell"
$startInfo.Arguments = "& { Import-Module WebAdministration; ... }"
$startInfo.Verb = "runas"
$startInfo.RedirectStandardOutput = $true
$process = [System.Diagnostics.Process]::Start($startInfo)
$output = $process.StandardOutput.ReadToEnd()
$process.WaitForExit()