how to put services into array??

This topic contains 11 replies, has 3 voices, and was last updated by  tommls 3 months ago.

  • Author
    Posts
  • #94066

    tommls
    Participant

    I can use this to get which services run with this account:
    Get-WmiObject "win32_service" -Filter "StartName='domain\\user'"

    Where I'm stumped is how to put the services into an array so I can loop through the array and stop the services and of course then restart the services.

    air code:

    $services = do the above into array

    for each $service in $services
    stop the service

    change the domain\user password

    for each $service in $services
    stop the service

    Thank you, Tom

  • #94075

    Don Jones
    Keymaster
    $services = Get-WmiObject "win32_service" -Filter "StartName='domain\\user'"
    

    Or

    $services = Get-Service
    

    Or

    ForEach ($service in (Get-Services)) {
    }
    
  • #94083

    tommls
    Participant

    Alternatively how could I use something like this to get the services??

    $Services = Get-WmiObject Win32_Service -ComputerName "." | Where { $_.name -eq 'MSSQLSERVER' } — this is the general idea

    to become something like — this is almost what I need but I don't know how to construct the where part to properly query the start name of the service(s):

    $Services = Get-WmiObject Win32_Service -ComputerName "." | Where { startname -eq 'domain\\user' }

    Thank you, Tom

  • #94084

    Don Jones
    Keymaster

    This:

    $Services = Get-WmiObject Win32_Service -ComputerName "." | Where { $_.name -eq 'MSSQLSERVER' }
    

    Is a little inefficient.

    $Services = Get-WmiObject Win32_Service -ComputerName "." -Filter "Name='MSSQLSERVER'"
    

    Would be more efficient. And you can filter based on StartName the same way. You only use the double backslash when querying the way I've done here. If you're using Where-Object, then you'd use a single backslash. The double backslash is a WMI-specific escape sequence, not needed for filtering at PowerShell itself.

    Richard Siddaway wrote a lovely book on WMI and PowerShell, if you plan to mess with this stuff a lot. A lot of good tips and a ton of "how it works."

  • #94086

    tommls
    Participant

    Thank you, now I must learn how to change the password for that service account.
    The above array can be used to stop and start the services running with that account.
    Thank you, Tom

  • #94087

    Don Jones
    Keymaster

    The Win32_Service object has a couple of methods you can use to set the password – the Change() method is the one I've used most frequently.

  • #94089

    Don Jones
    Keymaster

    In fact, I think I might have used that exact example in my Toolmaking book.

  • #94092

    tommls
    Participant

    I saw that somewhere about the Change()!!
    I must look for it again. 🙂

  • #94123

    tommls
    Participant

    Could someone suggest how I fix this request for services??

    $services2 = Get-WMIObject Win32_Service -Filter "Name LIKE '%SQL%'" AND "StartName='$account'"

    The filter is the problem...I need the AND to narrow down the services.

  • #94128

    Olaf Soyk
    Participant

    You could make your life easier and use the Powershell way instead of the WMI way ...

    Get-CimInstance -ClassName Win32_Service |
        Where-Object -FilterScript {$_.Name -like '*SQL*' -and $_.StartName -eq $account}
  • #94135

    Don Jones
    Keymaster

    The entire filter statement goes inside one set of double quotes.

  • #94180

    tommls
    Participant

    Thank you everyone, now I better understand the get-ciminstance cmdlet too.
    Thank you, Tom

You must be logged in to reply to this topic.