disable and enable services

Welcome Forums General PowerShell Q&A disable and enable services

This topic contains 7 replies, has 3 voices, and was last updated by

 
Participant
9 months, 4 weeks ago.

  • Author
    Posts
  • #91763

    Participant
    Points: 1
    Rank: Member

    Hi. im trying to find a solution for our servers (windows 2008R2) when they are going to be pached we have to login to ech server and enable windows update services and start it. I think that this can be done through a invoke-comand script...

    Have tried with following scripts Next issue is – After Pathed is done is should disable and stop the services

    Get-content "E:\temp\srv.txt" | foreach {set-Service -ComputerName $_ -Name wuauserv -status Stopped | -StartupType Disabled -PassThru}
    
    =============================================================================================
    ## Script 2  After Pathed is done is should disable and stop the services##
    $Serverlist = Get-Content E:\Temp\srv.txt
    $service = "wuauserv"
    Invoke-Command -ComputerName $Serverlist {Get-Service -Name $service | Stop-Service -Force}
    Invoke-Command -ComputerName $Serverlist {Get-Service -Name $service | Set-Service -StartupType disabled}
    Get-Service $service | Set-Service -StartupType disabled}
    
    ## Script that enable services and start it ##
    $Serverlist = Get-Content E:\Temp\srv.txt
    $service = "wuauserv"
    Invoke-Command -ComputerName $Serverlist {Get-Service -Name $service | Set-Service -StartupType Manual}
    Invoke-Command -ComputerName $Serverlist {Get-Service -Name $service | Start-Service}
    
  • #91814

    Keymaster
    Points: 1,619
    Helping HandTeam Member
    Rank: Community Hero

    It would be faster to use New-PSSession to create a bunch of session objects against $ServerList. You can then use those sessions with Invoke-Command instead of -ComputerName. Re-using those sessions is a lot quicker than spinning up new sessions each time. See, "Secrets of PowerShell Remoting" on our ebooks menu.

    Variables inside the { script block } **are evaluated on the remote machine**. The remote machine won't know what $service contains, because that variable only lives on your local computer. You would use $using:service instead, to transfer the value from your local machine to the remote run space. See https://devops-collective-inc.gitbooks.io/the-big-book-of-powershell-gotchas/content/manuscript/remote-variables.html.

    I don't understand what "After Pathed is done" means, though, I'm sorry.

    • #91825

      Participant
      Points: 1
      Rank: Member

      Hmm sorry for bad english, AfterPatched is done. I trying to say that after the servers are patched and done the wuauserv must be stopped and set in disabled.

  • #91826

    Keymaster
    Points: 1,619
    Helping HandTeam Member
    Rank: Community Hero

    So, there's no easy or straightforward way of detecting when the installation is complete. It all depends on the patch being installed. You may have to loop every 5 minutes, for example, and query something like Win32_QuickFixEngineering to see if the hot fix ID shows as installed or not.

    • #91828

      Participant
      Points: 1
      Rank: Member

      We use BatchPatch for patching all servers remotely and it works really good.
      Just need to get this wuauserv up and kicking before and i dont want to login on each machine for this.

  • #91831
    Jon

    Participant
    Points: 24
    Rank: Member

    You've got the code, where are you having issues?

    I'd create a .ps1 file with the following code before patching

    #getservice
    $service = Get-Service -name wuauserv
    
    #set startup type
    $service | set-service -startuptype Manual
    
    #startservice
    $service | start-service

    then run

    invoke-command -computername $serverlist -filepath C:\scripts\startservices.ps1

    and to stop them

    #getservice
    $service = Get-Service -name wuauserv
    
    #stop service
    $service | stop-service
    
    #set startup type
    $service | set-service -startuptype Disabled
    
    

    Then run

    invoke-command -computername $serverlist -filepath C:\scripts\stopservices.ps1
  • #91832
    Jon

    Participant
    Points: 24
    Rank: Member

    Looks like you can even do it with batch patch!

    Remotely Starting and Stopping Services on Numerous Computers

    • #91835

      Participant
      Points: 1
      Rank: Member

      Really nice work Jon!! 🙂 works like charm

      Yeh seems to work in BatchPatch also, will try to add it in on to my Patch Jobs

The topic ‘disable and enable services’ is closed to new replies.