Tho*_*mas 6 remote-access powershell remoting
我在执行远程 powershell 脚本时遇到问题,该脚本应该更新应用程序的安装,以便在每天运行一次的任务管理器任务中进行自动化测试。
这个相当简单的脚本(详情如下)曾经成功运行了大约一年。突然它开始失败,因为无法执行远程PowerShell脚本。我不知道这是什么根本原因。本地 IT 确保他们没有更改任何内容。
(我应该注意,我可能可以用其他东西替换 powershell 脚本,但我不打算轻易放弃。除此之外,我想了解这里有什么问题)
这是一般设置:
未连接到域的 Windows Server 2008 R2 虚拟机,称为目标。u_target
分配到目标上的管理员组的本地用户。
域中的 Windows Server 2008 R2 虚拟机(我们称为域 D),称为源。u_source
分配给源上管理员组的域 D 用户。
Powershell 在两个 VM 上都有 2.0 版。
目标上的所有命令都u_target
以管理员权限执行,源上的所有命令都u_source
以管理员权限执行。在所有情况下,我都检查了 powershell 是否已以管理员身份启动。
大约一年前,我在两个 VM 上启用了 psremoting,如下所示:
在目标上,u_target 在 admin powershell 中执行
enable-psremoting -force
,
set-item wsman:\local\client\TrustedHosts -value 'source'
然后机器重新启动。
两个命令都执行没有任何错误。后来,当我遇到麻烦时,我用 * 替换了 'source' 以确保问题不是由于打字错误造成的。
在源代码中,u_source
在 admin powershell 中执行
enable-psremoting -force
。这台机器也重新启动了。
后来,当事情失败时,目标也被添加到了 TrustedHosts 中。
应该执行的脚本原则上如下所示:
$server = 'target' #(using the FQHN)
$username = 'u_target'
$password = 'u_targetpwd' #(the correct one, of course)
$pass = ConvertTo-SecureString -AsPlainText $password -Force
$Cred = New-Object System.Management.Automation.PSCredential -Argumentlist $username,$pass
$scbScriptBlock = {
# a valid script. For simplicity assume it's
Get-ChildItem C:\
}
Invoke-Command -ComputerName $server -Credential $Cred -ScriptBlock $scbScriptBlock
Run Code Online (Sandbox Code Playgroud)
这导致了大约一周后出现以下错误消息:
[target] Connecting to remote server failed with following error messages :
The connection to the specified remote host was refused. Verify that the
WS-Management service is running on the remote host and configured to
listen for requests on the correct port and HTTP URL. For more information,
see the about_Remote_Troubleshooting Help topic.
+ CategoryInfo : OpenError: (:) [], PSRemotingTransportException
+ FullyQualifiedErrorID : PSSessionStateBroken
Run Code Online (Sandbox Code Playgroud)
我试图解决这个问题或找出问题所在:
所以这里是我的问题:
当然,这个;-):你知道什么可能导致脚本突然失败吗???
我不确定的是端口和 HTTP URL WS-Management 正在侦听,我将如何检查?我执行了 'winrm enumerate winrm/config/listener' 因为事件中的一些旧消息告诉我这样做,我在这里注意到的是主机名似乎未定义。CertificateThumbprint 似乎也没有价值,但传输是 HTTP。
我想到了一些更荒谬的想法,也许不是太荒谬(?):
如果您启用 WSMan 提供程序的跟踪选项(至少在源服务器上,也可能在远程服务器上),您可能能够获得有关此类问题的更多信息。如果没有这个,就不会记录太多内容。要启用跟踪日志记录:
Import-Module PSDiagnostics
Enable-PsWsmanCombinedTrace
<run your script>
Disable-PsWsmanCombinedTrace
Run Code Online (Sandbox Code Playgroud)
查看 powershell 事件日志(应用程序和服务日志/Microsoft/Windows/PowerShell/操作)
如果 WinRM 使用 SSL,则证书应位于计算机帐户个人存储中。
归档时间: |
|
查看次数: |
2301 次 |
最近记录: |