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" )
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.
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.
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.
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.
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: