I recently ran into an issue. Where specific services that I feel should not run on a server were on the server. On top of this the services that were running are throwing red flags. We all know the red flags take away from real problems. Not to mention as these services shouldn’t be on the system anyway. What services? For instance, the MapsBroker service allows downloading maps. The gupdate service is a Google update service. The OneSyncSvc, the service that started it all for me. The OneSyncSvc is a sync service, but Microsoft does not specify what it syncs, time to go!). I had to do something about it. I have to find a way to disable nonessential server services using PowerShell. Because. Reasons.
Primarily because I did not want to see any more flags on the server. I found this thread that was the exact issue I am having. Around halfway down the page, I found the CMD solution by Elvis Gustin. I did not want to use DOS, as the same command fails in PowerShell. I came up with another way.
#Start with the array, fill in any other services you might need to disable. $ServiceName = @("OneSyncSvc", "MapsBroker", "gupdate", "DDVCollectorSvcApi", "DDVDataCollector") #ForEach variable in the array do this. foreach ($s in $ServiceName) { #This was my method of getting out of the quote hell I was facing adding everything in one line while interjecting the $s variable. $Command = "cmd.exe /C sc config " $Tail = " start= disabled" $Complete = "$Command$s$Tail" #Lastly the actual command to do the work. Invoke-Expression -Command:$Complete }
It is worth noting that since we are not piping “|” any variable we cannot use the $_ in place of $s variable when building the $Complete variable. The reason for this is $_ represents the current variable in the pipeline, and if we are not using a piping character “|”.
Here’s the output from PowerShell: (Two of the services were not on this server, hence the failure at the end)
[SC] ChangeServiceConfig SUCCESS [SC] ChangeServiceConfig SUCCESS [SC] ChangeServiceConfig SUCCESS [SC] OpenService FAILED 1060:The specified service does not exist as an installed service. [SC] OpenService FAILED <p>1060:The specified service does not exist as an installed service.
You will notice that there is little to no feedback from PowerShell as to what is happening in regards to which service PowerShell is currently working. What we can do is add some output for the console, so the admin doesn’t feel too lonely. Here’s how;
#Start with the array, fill in any other services you might need to disable. $ServiceName = @("OneSyncSvc", "MapsBroker", "gupdate", "DDVCollectorSvcApi", "DDVDataCollector") #ForEach variable in the array do this. foreach ($s in $ServiceName) { #This was my method of getting out of the quote hell I was facing adding everything in one line while interjecting the $s variable. $Command = "cmd.exe /C sc config " $Tail = " start= disabled" $Complete = "$Command$s$Tail" #Lastly the actual command to do the work. Write-Host Disabling $s service: Write-Host "" -NoNewLine Invoke-Expression -Command:$Complete Write-Host "" }
This is much better read by a human. We know what service the system was working on that completed, and we know what service the system was unable to make changes to along with spacing it out and grouping the commands together. Here is the output;
Disabling OneSyncSvc service: [SC] ChangeServiceConfig SUCCESS Disabling MapsBroker service: [SC] ChangeServiceConfig SUCCESS Disabling gupdate service: [SC] ChangeServiceConfig SUCCESS Disabling DDVCollectorSvcApi service: [SC] OpenService FAILED 1060: The specified service does not exist as an installed service. Disabling DDVDataCollector service: [SC] OpenService FAILED 1060: The specified service does not exist as an installed service.
That’s all, I hope it helps.