How to list all the services running with a service account in a server using Powershell
I want to update the password of all the services running under one account on multiple servers using powershell. i tried Get-process, Get-WMIObject cmdlets, but these two commands don't have s开发者_如何转开发erviceaccount usage. is there a way to update password of all the services running with an account by passing service account,password as parameters to the script.
To get list of services using a particular account you can do:
Get-WmiObject "win32_service" -Filter "StartName='domain\\user'"
To change the password for these, you can do:
Get-WmiObject "win32_service" -Filter "StartName='domain\\user'" |
%{$_.StopService();$_.Change($null,$null,$null,$null,$null,$null,$null,"blah");}
From here: http://www.send4help.net/change-remote-windows-service-credentials-password-powershel-495
try this:
Function GLOBAL:GET-PROCESSUSER ( $ProcessID ) {
(GET-WMIOBJECT win32_process –filter “Handle=$ProcessID”).GetOwner().User
}
$svcs = Get-Process | Select-Object name, starttime, ID
$a = @()
foreach ($svc in $svcs)
{
if ( $svc.name -ne "Idle" -and $svc.name -ne "System")
{
$al = New-Object System.Object
$al | Add-Member -type NoteProperty -name Name -Value $svc.name
$al | Add-Member -type NoteProperty -name Owner -Value ( get-processuser $svc.id)
$a += $al
}
}
$a
Edit after comment:
$a = (GET-WMIOBJECT win32_service) | ? { $_.startname -eq "domain\\username"} | %{$_.StopService();$_.Change($null,$null,$null,$null,$null,$null,$null,"newpassword");}
This is what you guys need
Get-WMIObject Win32_Service | Where-Object {$_.startname -ne "localSystem" }| Where-Object {$_.startname -ne "NT AUTHORITY\LocalService" } |Where-Object {$_.startname -ne "NT AUTHORITY\NetworkService" } |select startname, name
Yeah - this seems to be the best approach
Get-WMIObject Win32_Service | Where-Object {($_.startname -ne "NT AUTHORITY\LocalService") -and ($_.startname -ne "NT AUTHORITY\NetworkService") -and ($_.startname -ne "localSystem") } `
|select @{ Name = "Service Account " ; Expression = { ( $_.startname ) } }, `
@{ Name = "Service Dispaly Name " ; Expression = { ( $_.name ) } }, StartMode,State, Status | FT -AutoSize
精彩评论