本文介绍了如何使用 Powershell 集成控制台在 VS Code 中运行 Powershell 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个小脚本,它在 PowerShell 中运行良好(在 VS Code 之外):

$a = @{Portfolio = "CALoan";文件夹 = "S:\Data\{yymmdd}";文件名 = "LN{yymmdd}.txt"}$l = @($a)$l |ForEach-Object -Process {$p = ($_.Folder + '\' + $_.Filename).Replace("{yymmdd}", "190911")如果(测试路径 $p){[pscustomobject] @{Portfolio = $_.Portfolio;路径 = $p;CreateTime = (Get-ChildItem $p).CreationTime}} 别的 {[pscustomobject] @{Portfolio = $_.Portfolio;路径 = $p;CreateTime = "未找到"}}} |外网格视图

但是,在 VSCode 编辑器中查看脚本时,如果我只是右键单击并选择 Run Code(使用 Code Runner 扩展),我会收到大量这样的错误:

PS C:\Users\me> $l |ForEach-Object -Process {在语句块或类型定义中缺少结束}".在行:0 字符:0PS C:\Users\me> $p = ($_.Folder + '\' + $_.Filename).Replace("{yymmdd}", "190911")PS C:\Users\me> if (Test-Path $p) {在语句块或类型定义中缺少结束}".在行:0 字符:0PS C:\Users\me> [pscustomobject] @{Portfolio = $_.Portfolio;路径 = $p;CreateTime = (Get-ChildItem $p).CreationTime}投资组合路径创建时间--------- ---- ----------\ {2/28/2019 12:41:46 PM,2/20/2019 12:32:15 PM,1/24/2019 3:54:50 PM,3/15/2019 1:46:40 PM...}PS C:\用户\我> } else {在行:1 字符:1+ } 其他 {+ ~表达式或语句中出现意外标记}".在行:1 字符:8+ } 其他 {+ ~在语句块或类型定义中缺少结束}".PS C:\Users\me> [pscustomobject] @{Portfolio = $_.Portfolio;路径 = $p;CreateTime = "未找到"}投资组合路径创建时间--------- ---- ----------\ 未找到PS C:\用户\我> }在行:1 字符:1+ }+ ~表达式或语句中出现意外标记}".PS C:\用户\我> } |外网格视图在行:1 字符:1+ } |外网格视图+ ~表达式或语句中出现意外标记}".在行:1 字符:3+ } |外网格视图+ ~不允许使用空管道元素.PS C:\用户\我>

就好像集成终端一次执行一行,而不是将整个脚本发送到 PowerShell.这样做的正确方法是什么?

顺便说一句,如果我在 VS Code 中启动一个新的 PowerShell 终端,它会按预期工作(也使用 Code Runner).那么,PowerShell 集成控制台和 Code Runner 怎么样?

解决方案

我无法使用 Code Runner 扩展,但您可以通过安装 PowerShell 扩展,这对于在 Visual Studio Code 中编辑和运行 PowerShell 代码非常有用.

它允许您通过以下方式可靠且快速地运行选定的代码(因为不涉及中间脚本文件和外部 PowerShell 进程 - 见下文):

  • 或右键单击所选文本并单击运行选择.

警告:

  • 默认情况下,您通过 PowerShell 扩展运行的代码在 相同 PowerShell 会话中执行,因此后续调用可能会受到先前调用的影响;例如,如果您突出显示包含 (++$i) 的一行并重复运行它,$i 的值会不断增加.

  • 打开设置创建临时集成控制台(通过文件>首选项>设置或) 可用于更改它,以便每次调用都会创建一个新的临时会话以在其中运行.

I have this little script, which works fine in PowerShell (outside of VS Code):

$a = @{Portfolio = "CALoan"; Folder = "S:\Data\{yymmdd}"; Filename = "LN{yymmdd}.txt"}
$l = @($a)
$l | ForEach-Object -Process {
    $p = ($_.Folder + '\' + $_.Filename).Replace("{yymmdd}", "190911")
    if (Test-Path $p) {
        [pscustomobject] @{Portfolio = $_.Portfolio; Path = $p; CreateTime = (Get-ChildItem $p).CreationTime}
    } else {
        [pscustomobject] @{Portfolio = $_.Portfolio; Path = $p; CreateTime = "not found"}
    }
} | Out-GridView

However, when viewing the script in the VSCode editor, if I just right-click and choose Run Code (using the Code Runner extension), I get tons of errors like this:

PS C:\Users\me> $l | ForEach-Object -Process {
Missing closing '}' in statement block or type definition.
At line:0 char:0
PS C:\Users\me>     $p = ($_.Folder + '\' + $_.Filename).Replace("{yymmdd}", "190911")
PS C:\Users\me>     if (Test-Path $p) {
Missing closing '}' in statement block or type definition.
At line:0 char:0
PS C:\Users\me>         [pscustomobject] @{Portfolio = $_.Portfolio; Path = $p; CreateTime = (Get-ChildItem $p).CreationTime}

Portfolio Path CreateTime
--------- ---- ----------
          \    {2/28/2019 12:41:46 PM, 2/20/2019 12:32:15 PM, 1/24/2019 3:54:50 PM, 3/15/2019 1:46:40 PM...}


PS C:\Users\me>     } else {
At line:1 char:1
+ } else {
+ ~
Unexpected token '}' in expression or statement.

At line:1 char:8
+ } else {
+        ~
Missing closing '}' in statement block or type definition.
PS C:\Users\me>         [pscustomobject] @{Portfolio = $_.Portfolio; Path = $p; CreateTime = "not found"}

Portfolio Path CreateTime
--------- ---- ----------
          \    not found


PS C:\Users\me>     }
At line:1 char:1
+ }
+ ~
Unexpected token '}' in expression or statement.
PS C:\Users\me> } | Out-GridView
At line:1 char:1
+ } | Out-GridView
+ ~
Unexpected token '}' in expression or statement.

At line:1 char:3
+ } | Out-GridView
+   ~
An empty pipe element is not allowed.
PS C:\Users\me>

It's as if the integrated terminal is executing one line at a time rather than sending the whole script to PowerShell. What is the right way to do this?

BTW If I start a new PowerShell terminal within VS Code, it works as expected (also using Code Runner). So, what's up with the PowerShell Integrated Console and Code Runner?

解决方案

I can't speak to the Code Runner extension, but you can bypass the problem by installing the PowerShell extension, which is invaluable for both editing and running PowerShell code in Visual Studio Code.

It allows you to run selected code reliably and faster (because no intermediate script file and external PowerShell process is involved - see below) by:

  • either pressing
  • or right-clicking the selected text and clicking Run Selection.

Caveat:

  • By default, code you run via the PowerShell extension executes in the same PowerShell session, so that subsequent invocations can be affected by previous ones; e.g., if you highlight a line containing (++$i) and run it repeatedly, the value of $i keeps incrementing.

  • Turning on setting Create Temporary Integrated Console (via File > Preferences > Settings or ) can be used to change that, so that every invocation creates a new, temporary session to run in.

这篇关于如何使用 Powershell 集成控制台在 VS Code 中运行 Powershell 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 19:46