【问题标题】:How can I run my Powershell Script inside of Visual Studio Code?如何在 Visual Studio Code 中运行我的 Powershell 脚本?
【发布时间】:2018-02-12 12:52:54
【问题描述】:

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

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

这是控制台输出:

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 尝试过。

【问题讨论】:

    标签: powershell visual-studio-code


    【解决方案1】:

    您在 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 脚本项目

    【讨论】:

    • 就是这样,因为代码只是在Show-Menu函数中调用ShowMenu,所以调用Show-Menu时不能停止循环。
    • 您好,感谢您的回答。我修正了你的建议,并在预装的 ISE 中尝试了同样的事情,每个人都在那里工作。使用 Visual Studio Code,它不起作用。我也没有“文件-> 新项目”菜单。我有 Visual Studio Code 而不是 Visual Studio。
    • @GeraltDieSocke 啊,好的。抱歉没有发现你有 Visual Studio 代码。所以我无法证明这一点,因为我有 Visual Studio 2013。嗯,我会尝试上面发布的代码。我可以从 Visual Studio 运行它,并提示我输入输入值。在您的原始代码中,您将所有内容都包含在函数中,因此该函数永远不会被调用。这就是脚本可能运行并退出的原因。
    • @GeraltDieSocke 嗯,您是否尝试过在 VSCode 菜单中重新加载 Powershell 扩展?(此处的屏幕截图 #2:mikefrobbins.com/2017/08/24/…
    • @GeraltDieSocke,一旦更正,我在 Visual Studio 代码 (1.19.3) 中运行代码并且它可以工作(PowerShell 语言支持 1.5.1)。
    猜你喜欢
    • 2020-01-02
    • 1970-01-01
    • 2018-02-26
    • 1970-01-01
    • 2018-04-11
    • 2022-12-16
    • 2021-02-01
    • 2017-08-08
    • 2016-01-01
    相关资源
    最近更新 更多