我想使用PowerShell更新在多个服务器上的一个帐户下运行的所有服务的密码。我尝试了Get-process,Get-WMIObject cmdlet,但这两个命令没有serviceaccount用法。有没有办法通过将服务帐户,密码作为参数传递给脚本来更新所有使用帐户运行的服务的密码。
答案 0 :(得分:10)
要获取使用特定帐户的服务列表,您可以执行以下操作:
Get-WmiObject "win32_service" -Filter "StartName='domain\\user'"
要更改这些密码,您可以执行以下操作:
Get-WmiObject "win32_service" -Filter "StartName='domain\\user'" |
%{$_.StopService();$_.Change($null,$null,$null,$null,$null,$null,$null,"blah");}
从这里开始:http://www.send4help.net/change-remote-windows-service-credentials-password-powershel-495
答案 1 :(得分:2)
试试这个:
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
评论后编辑:
$a = (GET-WMIOBJECT win32_service) | ? { $_.startname -eq "domain\\username"} | %{$_.StopService();$_.Change($null,$null,$null,$null,$null,$null,$null,"newpassword");}
答案 2 :(得分:1)
这是你们需要的
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
答案 3 :(得分:0)
是的-这似乎是最好的方法
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