简体   繁体   中英

How to run Powershell script in VS Code using Powershell Integrated Console

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 F8
  • 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 ; eg, 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 Ctrl+, ) can be used to change that, so that every invocation creates a new, temporary session to run in .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM