Capture system restarts or not when executing PowerShell remotely

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Rob Simmers Rob Simmers 3 weeks, 3 days ago.

  • Author
    Posts
  • #65556
    Profile photo of powershelluser143
    powershelluser143
    Participant

    Hi all I am having the following script which updates the software remotely

    $path= 'c:\mysw-x64.msu' #c:\_temp\W2K12-KB3134759-x64.msu
    
    Invoke-Command -Session $psSession -ArgumentList $path-ScriptBlock { param($path) 
         $SB1={
                 Start-Process -FilePath 'wusa' -ArgumentList "$path /extract:c:\updates" -wait -PassThru
    
        $SB2={
                Start-Process -FilePath 'dism' -ArgumentList "/online /add-package /PackagePath:c:\updates /IgnoreCheck /Quiet" -PassThru -wait
             }
          Invoke-Command -ScriptBlock $SB1
          Invoke-Command -ScriptBlock $SB2
       }

    This executes fine and system is getting restarted but I need to check whether it actually restarts or any issues while updating is it possible to do so or if it successfully restarts I need a return code

  • #65586
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    You don't want the reboot to be initiated by your installation command. For instance, for wusa you would specify a /norestart, let the command finish, get the exit code (e.g.3010-reboot required) and then use Restart-Computer to initiate a system reboot. Additionally, you may need to call the command with Start-Process -Wait to ensure that the command completes or it will likely run the command and immediately reboot the system. If you do some searches on running MSI with Powershell, you should find plenty of examples

    • #65686
      Profile photo of powershelluser143
      powershelluser143
      Participant

      Hi Rob I added /norestart but still I am not getting the exit code

  • #65734
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    You can find answers to your questions with simple searches like "Start-Process exit code".:

    $process = Start-Process -FilePath 'wusa' -ArgumentList "$path /extract:c:\updates" -wait -PassThru
    $process.ExitCode()
    

You must be logged in to reply to this topic.