locked
Powershell Script to start Windows Update and wait for it to be Running on Remote Computer RRS feed

  • Question

  • I heve a powershell script that will Stop and disable Windows updates and deletes the client GUID that it uses to check in with WSUS.  It then re-creates the reg entries (I know that GPO is preferred for this), but in this OU for various reason, we do not have GPOs.  For the most part the script woks fine. However, the part I have pasted seems to lag for whatever reason.  The gist is it simply sets the Update Service to Manual Startup and then start the service.  It then waits for the service to be running before proceeding.  The wait is where the problem occurs.  I can open up another powershell session.  I can poll the session.  I can keep refreshing and see the service stop (from other portion of code) and become disabled.  I can see the service get renabled.  At this time of the Start-Service command, I even see it start and go RUNNING.  However, I dont think the code sees it, therefore is keeps waiting.  It will eventually see it running and progress.  But it could be minutes, when in fact it is about 15 seconds or less.  I am wandering if it is not getting refreshed in the code.   Note that this code is a small part of a foreach routine, so there is no closing bracket at the end.  Any help on what is wrong or a better way to check for this service running, will be appreciated.

     ###################################################
        ##                                                 ##
        ##  Start and Enable Update Service                ##
        ##                                                 ##
         ###################################################
     
        $ScriptBlock = {
         $ServiceName = "wuauserv"
         $Serv = Get-Service -Name $ServiceName
         Write-Host 'Setting $ServiceName Startup to Manual'
         Set-Service $ServiceName -StartupType Manual
         Start-Service $ServiceName 
         
         if ($Serv.Status -ne 'Running'){
            $ServStarted = $false}
            else {$ServStarted = $true}
                while ($ServStarted -eq $false){
                    Start-Service $ServiceName
                    Write-host $ServStarted
                    write-host $Serv.Status
                    Write-host 'Starting Update Service'
                    Start-Sleep -seconds 3
                    $Serv = Get-Service -Name $ServiceName
                      if ($Serv.Status -eq 'Running'){
                       $ServStarted = $true}
                         }
        
            }
                Invoke-Command -ComputerName $CompName -ScriptBlock $Scriptblock

     
    Tuesday, February 11, 2020 5:05 PM

All replies

  • There is no need for all of that code:

    $ScriptBlock = { $serviceName = 'wuauserv' Set-Service $ServiceName -StartupType Manual    Start-Service $ServiceName do{
    Sleep 3
    }until((Get-Service -Name $ServiceName).Status -eq 'Running')

    } Invoke-Command -ComputerName $CompName -ScriptBlock $Scriptblock



    \_(ツ)_/


    • Edited by jrv Tuesday, February 11, 2020 5:21 PM
    Tuesday, February 11, 2020 5:18 PM
  • Now this is the real PowerShell way to do this:

    $service = Get-Service wuauserv -Computername $compname
    $service | Set-Service -StartupType Manual
    $service | Start-Service
    do{
        Start-Sleep -seconds 3
        $service.Refresh()
    }
    until($service.Status -eq 'Running')
    

    There is no need to use Invoke with this command.  It is self-contained and does its own remoting.


    \_(ツ)_/


    • Edited by jrv Tuesday, February 11, 2020 5:27 PM
    Tuesday, February 11, 2020 5:26 PM
  • And this is also available with this clase:

    $service = Get-Service wuauserv -Computername $compname
    $service | Set-Service -StartupType Manual
    $service | Start-Service
    $service.WaitForStatus('Running')


    \_(ツ)_/

    Tuesday, February 11, 2020 5:33 PM