[英]Powershell Hyper-V remote execution receives no response
I have a work machine(A) and a different Hyper-V server (B) with virtual machines running. 我有一台工作机(A)和另一台运行虚拟机的Hyper-V服务器(B)。
If I connect to the Hyper-V server B with RDP and run Get-VM Where-Object {$_.State -eq 'Running'}
I get a valid answer: 如果我使用RDP连接到Hyper-V服务器B并运行Get-VM Where-Object {$_.State -eq 'Running'}
我将得到一个有效的答案:
Name State CPUUsage(%) MemoryAssigned(M) Uptime Status
---- ----- ----------- ----------------- ------ ------
vm1 Running 2 2048 20:07:05 Operating normally
vm2 Running 0 1024 3.00:49:30 Operating normally
Next, I wrote a PS script to run on my work machine A: 接下来,我编写了一个PS脚本以在我的工作机A上运行:
$sess = New-PSSession $SETTING_SESSION_HOST
$commandStr = "Get-VM | Where-Object { `$_.State -eq 'Running' }"
#or a simple version: $commandStr = "Get-VM"
[console]::writeline("Executing: [{0}]", $commandStr)
$commandBlock = [scriptblock]::Create($commandStr)
$job = Invoke-Command -Session $sess -ScriptBlock $commandBlock -AsJob
Wait-Job $job
$vml = Receive-Job $job
foreach($m in $vml)
{
[console]::writeline("Executing: [{0}]", $m.Name)
}
$g = Format-List -InputObject $vml
[console]::writeline("format list: [{0}]", $g)
Here I would expect to see 2 lines containing "vm1" and "vm2" respectively. 在这里,我希望看到两行分别包含“ vm1”和“ vm2”。 But I get an empty response: 但是我得到一个空洞的回应:
Executing: [Get-VM | Where-Object { $_.State -eq 'Running' }]
format list: []
Any idea on how to get remote response from remote job? 关于如何从远程作业中获得远程响应的任何想法吗? Also, the execution time of the script is ~6 seconds (all spent in Wait-Job), while on the server it runs instantaneously. 同样,脚本的执行时间约为6秒(所有时间都花在Wait-Job中),而在服务器上则可以立即运行。
EDIT: added -AsJob parameter EDIT: fixed variables 编辑:添加-AsJob参数编辑:固定变量
may I suggest the following: 我可以提出以下建议:
$sess = New-PSSession $SETTING_SESSION_HOST
$wml = Invoke-Command -Session $sess -ScriptBlock {Get-VM | where State -eq 'Running'}
$wml.Name
PS: while writing this i noticed you set $wml
to Receive-Job $job
but then you iterate through $vml
so it might just be a typo v
for w
PS:在写这个我注意到你设置$wml
到Receive-Job $job
但你通过迭代$vml
所以它可能只是一个错字v
为w
After doing some debugging, I found the answer: when there's only 1 machine running, then the return result is an object of type Microsoft.HyperV.PowerShell.VirtualMachine
, so this code works nicely: 经过一些调试之后,我找到了答案:当只有一台计算机在运行时,返回结果是类型为Microsoft.HyperV.PowerShell.VirtualMachine
的对象,因此此代码可以很好地工作:
$vml = Invoke-Command -Session $sess -ScriptBlock {Get-VM | where State -eq 'Running'}
$vml.Name
However, if there are more than 1 machine running, then the result is Object[]
, and the code I found out working is like this: 但是,如果有不止一台机器在运行,那么结果是Object[]
,我发现工作的代码是这样的:
$vml = Invoke-Command -Session $sess -ScriptBlock {Get-VM | where State -eq 'Running'}
$len = $vml.length
if ($len)
{
[console]::writeline("Machine count: [{0}]", $len)
for ($i=0; $i -lt $len; $i++)
{
$m = $vml[$i]
[console]::writeline("machine: [{0}]", $m.Name)
}
}
else
{
[console]::writeline("Machine: [{0}]", $vml.Name)
}
Note that doing a foreach($m in $vml)
doesn't seem to work for some reason. 请注意,由于某些原因,进行foreach($m in $vml)
似乎不起作用。
The problem in my opinion is that the method is either returning an object or an array. 我认为问题在于方法要么返回对象,要么返回数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.