简体   繁体   English

使用PowerShell脚本更改Windows服务

[英]Change Windows service with PowerShell script

I wrote a PowerShell script to change the login user for a service on my remote VMs. 我编写了一个PowerShell脚本,以更改远程VM上的服务的登录用户。 It works when I execute it. 当我执行它时,它可以工作。 However, when I send it to my coworkers, the script appears that it ran without errors and they checked it still listed as "local account'. 但是,当我将其发送给我的同事时,脚本显示该脚本运行无误,他们检查了该脚本仍列为“本地帐户”。

$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

I would move this block 我会移动这个方块

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()
}

into a invoke-command block. 进入invoke-command块。 Cmd-lets using the -Computer parameter implement the remote actions in a proprietary way, while invoke-command uses WSMAN (-> more standardized way). 使用-Computer参数的Cmd-let以专有方式实现远程操作,而invoke-command使用WSMAN (->更标准化的方式)。

Try this: 尝试这个:

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()
   }
}

With that proposal all actions are performed on the remote machine. 根据该建议,所有操作都在远程计算机上执行。 In contrary to the first attempt. 与第一次尝试相反。 The first attempt "fetches" the remote objects to you local machine (objects are converted), than you locally perform some actions on the converted object (-> changed properties are send back to the remote). 第一次尝试将远程对象“获取”到本地计算机(对象已转换),而不是在本地对转换后的对象执行一些操作(->更改后的属性被发送回远程)。

If your computer is not in the same domain as the remote ones, you've to add your remote targets to your local trusted host list. 如果您的计算机与远程计算机不在同一域中,则必须将远程目标添加到本地受信任主机列表中。 This link describes how to update your trusted hosts list. 链接描述了如何更新您的可信主机列表。

You should also check if Powershell remoting is active on your targets, also described in this link . 您还应该检查Powershell远程处理是否在目标上处于活动状态,这也在本链接中进行了描述。 If your target OS is WIN Server 2012 R2 Powershell remoting is active per default. 如果目标操作系统是WIN Server 2012 R2,则默认情况下将激活Powershell远程处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM