Search code examples
powershellpidwindows-update

How to get process id of process that is triggered by a powershell script


I have a script that has been executed using powershell ISE. This Invoke-WUInstall seems to trigger powershell.exe. How can I capture the process ID for the powershell.exe.

$WUInstallScript = { Get-WUInstall -AcceptAll |Out-File C:\SUPPORT\text.log}
Invoke-WUInstall -ComputerName computername -Script $WUInstallScript -Confirm:$false

Solution

  • You can write it to the log file, and pick it up from there:

    $WUInstallScript = { "Process ID is $PID" | Out-File C:\SUPPORT\text.log
                         Get-WUInstall -AcceptAll | Out-File C:\SUPPORT\text.log -Append}
    Invoke-WUInstall -ComputerName computername -Script $WUInstallScript -Confirm:$false