How to return PID of nested start-process command

Welcome Forums General PowerShell Q&A How to return PID of nested start-process command

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
1 week, 5 days ago.

  • Author
    Posts
  • #181632

    Participant
    Topics: 1
    Replies: 0
    Points: -9
    Rank: Member

    Hi All,

    Hopefully someone can help.

    I have the below which seems to work fine (in order to run an installer both using credentials and elevated):

    $credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $credu, $credp
    
    $subproc = "Start-Process -FilePath msiexec.exe -ArgumentList '/i ', '\\networkloc\INSTALLER.msi', 'SERIALNUMBER=BLA DEFAULTSINI=\\networkloc\nsdefaults.ini ROAMINGUSERINI=\\networkloc\roamingdef.ini PRODUCTUPDATEFLAG=1 REBOOT=ReallySuppress REGION=UK'"
    $args = "-noprofile -command &{" + $subproc + " -verb runas -passthru}"
    
    $ProcID1 = Start-Process powershell -Credential ($credentials) -ArgumentList $args
    
    Write-Host("ProcID1: $ProcID1")
    Write-Host("ProcID2: $ProcID2")
    
    do {start-sleep -Milliseconds 500}
    until ($ProcID1.HasExited)
    
    do {start-sleep -Milliseconds 500}
    until ($ProcID2.HasExited)
    
    DO OTHER STUFF

    The difficulty I have is that I need to get the PID of the nested running application in order to be able to wait for it to exit, so in this example the specific \\networkloc\INSTALLER.msi PID

    Please note that above script is a saved as a function so will be called multiple times as part of a bigger script and I just want it to wait until each one is done in turn.

  • #181677

    Participant
    Topics: 6
    Replies: 97
    Points: 239
    Helping Hand
    Rank: Participant

    Look into using PoshRSJob. I use it in a script to pull and log resource usage on each node in a VMware cluster. For each cluster node (host), I kick off a separate RSJob. The module has functionality built-in to wait and report on each job and wait for all of them to complete before continuing the script.

    # Pull the list of hosts from VMware and execute loop for each host
    Get-Cluster "$ClusterName" | 
    Get-VMHost |
    Start-RSJob -Name { "DataGrab_$($_.Name)" } -ScriptBlock $scriptBlock -ModulesToImport VMware.VimAutomation.Core |
    Wait-RSJob
    
    # Gather data returned for each host into one array
    $eachHost = Get-RSJob | Receive-RSJob

You must be logged in to reply to this topic.