是否有人有Powershell腳本來更改Windows服務使用的憑據?用於更改服務帳戶的Powershell腳本
回答
位 - 使用WMI。
$service = gwmi win32_service -computer [computername] -filter "name='whatever'"
$service.change($null,$null,$null,$null,$null,$null,$null,"[email protected]")
在過濾器中正確更改服務名稱;適當地設置遠程計算機名稱。
只是關於這個答案的說明。如果您想更新用戶帳戶,則需要更新前面的密碼值。即$ service.change($ null,$ null,$ null,$ null,$ null,$ null,「。\ MyAccount」,「P @ ssw0rd」)。看來你總是需要在帳戶名稱前加上域名或「。\」,否則它將無法工作。有關其他參數的更多信息,請參閱此處:http://msdn.microsoft.com/en-us/library/windows/desktop/aa384901(v=vs.85).aspx – Rohland 2012-06-19 10:25:59
此外,請查看我的答案以下使用此之前。服務帳戶更改需要重置服務才能生效,並且包含示例代碼。 – 2012-09-12 15:59:56
考慮到whithin這個類:
$class=[WMICLASS]'\\.\root\Microsoft\SqlServer\ComputerManagement:SqlService'
有一個名爲方法setserviceaccount()
,可能是這個script會做你想要的:更容易
# Copyright Buck Woody, 2007
# All scripts provided AS-IS. No functionality is guaranteed in any way.
# Change Service Account name and password using PowerShell and WMI
$class = Get-WmiObject -computername "SQLVM03-QF59YPW" -namespace
root\Microsoft\SqlServer\ComputerManagement -class SqlService
#This remmed out part shows the services - I'll just go after number 6 (SQL
#Server Agent in my case):
# foreach ($classname in $class) {write-host $classname.DisplayName}
# $class[6].DisplayName
stop-service -displayName $class[6].DisplayName
# Note: I recommend you make these parameters, so that you don't store
# passwords. At your own risk here!
$class[6].SetServiceAccount("account", "password")
start-service -displayName $class[6].DisplayName
我創建一個文本文件「changeserviceaccount.ps1」包含以下腳本:
$account="domain\user"
$password="passsword"
$service="name='servicename'"
$svc=gwmi win32_service -filter $service
$svc.StopService()
$svc.change($null,$null,$null,$null,$null,$null,$account,$password,$null,$null,$null)
$svc.StartService()
我一個窗口服務的開發過程中使用這種由生成後命令行的一部分:
Visual Studio中:項目屬性\生成事件
預生成事件命令行:
"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\installutil.exe" myservice.exe /u
生成後事件命令行:
"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\installutil.exe" myservice.exe
powershell -command - < c:\psscripts\changeserviceaccount.ps1
我寫了PowerShell中改變用戶名,密碼功能,並重新啓動遠程計算機上的服務(你可以使用localhost,如果你想改變本地服務器)。我已經使用這個數字服務帳戶密碼在數百臺服務器上重置。
您可以在http://www.send4help.net/change-remote-windows-service-credentials-password-powershel-495
它還等待,直到該服務完全停止嘗試再次啓動它,不像其他的答案中尋找的原件及複印件。
Function Set-ServiceAcctCreds([string]$strCompName,[string]$strServiceName,[string]$newAcct,[string]$newPass){
$filter = 'Name=' + "'" + $strServiceName + "'" + ''
$service = Get-WMIObject -ComputerName $strCompName -namespace "root\cimv2" -class Win32_Service -Filter $filter
$service.Change($null,$null,$null,$null,$null,$null,$newAcct,$newPass)
$service.StopService()
while ($service.Started){
sleep 2
$service = Get-WMIObject -ComputerName $strCompName -namespace "root\cimv2" -class Win32_Service -Filter $filter
}
$service.StartService()
}
這裏的其他腳本略有不同,如下所示。這將爲給定登錄帳戶下運行的任何/所有服務設置憑據。它只會嘗試在服務已經運行的情況下重新啓動服務,以便我們不會意外啓動因某種原因而停止的服務。該腳本必須運行並提升shell(如果腳本開始告訴你關於ReturnValue = 2
,你可能運行它未升級)。一些有用的例子是:在運行
所有服務作爲當前登錄的用戶,在本地主機上:
.\set-servicecredentials.ps1 -password [email protected]
所有服務運行的用戶:
somedomain\someuser
主機somehost.somedomain
:.\set-servicecredentials.ps1 somehost.somedomain somedomain\someuser [email protected]
集-ServiceCredentials.ps1:
param (
[alias('computer', 'c')]
[string] $computerName = $env:COMPUTERNAME,
[alias('username', 'u')]
[string] $serviceUsername = "$env:USERDOMAIN\$env:USERNAME",
[alias('password', 'p')]
[parameter(mandatory=$true)]
[string] $servicePassword
)
Invoke-Command -ComputerName $computerName -Script {
param(
[string] $computerName,
[string] $serviceUsername,
[string] $servicePassword
)
Get-WmiObject -ComputerName $computerName -Namespace root\cimv2 -Class Win32_Service | Where-Object { $_.StartName -eq $serviceUsername } | ForEach-Object {
Write-Host ("Setting credentials for service: {0} (username: {1}), on host: {2}." -f $_.Name, $serviceUsername, $computerName)
$change = $_.Change($null, $null, $null, $null, $null, $null, $serviceUsername, $servicePassword).ReturnValue
if ($change -eq 0) {
Write-Host ("Service Change() request accepted.")
if ($_.Started) {
$serviceName = $_.Name
Write-Host ("Restarting service: {0}, on host: {1}, to implement credential change." -f $serviceName, $computerName)
$stop = ($_.StopService()).ReturnValue
if ($stop -eq 0) {
Write-Host -NoNewline ("StopService() request accepted. Awaiting 'stopped' status.")
while ((Get-WmiObject -ComputerName $computerName -Namespace root\cimv2 -Class Win32_Service -Filter "Name='$serviceName'").Started) {
Start-Sleep -s 2
Write-Host -NoNewline "."
}
Write-Host "."
$start = $_.StartService().ReturnValue
if ($start -eq 0) {
Write-Host ("StartService() request accepted.")
} else {
Write-Host ("Failed to start service. ReturnValue was '{0}'. See: http://msdn.microsoft.com/en-us/library/aa393660(v=vs.85).aspx" -f $start) -ForegroundColor "red"
}
} else {
Write-Host ("Failed to stop service. ReturnValue was '{0}'. See: http://msdn.microsoft.com/en-us/library/aa393673(v=vs.85).aspx" -f $stop) -ForegroundColor "red"
}
}
} else {
Write-Host ("Failed to change service credentials. ReturnValue was '{0}'. See: http://msdn.microsoft.com/en-us/library/aa384901(v=vs.85).aspx" -f $change) -ForegroundColor "red"
}
}
} -Credential "$env:USERDOMAIN\$env:USERNAME" -ArgumentList $computerName, $serviceUsername, $servicePassword
我不能在默認的PS堆棧發現了什麼,我發現它在Carbon
實施:
- 1. 使用PowerShell腳本更改Windows服務
- 2. 更改的服務器帳戶和php腳本不再有效
- 3. SQL Server服務帳戶使用Powershell更改
- 4. 驗證服務帳戶Powershell
- 5. 更改用於運行服務的帳戶的密碼
- 6. Powershell服務帳戶密碼更改登錄失敗
- 7. 使用powershell修改本地帳戶
- 8. 在Google dataproc上更改服務帳戶
- 9. 更改服務帳戶密碼
- 10. 如何使用powershell腳本更改clearcase_albd帳戶域名和加密密碼?
- 11. 用於CSV修改的Slow Powershell腳本
- 12. 如何更改GAE本地服務器在用戶帳戶中的登錄名?
- 13. 使用本地系統帳戶運行exe的PowerShell腳本
- 14. 更改服務使用的帳戶的密碼
- 15. 如何更改路徑,用戶創建的混帳腳本
- 16. 如何更改使用PowerShell腳本
- 17. PowerShell腳本中的獲取服務
- 18. Powershell更改SQL Server的備份腳本
- 19. Google Drive上傳腳本的OAuth2服務帳戶憑證
- 20. PowerShell腳本來檢查是否從CSV文件啓用帳戶
- 21. C#GMAIL API服務帳戶修改 - unauthorized_client
- 22. 如何更改與sql服務器配合的帳戶
- 23. IIS管理員可以更改Windows服務帳戶的密碼
- 24. 如何將mongodb導出的powershell命令行更改爲Powershell腳本(用於計劃的任務目的)
- 25. PowerShell腳本重新啓動服務
- 26. PowerShell腳本for Windows服務器
- 27. MS Exchange服務器SMTP名PowerShell腳本
- 28. Powershell腳本更新多臺服務器上的DNS
- 29. 服務帳戶oauth2
- 30. 用於合併客戶帳戶的SQL腳本
您可以刪除獎勵問題文本嗎?我注意到你單獨發佈了。讓我們不要混淆人們,並讓答案走錯了地方。 – halr9000 2008-11-24 13:35:07
[使用`sc.exe`](http://stackoverflow.com/q/308298/1394393)也是一個選項。 – jpmc26 2017-04-13 21:03:26