Powershell远程机多跳问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baidu_35407267/article/details/79648010

何为多跳问题,即A机器远程到B机器,然后去C机器copy或者获取文件。
这里写图片描述
对于Powershell新手,多跳问题(multi-hop)是一个难搞的事情,明明Winrm到远程机没问题,在远程机上直接操作也是能行,为啥两跳以后,就会报access is deny等一系列问题呢?最最根本的原因是从A机器远程到B机器时会丢掉credential。国内的博客上好多解决方案,绝不奏效,都没有提到最最关键的一步:组策略的修改。
第一步:Trustedhost
因为你要连远程机,所以远程机必须是trusted。很简单的一句命令搞定。

Set-Item WSMan:\localhost\Client\TrustedHosts -value * -Force

如果不加,可能会报如下这样子的错:
这里写图片描述
第二步:CredSSP
因为远程到B机器时丢掉了credential,所以首先想到的就是Enable-WSManCredSSP,文档如是说:

The Enable-WSManCredSSP cmdlet enables Credential Security Support Provider (CredSSP) authentication on a client or on a server computer. When CredSSP authentication is used, the user credentials are passed to a remote computer to be authenticated. This type of authentication is designed for commands that create a remote session from another remote session. For example, if you want to run a background job on a remote computer, use this kind of authentication.
粗略翻译如下:
Enable-WSManCredSSP能开启CredSSP授权电脑为一个客户端或者服务端。CredSSP授权可以允许把用户凭据代到被授权的远程机。这类授权是为从一个远程会话向另一个远程会话建立链接而设计的。例如,当你当在远程机器执行一个后台执行任务时,用这种授权
这个时候就需要理解,哪个是server哪个是client,凭据是从client机器带到server机器,所以不要授权反了,那样肯定没效果,还是上边例子,A为client,B为server,凭据从A机器会代到B机器。没毛病。废话少说,上代码!

##当前A机器为cosdesdev1,我们控制B机器vmaosupse2去C机器cosmoxydev8的UNC路径读取文件夹列表
Enable-WSManCredSSP -Role "Client" -DelegateComputer * -force | out-null
$secPassword = ConvertTo-SecureString "guguji5" -AsPlainText -Force
$cred = New-Object system.Management.Automation.PSCredential("advent\axyssu", $secPassword)

invoke-Command -ComputerName vmaosupse2 -Credential $cred -ScriptBlock {
    Enable-WSManCredSSP -Role "Server" -force | out-null
}
invoke-Command -ComputerName vmaosupse2 -Credential $cred -Authentication Credssp -ScriptBlock{
    $path = "\\cosmoxydev8\c$\Moxy\Client"
    get-childitem -path $path
}

得到的报错大概如下图
这里写图片描述
从我认识的仅有的几个英语单词中,可以挑出“组策略”不允许代理用户的凭据到目标机器。接下来,就到了下一步,也是成功的关键。

第三步:组策略修改
我参考了好多文章都没成功,因为都只做了第一步,其实组策略的修改才是关键。首先在运行gpedit.msc打开组策略的面板,Computer Configuration(计算机配置)->Administrative Templates(管理员模版)->System(系统)->Credentials Delegation(凭据代理),在双击“允许仅服务器代理NTLM凭据”(蓝色的那一项),设置为“启用”,在增加servers list里加入“*”。
这里写图片描述
这里写图片描述
然后去windows servers里重启winrm servers,再次执行第一步中代码即可成功。

感谢田总的指导,田总金句“跟你讲多少遍也不如自己试一遍“

猜你喜欢

转载自blog.csdn.net/baidu_35407267/article/details/79648010