我编写了PowerShell脚本来更改远程VM上的服务的登录用户。它在我执行它时起作用。然而,当我把它发送给我的同事,脚本看来,其运行没有任何错误,他们检查了它仍被列为“本地帐户”。使用PowerShell脚本更改Windows服务
$account = Read-Host "Please admin account Name"
$password = Read-Host -AsSecureString "Please enter your password"
$password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))
$service = Read-Host "Enter service name"
$computers = Get-Content -Path ".\servers.txt"
foreach ($computer in $computers) {
$svc = gwmi Win32_Service -ComputerName $computer -Filter "name='$service'"
$svc.StopService()
$svc.Change($null,$null,$null,$null,$null,$null,$account,$password,$null,$null,$null)
$svc.StartService()
}
Write-Host $service "is now running as" $account
Read-Host
这将是非常有用的知道错误是什么。一个好的起点,你确定他们有一个名为servers.txt的文本文件与你的代码在同一个目录中吗?他们的PowerShell执行策略是否允许远程代码运行?此外,你让他们输入他们的密码。您应该使用'Get-Credential',否则会更安全。 – FoxDeploy
我输出'$ computer',因为听起来你的脚本没有找到'。\ servers.txt'并且认为它没有任何关系。 – Filburt
for server.txt正在从它们更新的servers.txt文件读取VM的名称。没有提供任何错误消息,它看起来好像没有问题,但没有对其指定的虚拟机进行更改。 –