disable and enable services

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}

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.

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.

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.

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.

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

Looks like you can even do it with batch patch!

https://batchpatch.com/remotely-starting-and-stopping-services-on-numerous-computers

Really nice work Jon!! :slight_smile: works like charm

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