You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell循环中Get-VM无输出,退出程序后才显示的问题

问题排查与修复方案

核心问题1:循环终止条件逻辑错误

你的do-while循环条件$choice -ne 'Q' -or $choice -ne 'q'永远为$true——任何输入都不可能同时等于Q和q,导致循环无限执行。PowerShell的管道输出(比如Get-VM | Select-Object Name)会被缓冲,直到循环结束才会一次性输出,这就是选择选项5后看不到结果、退出程序才显示的根本原因。

修复:将-or替换为-and,只有当输入既不是Q也不是q时才继续循环:

} while($choice -ne 'Q' -and $choice -ne 'q')

核心问题2:管道输出的即时显示问题

Get-VM | Select-Object -Property Name返回的是对象,在循环场景下PowerShell可能延迟输出。可以通过管道传递给Out-Host强制即时显示,或者用格式化命令明确输出形式:
修改listVMs函数:

function listVMs() {
    Write-Host "Here is a list of your VMs:`n"
    # 强制即时输出对象内容
    Get-VM | Select-Object -Property Name | Out-Host
    # 也可以用格式化命令确保显示效果
    # Get-VM | Select-Object Name | Format-Table -AutoSize
    Start-Sleep -Seconds 4
}

额外问题:函数名拼写错误

switch语句中选项6调用的是settingVM,但你定义的函数名是settingsVM(多了一个s),会导致选择选项6时报错。修复switch分支中的函数名:

'6' {
    clear
    settingsVM
    break
} 

完整修复后的脚本

整合所有修改后的完整代码如下:

function createVM {
    New-VM -Name TestVM -MemoryStartupBytes 4GB -BootDevice VHD -VHDPath .\VMs\Test.vhdx -Path .\VMData -Generation 2
    Write-Host "Creating VM..."
    Start-Sleep -Seconds 4
}

function deleteVM {
    Remove-VM -Name "TestVM" -Force
    Write-Host "Removing VM..."
    Start-Sleep -Seconds 4
}

function startVM() {
    Start-VM -Name TestVM
    Write-Host "Starting VM..."
    Start-Sleep -Seconds 4
}

function stopVM() {
    Stop-VM -Name TestVM -Force
    Write-Host "Stopping VM..."
    Start-Sleep -Seconds 4
}

function listVMs() {
    Write-Host "Here is a list of your VMs:`n"
    Get-VM | Select-Object -Property Name | Out-Host
    Start-Sleep -Seconds 4
}

function settingsVM() {
    Write-Host "Here are some VM settings:`n"
    Start-Sleep -Seconds 5
}

do {
    Write-Host "`nHyper-V VM Manager`n"
    Write-Host "Enter Selection:`n"
    Write-Host "`t1 - Create a VM"
    Write-Host "`t2 - Delete a VM"
    Write-Host "`t3 - Start a VM"
    Write-Host "`t4 - Stop a VM"
    Write-Host "`t5 - List available VMs"
    Write-Host "`t6 - List Settings of a VM"

    $choice = Read-Host -Prompt "`nPlease enter a number (1-6) or 'Q/q' to quit the program"

    switch($choice) {
        '1' {
            clear
            createVM
            break
        }
        '2' {
            clear
            deleteVM
            break
        }
        '3' {
            clear
            startVM
            break
        }
        '4' {
            clear
            stopVM
            break
        }
        '5' {
            clear
            listVMs
            break
        }
        '6' {
            clear
            settingsVM
            break
        } 
    }
} while($choice -ne 'Q' -and $choice -ne 'q')

Write-Host "Quitting program: returning to shell.`n"
Write-Host "Have a wonderful day!`n"
Start-Sleep -Seconds 4 
exit

内容的提问来源于stack exchange,提问作者ispytj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 03:57:45