How to change user credentials of windows service from command line?

廉价感情. 提交于 2019-11-27 20:28:37

问题


How to change user credentials of windows service from command line?


回答1:


sc.exe config "Service Name" obj= "DOMAIN\User" password= "password" type= own

See Shortcut Setting Log-On Credentials for Windows Services » jonathanmalek.com.

@MattT points out that on Windows Server 2008R2 you have to add type= own, but prior to that version it isn't necessary.

In PowerShell 3+, you can avoid escaping the arguments with the stop-parsing symbol: --%

sc.exe --% config "Service Name" obj= "DOMAIN\User" password= "password" type= own



回答2:


I simply called WMI from powershell to do this.

$Svc = Get-WmiObject win32_service -filter "name='ServiceName'"
$Svc.Change($Null, $Null, $Null, $Null, $Null, $Null, "User", "Password")

Don't forget to restart the service afterwards:

Stop-Service -Name 'ServiceName'
Start-Service -Name 'ServiceName'

For more fun with WMI and services, see Win32_Service Class




回答3:


Using WMI results in non-encrypted communication between your machine and the machine you are changing the service credentials on. So your new password can be sniffed quite easily. You just have to parse the WMI blob send over the network. By now I found no really secure way to change a service accounts password remotely with a tool.




回答4:


For those who are wondering how to pass a secure password:

$credentials = Get-Credential -UserName 'Domain\username' -Message 'Enter password below'
$service = Get-WmiObject win32_service -filter "name='SERVICE_NAME'"
$service.Change($null,$null,$null,$null,$null,$null,$credentials.username,($credentials.Password | ConvertFrom-SecureString))


来源:https://stackoverflow.com/questions/966389/how-to-change-user-credentials-of-windows-service-from-command-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!