Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PowerShell脚本更改Windows服务_Windows_Powershell_Service - Fatal编程技术网

使用PowerShell脚本更改Windows服务

使用PowerShell脚本更改Windows服务,windows,powershell,service,Windows,Powershell,Service,我编写了一个PowerShell脚本来更改远程VM上服务的登录用户。当我执行它时,它工作。但是,当我将它发送给同事时,脚本显示它运行时没有错误,并且他们检查后仍然将其列为“本地帐户” $account = Read-Host "Please admin account Name" $password = Read-Host -AsSecureString "Please enter your password" $password = [System.Runtime.InteropServic

我编写了一个PowerShell脚本来更改远程VM上服务的登录用户。当我执行它时,它工作。但是,当我将它发送给同事时,脚本显示它运行时没有错误,并且他们检查后仍然将其列为“本地帐户”

$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

我会移动这个街区

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()
}
进入
invoke命令
block.Cmd允许使用
-Computer
参数以专有方式实现远程操作,而
invoke命令
使用
WSMAN
(>更标准化的方式)

试试这个:

foreach ($computer in $computers) {
   # $computer can be either an IP-address or a FQDN e.g. computer.mydomain
   invoke-command -computer $computer -credential (get-credential) -scripblock {
      $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()
   }
}
根据该建议,所有操作都在远程计算机上执行。与第一次尝试相反,第一次尝试将远程对象“获取”到本地计算机(对象被转换),然后在本地对转换的对象执行一些操作(->更改的属性被发送回远程)

如果您的计算机与远程计算机不在同一域中,则必须将远程目标添加到本地受信任主机列表中。这说明了如何更新受信任主机列表


您还应该检查Powershell远程处理是否在您的目标上处于活动状态,如本文所述。如果您的目标操作系统是WIN Server 2012 R2,则Powershell远程处理默认为活动状态。

了解错误是什么将非常有帮助。一个好的起点是,您确定他们在与您的code?他们的PowerShell执行策略是否允许远程代码运行?此外,您正在让他们输入密码。您应该使用
获取凭据
,这样会更安全。我会输出
$computer
,因为听起来您的脚本没有找到
\servers.txt
,并且确定它没有任何内容to do.for the server.txt正在从他们更新的servers.txt文件中读取VM的名称。没有提供任何错误消息,只是看起来运行时没有问题,但没有对指定的VM进行任何更改。@FoxDeploy通常是的,但因为无论如何都需要明文密码,所以使用
Get Credential
不会改进一切。除了编码,远程PC上的帐户是否具有“作为服务登录”权限?感谢您的尝试,但我在尝试运行它时遇到以下错误:“调用命令:一个或多个计算机名无效。”。如果您试图传递URI,请使用-ConnectionUri参数,或传递URI对象而不是字符串。“检查是否需要在受信任主机列表中添加远程目标。我已更新了答案。”。