Author Posts

September 25, 2015 at 6:42 am

Hi,

I'm trying to pass a list of services to the get-ciminstance Win32_Service class to be able to disable them. I get the list of services i require into a variable but after trying foreach commands, expressions can't get it to work.Help !

$services = get-service -ErrorAction SilentlyContinue @(
"Distributed Link Tracking Client",
"Family Safety",	
"Function Discovery Provider Host",
"Function Discovery Resource Publication",
"HomeGroup Listener",
"HomeGroup Provider",
"Internet Connection Sharing (ICS)",
"IP Helper",
"KtmRm for Distributed Transaction Coordinator",
"Microsoft iSCSI Initiator Service",
"Microsoft Keyboard Filter",
"Net.Tcp Port Sharing Service",
"Offline Files",
"Peer Name Resolution Protocol",
"Peer Networking Grouping",
"Peer Networking Identity Manager",
"PNRP Machine Name Publication Service",
"Quality Windows Audio Video Experience",
"Remote Access Auto Connection Manager",
"Remote Access Connection Manager",
"Remote Registry",
"Routing and Remote Access",
"Sensor Monitoring Service",
"Smart Card",
"SSDP Discovery",
"Telephony",
"UPnP Device Host",
"WebClient",
"Windows Connect Now - Config Registrar",
"Windows Media Player Network Sharing Service",
"WinHTTP Web Proxy Auto-Discovery Service" ) 

September 25, 2015 at 6:49 am

It'd help if you could explain what "can't get it to work" means in a bit more detail. However, the problem is likely that you're trying to use positional parameters, and you're using them out of position.

Try:

$services = get-service -ErrorAction SilentlyContinue -DisplayName @(

Instead. Also make sure you know the difference between a service's Name and its DisplayName, because that tells you if you should be using -Name or -DisplayName given your input.

September 25, 2015 at 6:52 am

Hi Don,

Sorry.

So i have a list of descriptions that i want to get from get-service and then pass the "name" of the service to the Win32_Service class.
From there i want to set them to disabled. Part of a build "hardening" process for OSD.

September 25, 2015 at 6:58 am

I'm not sure you NEED to use CIM or WMI. Can't use just use the Set-Service cmdlet to specify the services you want, and a new start mode?

And what input information do you have? The services' Name, DisplayName, or Description? You can't easily query by Description, is why I ask. You'll end up having to query all the services, and then filter out the Descriptions you don't want, if that's what your input is.

September 25, 2015 at 7:01 am

Been banging my head and i think you have made it a bit clearer for me.

So i have the description of the service and the set-service might just work.

Thank you.

September 25, 2015 at 7:03 am

So, in pseudo-code, you have to do something like...

Get-Service -ComputerName whatever |
Where { $_.Description -in @('my','list','of','descriptions') } |
Set-Service -ComputerName whatever -StartMode Disabled

Vaguely.

September 25, 2015 at 7:05 am

That's brilliant Don. You've come up with something i couldn't in hours. Very grateful.

September 25, 2015 at 8:13 am

Go that working a treat, thanks. How could I get output of stopping services to use write-warning for each service ?

September 26, 2015 at 11:31 pm

Get-Service -ComputerName whatever |
Where { $_.Description -in @('my','list','of','descriptions') } |
Foreach-Object {
Write-Warning "service $_.name is going to be disabled"
Set-Service -ComputerName whatever -StartMode Disabled
}

September 27, 2015 at 1:53 am

That's awesome, I did manage to find a way but that's certainly better, thanks Simeon.

September 27, 2015 at 11:25 pm

This is how i achieved it, does it look ok ?

$Disabled = (
Get-Service -ComputerName $ENV:COMPUTERNAME |
Where { $_.Displayname -in @(
"Fax",
"Family Safety"
 ) } |
set-Service -StartMode manual -PassThru ) 

$disabled | ForEach-Object { write-host -ForegroundColor Green "$ENV:COMPUTERNAME : Successfully disabled the service $($_.name)."}

September 27, 2015 at 11:40 pm

Hi Simeon,

Getting an issue when i run your suggestion, doesn't seem to pass the services on..

Get-Service -ComputerName $ENV:COMPUTERNAME |
Where { $_.Displayname -in 
@("Fax",
"Family Safety"
 ) } |
Foreach-Object {
Write-Warning "service $_.name is going to be disabled"
Set-Service -ComputerName $ENV:COMPUTERNAME -StartMode Disabled
}
PS C:\sccm> Get-Service -ComputerName $ENV:COMPUTERNAME |
Where { $_.Displayname -in 
@("Fax",
"Family Safety"
 ) } |
Foreach-Object {
Write-Warning "service $_.name is going to be disabled"
Set-Service -ComputerName $ENV:COMPUTERNAME -StartMode Disabled
}
WARNING: service System.ServiceProcess.ServiceController.name is going to be disabled
cmdlet Set-Service at command pipeline position 1
Supply values for the following parameters:
Name: 

September 27, 2015 at 11:46 pm

So by amending $_.name to $($_.name) it all works up until

Get-Service -ComputerName $ENV:COMPUTERNAME |
Where { $_.Displayname -in 
@("Fax",
"Family Safety"
 ) } |
Foreach-Object {
Write-Warning "service $($_.name) is going to be disabled" }

The last line doesn't seem to pass the name....

Set-Service -ComputerName $ENV:COMPUTERNAME -StartMode Disabled

September 27, 2015 at 11:50 pm

Sorry, another post,

I get it to work if i code it like this, but is there a better way ?

Get-Service -ComputerName $ENV:COMPUTERNAME |
Where { $_.Displayname -in 
@("Fax",
"Family Safety"
 ) } |
Foreach-Object {
Write-Warning "service $($_.name) is going to be disabled" 
Set-Service $($_.name) -ComputerName $ENV:COMPUTERNAME -StartMode Disabled
}