脚本创建本地管理员,然后使用不同的密码将其添加到服务

时间:2016-08-10 17:15:24

标签: powershell service change-password

我有一个脚本可以创建本地用户帐户,设置密码,将本地帐户添加到Administrators组,然后将服务设置为以此帐户运行。问题是当尝试重新启动服务时出现错误:

  

错误1069:由于登录失败,服务未启动。

我发现当我手动设置密码时,我可以启动该服务。

问题似乎是如何将密码传递给$service.Change($null,...,$password,...,$null)方法。

$name = $env:COMPUTERNAME
$computer=[ADSI]"WinNT://$name"
$newuser = $computer.Create('User','newuser')
$password = "Password1"
$newuser.SetPassword($password) #this works
$newuser.SetInfo()
$admin = "Administrators"
$group=[ADSI]"WinNT://$name/$admin,Group"
$group.Add($newuser.path)

$service = Get-WmiObject Win32_service -filter "name='ServiceName'"
$service.Change($null,$null,$null,$null,$null,$null,".\newuser",$password,$null,$null,$null) #this doesn't. Password has not changed.
Restart-Service -Name $service.Name
Restart-Service : Service 'Service Name (ServiceName)' cannot be started due to
the following error: Cannot start service ServiceName on computer '.'.
At C:\Users\superfoo\Documents\Scripts\ServiceUpdate.ps1:68 char:3
+   Restart-Service -Name $serviceControl
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Restart-Service], ServiceCommandException
    + FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.RestartServiceCommand

$password在创建帐户时有效,但不能在服务上设置密码。我以管理员身份运行脚本。

操作系统:Windows 10(x64) Powershell:版本5.0.10586.122

0 个答案:

没有答案