how to put services into array??

Welcome Forums General PowerShell Q&A how to put services into array??

This topic contains 11 replies, has 3 voices, and was last updated by

 
Participant
9 months ago.

  • Author
    Posts
  • #94066

    Participant
    Points: -19
    Rank: Member

    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

    Keymaster
    Points: 1,644
    Helping HandTeam Member
    Rank: Community Hero
    $services = Get-WmiObject "win32_service" -Filter "StartName='domain\\user'"
    

    Or

    $services = Get-Service
    

    Or

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

    Participant
    Points: -19
    Rank: Member

    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

    Keymaster
    Points: 1,644
    Helping HandTeam Member
    Rank: Community Hero

    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

    Participant
    Points: -19
    Rank: Member

    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

    Keymaster
    Points: 1,644
    Helping HandTeam Member
    Rank: Community Hero

    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

    Keymaster
    Points: 1,644
    Helping HandTeam Member
    Rank: Community Hero

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

  • #94092

    Participant
    Points: -19
    Rank: Member

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

  • #94123

    Participant
    Points: -19
    Rank: Member

    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

    Participant
    Points: 170
    Helping Hand
    Rank: 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

    Keymaster
    Points: 1,644
    Helping HandTeam Member
    Rank: Community Hero

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

  • #94180

    Participant
    Points: -19
    Rank: Member

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

The topic ‘how to put services into array??’ is closed to new replies.