Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list all the services running with a service account in a server using Powershell

Tags:

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

like image 699
Ram490 Avatar asked Dec 04 '22 20:12

Ram490


2 Answers

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

like image 130
manojlds Avatar answered Jan 07 '23 06:01

manojlds


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");}
like image 32
CB. Avatar answered Jan 07 '23 07:01

CB.