如果Powershell中的foreach循环内的条件不匹配

如果Powershell中的foreach循环内的条件不匹配,powershell,Powershell,我刚刚完成10961的Powershell培训,并尝试创建脚本 目前,我正在编写一个非常简单的脚本,该脚本连接到vCenter服务器,并在关机时启动VM # List of VMs $VMs = "VM001","VM002","VM003" # Retrieve VM state $VM = Get-VM -Name $VMs foreach ($VM in $VMs) { if ($VM.PowerState -ne "PoweredOn") { Start-VM

我刚刚完成10961的Powershell培训,并尝试创建脚本

目前,我正在编写一个非常简单的脚本,该脚本连接到vCenter服务器,并在关机时启动VM

# List of VMs
$VMs = "VM001","VM002","VM003"

# Retrieve VM state
$VM = Get-VM -Name $VMs

foreach ($VM in $VMs)
{
    if ($VM.PowerState -ne "PoweredOn") {
        Start-VM -VM $vm -Confirm:$false
        Write-Host "$vm is starting"
    }        
    else {
        Write-Host "$vm is already Powered On" -ForegroundColor Green
    }
}
这些虚拟机中至少有一个已关闭电源,但脚本报告所有虚拟机都已打开电源。
我在这里做错了什么?

尝试检查
Get VM
返回的对象上的值:
Get VM-Name$VMs |选择Name,PowerState
。感谢您的提示。PowerState值显示为:PoweredOn,PoweredOff。但是,当我尝试打开已打开电源的VM时,控制台中显示的错误是无法在当前状态下执行“已打开电源”。我在脚本中添加了空间,现在可以正常工作。