用户未登录时不执行Invoke-Command

问题描述 投票:0回答:4

我的目标是重置虚拟机并在其上运行一些安装程序。这就是我所做的:

Write-Host "Revert VM to snapshot"
Get-VM -Name $vmName | Restore-VMSnapshot -Name "7Zip" -Confirm:$false

Write-Host "Start VM"
Get-VM -Name $vmName | Start-VM

Write-Host "Wait for VM to be ready"
Wait-VM $vmName
Write-Host "Wait a little bit more..."
Start-Sleep -Seconds 20

Write-Host "Create credetials"
$computer = "W7P-MY-COMPUT"
$username = $computer + "\localadmin"
$password = cat C:\build\mysecurestring.txt | convertto-securestring
$cred = new-object -typename System.Management.Automation.PSCredential `
     -argumentlist $username, $password

Write-Host "Invoke remote script"
Invoke-Command -ComputerName $computer -FilePath c:\build\GetInstallers.ps1 -Credential $cred

引用的脚本下载一些 msi 文件并安装它们。如果我运行所有 VM 命令,手动登录计算机,然后执行 Invoke-Command,那么所有这些都可以正常工作。

但是同时运行会给我这个输出:

Revert VM to snapshot
Start VM
Wait for VM to be ready
Wait a little bit more...
Create credetials
Invoke remote script
[W7P-SAMOS-WEB-I] Connecting to remote server W7P-SAMOS-WEB-I failed with the following error message : WinRM cannot complete the operation. Verify that the 
specified computer name is valid, that the computer is accessible over the network, and that a firewall exception for the WinRM service is enabled and allows 
access from this computer. By default, the WinRM firewall exception for public profiles limits access to remote computers within the same local subnet. For 
more information, see the about_Remote_Troubleshooting Help topic.
+ CategoryInfo          : OpenError: (W7P-SAMOS-WEB-I:String) [], PSRemotingTransportException
+ FullyQualifiedErrorId : WinRMOperationTimeout,PSSessionStateBroken
powershell powershell-remoting
4个回答
1
投票

检查服务状态。确保其正常运行

get-service winrm

要启用 PS 远程处理,请使用:

Enable-PSRemoting –force

将系统添加到受信任的主机列表:

winrm s winrm/config/client '@{TrustedHosts="RemoteComputer"}'

验证:

winrm quickconfig

完成后,从源到目标运行调用命令并查看结果。如果出现错误,请发布。

除此之外,请您仔细检查防火墙并确保其已禁用。

或者这样做:

Enable-PSRemoting -SkipNetworkProfileCheck -Force
Set-NetFirewallRule -Name "WINRM-HTTP-In-TCP-PUBLIC" -RemoteAddress Any

0
投票

根据虚拟机的操作系统版本,可能未配置 WinRM,在 Windows Server 2012 及更高版本上应该没问题,但仍然值得检查它是否已启用。然后,您可以在此处查看讨论:Invoke-Command failed: WinRM无法完成操作,这涉及Ranadip在评论中提到的要点。


0
投票

当我尝试执行 Invoke-Command 时,winrm 服务尚未运行。使用“开始-睡眠”命令等待更长时间(大约 3-4 分钟)可以解决该问题。


0
投票

我在使用静默安装任务在远程计算机上执行 Invoke-Command 时遇到问题。我已检查 WinRM 服务和端口,但仅当任何用户连接或登录远程计算机时,该命令才能正常工作。我还检查了 ScriptBlock 本地的所有内容。不知道我还应该检查什么?有什么建议么?谢谢

© www.soinside.com 2019 - 2024. All rights reserved.