Script help

This topic contains 14 replies, has 4 voices, and was last updated by  Simeon Iksanov 1 year, 12 months ago.

  • Author
    Posts
  • #30122

    Graham Beer
    Participant

    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" ) 
    
  • #30125

    Don Jones
    Keymaster

    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.

  • #30127

    Graham Beer
    Participant

    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.

  • #30128

    Don Jones
    Keymaster

    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.

  • #30130

    Graham Beer
    Participant

    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.

  • #30131

    Don Jones
    Keymaster

    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.

  • #30132

    Graham Beer
    Participant

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

  • #30137

    Graham Beer
    Participant

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

  • #30163

    Simeon Iksanov
    Participant

    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
    }

  • #30164

    Graham Beer
    Participant

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

  • #30170

    Graham Beer
    Participant

    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)."}
    
  • #30171

    Graham Beer
    Participant

    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: 
    
  • #30172

    Graham Beer
    Participant

    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
    
  • #30173

    Graham Beer
    Participant

    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
    }
    
  • #30213

    Simeon Iksanov
    Participant

    looks fine

You must be logged in to reply to this topic.