powershell - 如何在 Visual Studio Code 中运行我的 Powershell 脚本?

标签 powershell visual-studio-code

我有一些要在 Visual Studio Code 中执行的 powershell 脚本(简单脚本)。

当我使用 F5 运行它时,VSC 变成橙色( Debug模式)几秒钟,然后恢复正常,但没有显示任何东西。

这是控制台输出:

PS C:\Users\Huberdo\Documents\WindowsPowerShell> c:\Users\Huberdo\Documents\Meine Projekte\HD-Powershell-Scripts\Mailbox\HD-Mailbox.ps1

PS C:\Users\Huberdo\Documents\Meine Projekte\HD-Powershell-Scripts\Mailbox>

脚本应该要求我使用 Write-Host 做出选择,但它没有。 这是主要脚本:

function Show-Menu() {

Write-Host "================ $Title ================"

Write-Host "1: Press '1' for this option."
Write-Host "2: Press '2' for this option."
Write-Host "3: Press '3' for this option."
Write-Host "Q: Press 'Q' to quit."


do {
    Show-Menu
    $input = Read-Host "Please make a selection"
    switch ($input) {
        '1' {
            cls
            'You chose option #1'
        } '2' {
            cls
            'You chose option #2'
        } '3' {
            cls
            'You chose option #3'
        } 'q' {
            return
        }
    }
    pause
}
    until ($input -eq 'q')
}

我已经安装并激活了 Powershell 扩展。 我在 Stackoverflow 上用谷歌搜索并搜索了可能的解决方案或类似问题,但没有找到。

我做错了什么吗?我用 x86 和 x64 PS 试过了。

最佳答案

您在 Show-Menu 函数中缺少右括号。

function Show-Menu() {

Write-Host "================ $Title ================"

Write-Host "1: Press '1' for this option."
Write-Host "2: Press '2' for this option."
Write-Host "3: Press '3' for this option."
Write-Host "Q: Press 'Q' to quit."
}

do {
    Show-Menu
    $input = Read-Host "Please make a selection"
    switch ($input) {
        '1' {
            cls
            'You chose option #1'
        } '2' {
            cls
            'You chose option #2'
        } '3' {
            cls
            'You chose option #3'
        } 'q' {
            return
        }
    }
    pause
}
    until ($input -eq 'q')

如果这不能解决您的问题 - 那么我会考虑验证您是否正确安装了 powershell 扩展,以及当您在 VS 中创建您选择的 powershell 脚本时:

文件 -> 新建项目 -> Powershell -> Powershell 脚本项目

关于powershell - 如何在 Visual Studio Code 中运行我的 Powershell 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48746982/

相关文章:

visual-studio-code - 错误 : "extension/package.json not found inside zip." when repackaging and installing VSIX

ruby - VSCode/Rubocop 提示未配置的警察

sql-server - 在PowerShell中使用Invoke-Sqlcmd时,在SQL查询中保留注释文本?

Powershell - While 语句

c# - 如何让 VSCode 使用 C# 7 而不是 C# 4?

javascript - 使用jsdoc注释,调用时如何指定泛型函数的输出类型

visual-studio-code - VSCode Vim 插件折叠 ctrl+k ctrl+<number>

powershell - 执行完成后删除 self 文件夹

c# - 有没有办法从 .cs 文件中获取函数列表?

powershell - 使用 PowerShell 的类型 -wait 和 select-string 实时监控应用程序日志的条件并执行操作(如 tail -f 或 watch)