首先需要添加信任主机
winrm quickconfig -q
$cname = "172.18.18.36"
set-Item WSMan:\localhost\Client\TrustedHosts -Value $cname
现在通过输入用户名密码获取操作权限
$c = Get-Credential
$session =New-PSSession -ComputerName $cname -Credential $c
测试下远程操作命令的效果:
invoke-command -session $session -scriptblock {pwd}
invoke-command -session $session -scriptblock {netstat -ano}
现在可以以下多个命令的任意一个重启机器了:
invoke-command -session $ser1 -scriptblock {Shutdown -r -t 0}
icm $session {cmd.exe /c shutdown /r /f /t 1}
icm $session {Restart-Computer -com .}
icm $session {(gwmi win32_operatingsystem).Win32Shutdown(6)}
如果已经添加了信任主机,下次想直接执行命令,就是用下面命令:
$cname = "172.18.18.36"
$c = Get-Credential
$session =New-PSSession -ComputerName $cname -Credential $c
icm $session {netstat -ano}
icm $session {cmd /c dir c:\}
网友评论