一、获取服务器登录凭证
bash
$Username = 'myft\xngrq'
$PWD = '123!@#'
#将密码加密成特殊的字符串对象
$pass = ConvertTo-SecureString -AsPlainText $PWD -Force
#创建一个登录凭证对象
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
二、远程发送命令
bash
#向远程电脑发送命令
Invoke-Command -ComputerName 10.5.11.218 -ScriptBlock { Stop-Service Mysoft.DataCenterService50780 } -credential $Cred
三、远程启停服务
bash
echo "--------------停止服务 ---------------------------------------------------------"
#向远程电脑发送命令
Invoke-Command -ComputerName 10.5.11.218 -ScriptBlock { Stop-Service Mysoft.DataCenterService50780 } -credential $Cred
#进行一系列环境更新
echo "--------------重启服务 ---------------------------------------------------------"
#向远程电脑发送命令
Invoke-Command -ComputerName 10.5.11.218 -ScriptBlock { Restart-Service ysoft.DataCenterService50780 } -credential $Cred