使用 PowerShell 远程管理

要求

  1. PowerShell 版本要求至少是2.0版本以上,目前PowerShell 2.0 支持最低的操作系统版本为Windows XP。本次操作使用的是 PowerShell 5.1
  2. 请使用管理员身份操作PowerShell

服务端

Enable-PSRemoting

就是如此简单。

客户端

1. 设置服务端为可信主机,如果读者的计算机与服务器在同一个域中,请忽略此步骤

Set-Item wsman:localhost\Client\TrustedHosts -value <IP或计算机名称>

2. 连接服务端

如果读者的计算机在域中,请将$User = "test"替换成$User = "domain\test"

$User = "test"
$PWord = ConvertTo-SecureString -String "test" -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord

Enter-PSSession -ComputerName 192.168.236.128 -Credential $Credential

至此连接完成

3. 断开服务端

可选步骤,PowerShell 会"智能"识别读者的行为,自动断开连接。比如直接关闭PowerShell窗口的时候。

Exit-PSSession

4. 删除TrustedHosts

全部清空:

CleSar-Item WSMan:\localhost\Client\TrustedHosts

指定删除:
参考Github

Remove-TrustedHost.md https://github.com/BornToBeRoot/PowerShell

Remove-TrustedHost.ps1 https://github.com/BornToBeRoot/PowerShell

参考

远程管理WinRM,Enter-PSSession
PowerTip: Use PowerShell to View Trusted Hosts
PowerShell - remove wsman trustedhosts value
PowerShell: Configure WinRM and enable PSRemoting
Get-Credential

声明

本文采用知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议进行许可,发表在CSDN (生活在底层的低级码农)博客园 (低级码农),欢迎读者转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接!请读者/爬虫们尊重版权

猜你喜欢

转载自www.cnblogs.com/chasingdreams2017/p/9501877.html