Windows powerShell远程连接服务器,需要设置以下3个步骤:
1、在服务器端添加信任的客户端地址(客户端的IP地址)
通过winrm命令配置:
这里host1,2,3可以填你所在的客户端机器的ip或者主机名
winrm set winrm/config/client @{TrustedHosts="host1, host2, host3"}
2、在客户端添加信任主机
在客户端以管理员打开Windows powerShell;
(1)、通过“enable-psremoting”命令将winrm更新为接收请求
(2)、添加信任主机:
3、连接远程服务器:#1.切到远程管理的client目录下
cd WSMan::localhost\client
#2.将192.168.1.135添加为可信主机
Set-Item ./TrustedHosts 192.168.1.135
通过以下命令连接
$uname="admin" #administrator为用户名
$pwd=ConvertTo-SecureString "nmczqk" -AsPlainText -Force; #nmczqk为密码
$cred=New-Object System.Management.Automation.PSCredential($uname,$pwd); #创建自动认证对象
$pcname="10.28.17.37"Enter-PSSession -ComputerName $pcname -Credential $cred #登录
4、注意
Powershell Remoting依赖于WinRM (Windows Remote Management)在远程机器上运行操作。默认情况下,WinRM为每一个Powershell远程连接分配了最大(MaxMemoryPerShellMB=1024)1G的内存空间(早期的版本号仅仅有150M),用于运行远程操作。
但远程操作所需的运行内存空间 > 1G时,就会出现了内存不足的问题,不同的操作可能表现会有所不同,如:有的会抛出OutOfMemoryException等。针对这个问题。解决的办法就是添加MaxMemoryPerShellMB,然后重新启动WinRM服务:
$maxMemoryPerShellVM = 3072
Set-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB $maxMemoryPerShellVM
Set-Item WSMan:\localhost\Plugin\Microsoft.PowerShell\Quotas\MaxMemoryPerShellMB $maxMemoryPerShellVM
Write-Output "List MaxMemoryPerShellMB configuration"
# Restart WinRM service to make the change take effect
Restart-Service winrm