Author Posts

April 7, 2014 at 6:49 pm

Hey Scripting Experts!

I'm trying my 1st script out, well my 1st that I'm trying to write where I haven't found something online to put it together with. I did find information on how to disable services, but I can't find anything to do this for multiple services across multiple servers.
Not all of the servers will have all of the services. I want to try to get some "results" logged if possible.

Here is what I have to disable the multiple services, but how can I do this remotely and on multiple servers, either 1 server at a time or in parallel?
$ListOfServices = Get-Content E:\Powershellscripts\DisableTheseServices.txt
foreach ($service in $ListOfServices)
{
Set-Service -Name $service -StartupType Disabled
Stop-Service -Name $service –Force
Get-WmiObject Win32_Service |Where-Object {$_.name -eq $Service} | Format-List -Property Name,Startmode,State
}

April 8, 2014 at 4:42 am

So the Get-Service command lacks the property of the startmode so you have to resort to WMI to see that.

However the stupid thing is that the Set-Service command does allow you to do this and will handle piped in object names.

You're basically executing in the blind. The best thing I can recommend is use your Get-Content with your text file and pipe that to the Set-Service command and use the -Verbose switch it will display confirmations in yellow I believe.

Get-Content E:\Powershellscripts\DisableTheseServices.txt | Set-Service -StartupType Disabled -Verbose

-VERN

June 11, 2014 at 6:23 am

[code language="powershell"]
Get-Content E:\Powershellscripts\DisableTheseServices.txt | Set-Service -StartupType Disabled -Verbose
[/code]

June 11, 2014 at 7:18 am

According to its help file, the Name parameter of Set-Service doesn't accept multiple values, so you have to use foreach. I can't test this right now and I don't want to mess with services on my laptop, but that's what I'd try:

$services = Get-Service -Name (Get-Content E:\Powershellscripts\DisableTheseServices.txt)

foreach ($service in $services)
{
Stop-Service -Name $service –Force
Set-Service -Name $service -StartupType Disabled -Verbose
}

To remotely do this on multiple computers, you have to use Invoke-Command and its -ComputerName and -ScriptBlock or -FilePath parameters. The FilePath parameter makes it possible to run a local script on remote computers, so you could save it as a script and do that.
However, you may have to define some arguments and I'm not sure how to make it work.
If a foreach one-liner could accomplish the task, you'd just put that into the ScriptBlock parameter.
In either case, you can feed in the remote computer names into the ComputerName parameter with Get-Content or by typing a list of names.