powershell cmdlet Get-Vm未在正确的时间显示

时间:2017-12-20 14:20:47

标签: powershell virtual-machine

这是最新形式的脚本,在代码的第6行,我试图让它显示主机上虚拟的当前状态。 当我运行代码时,它直到最后才会显示。

它可以在其他任何地方正常运行,并且可以像我希望的那样对其进行操作。但是Get-VM直到脚本退出之前才会运行。

是否有某种强制命令使命令执行我不知道的?我知道这可能只是我看不到的一些愚蠢的错误。

如果您需要进一步说明,请与我们联系。

#new-alias -Name rh -Value read-host
Get-VM
$AA = rh "Would you like to preform an action?[y/n]"
While($AA -eq "y"){
$A = rh "What would you like to do? `n save?[sa] `n start?[st] `n stop?[x] 
`n restart?[re]`n "

# blank-vm -name blank
If($A -eq "sa"){
$s = "Save"
$B = rh "Which VM would you like to $s ?[a for all]"
if($B -eq "a"){
Get-VM | Save-VM
}
else{save-vm -name "$B"}
}
If($A -eq "st"){
$s = "Start"
$B = rh "Which VM would you like to $s ?[a for all]"
if($B -eq "a"){
Get-VM | Start-VM
}
else{Start-vm -name "$B"}
}
If($A -eq "x"){
$s = "Stop"
$B = rh "Which VM would you like to $s ?[a for all]"
if($B -eq "a"){
Get-VM | stop-VM
}
else{Stop-vm -name "$B"}
}
If($A -eq "re"){
$s = "Restart"
$B = rh "Which VM would you like to $s ?[a for all]"
if($B -eq "a"){
Get-VM | restart-VM
}
else{restart-vm -name "$B"}
}
$AA = rh "Would you like to preform another action?[y/n]"
}

1 个答案:

答案 0 :(得分:0)

这似乎是Hyper-V cmdlet和读/写主机cmdlet的问题,例如:克。

Get-VM
Write-Host "Test"

将导致首先将字符串Test写入主机,然后输出Get-VM的结果。 Get-VMHost,Get-VMHardDisk和Get-VMSwitch具有相同的问题。 "标准"像Get-Service和Get-Process这样的cmdlet正常工作,例如:克。

Get-Service
Write-Host "Test"

将首先输出服务列表,然后输出字符串Test。使用Write-Output而不是Write-Host也会导致正常的行为。

为避免这种情况,您可以将Get-VM输出转换为字符串,e。 G:

Get-VM | Out-String
Write-Host "Test"

其他cmdlet可能会出现同样的问题

相关问题