Welcome › Forums › General PowerShell Q&A › disable and enable services
This topic contains 7 replies, has 3 voices, and was last updated by
-
AuthorPosts
-
January 19, 2018 at 10:59 am #91763
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}
-
January 19, 2018 at 3:52 pm #91814
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.
-
January 19, 2018 at 6:51 pm #91825
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.
-
-
January 19, 2018 at 6:55 pm #91826
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.
-
January 19, 2018 at 7:28 pm #91828
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.
-
-
January 19, 2018 at 7:56 pm #91831
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
-
January 19, 2018 at 8:01 pm #91832
Looks like you can even do it with batch patch!
Remotely Starting and Stopping Services on Numerous Computers
-
January 19, 2018 at 9:31 pm #91835
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
-
-
AuthorPosts
The topic ‘disable and enable services’ is closed to new replies.