Author Posts

February 19, 2018 at 9:19 pm

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

February 19, 2018 at 9:24 pm

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

Or

$services = Get-Service

Or

ForEach ($service in (Get-Services)) {
}

February 19, 2018 at 9:28 pm

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

February 19, 2018 at 9:31 pm

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

February 19, 2018 at 9:36 pm

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

February 19, 2018 at 9:37 pm

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.

February 19, 2018 at 9:37 pm

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

February 19, 2018 at 9:39 pm

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

February 19, 2018 at 11:56 pm

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.

February 20, 2018 at 12:46 am

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}

February 20, 2018 at 1:04 am

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

February 20, 2018 at 2:12 pm

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