如何在Visual Studio代码中运行Powershell脚本?
我有一些powershell脚本(简单的),我想在VisualStudio代码中执行 当我用F5运行它时,VSC会变成橙色(调试模式)几秒钟,然后返回到正常状态,没有显示任何内容 这是控制台输出:如何在Visual Studio代码中运行Powershell脚本?,powershell,visual-studio-code,Powershell,Visual Studio Code,我有一些powershell脚本(简单的),我想在VisualStudio代码中执行 当我用F5运行它时,VSC会变成橙色(调试模式)几秒钟,然后返回到正常状态,没有显示任何内容 这是控制台输出: function Show-Menu() { Write-Host "================ $Title ================" Write-Host "1: Press '1' for this option." Write-Host "2: Press '2' for
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')
}
PS C:\Users\Huberdo\Documents\WindowsPowerShell>C:\Users\Huberdo\Documents\Meine Projekte\HD Powershell脚本\Mailbox\HD-Mailbox.ps1
PS C:\Users\Huberdo\Documents\Meine Projekte\HD Powershell脚本\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试过了。显示菜单功能中缺少一个右括号
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中尝试了同样的方法,每个人都在那里工作。对于VisualStudio代码,它不起作用。我也没有“文件->新项目”菜单。我有Visual Studio代码,而不是Visual Studio。@GeraltDieSocke啊,好的。抱歉,没有发现您有visual studio代码。所以我不能证明这一点,因为我有Visual Studio 2013。嗯,我会试试上面贴的代码。我可以在VisualStudio中运行它,并得到输入值的提示。在原始代码中,所有内容都封装在函数中,因此函数永远不会被调用。这就是脚本可能会运行并退出的原因。@GeraltDieSocke嗯,您是否尝试在VSCode菜单中重新加载Powershell扩展?(屏幕截图#2,此处:)@GeraltDieSocke,更正后,我在Visual studio代码(1.19.3)中运行代码,它可以正常工作(Powershell语言支持1.5.1)。