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

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

Related

powershell : pipe get-content to ps1 file with parameters

I'm trying to write a script which uses the powershell cmdlet get-content tail and inserts the new lines into the sql server table. i can't get the syntax to pipe the tail to the sqlinsert.ps1 file that handles the table insert.
i'm looking for help on how to pipe "get-content tail" to a sqlinsert.ps1 file to do a sql database insert statement using the following :
$startTime = get-date
Write-Host "\\iisserver\logs\Logs-$("{0:yyyyMMdd}" -f (get-date)).txt"
get-content "\\iisserver\logs\Logs-$("{0:yyyyMMdd}" -f (get-date)).txt" -tail 1 -wait | & "sqlinsert.ps1" -stmp $("{0:yyyy-MM-dd hh:mm:ss.fff}" -f (get-date)) -method "Error" -msg $_
# % { "$_ read at $(Get-Date -Format "hh:mm:ss")" }
in the sqlinsert.ps1 :
param ([string]$stmp, [string]$method, [string]$msg )
$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = "server='$serverName';database='$databaseName';User ID = $uid; Password = $pwd;"
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
$sql = "insert into [tbl_iiserrors] (errstamp, method, msg) values (#stmp , #method, #msg) "
.
.
.
error i get:
& : The term 'sqlinsert.ps1' is not recognized as the name of a
cmdlet, function, script file, or operable program. Check the spelling
of the name, or if a path was included, verify that the path is
correct and try again. At C:\Temp\ob\iislog\tst_tail.ps1:3 char:95
... Mdd}" -f (get-date)).txt" -tail 1 -wait | & "sqlinsert.ps1" -stmp $ ...
~~~~~~~~~~~~~~~
CategoryInfo : ObjectNotFound: (sqlinsert.ps1:String) [], CommandNotFoundException
FullyQualifiedErrorId : CommandNotFoundException
Suggestion [3,General]: The command sqlinsert.ps1 was not found, but
does exist in the current location. Windows PowerShell does not load
commands from the current location by default. If you trust this
command, instead type: ".\sqlinsert.ps1". See "get-help
about_Command_Precedence" for more details.
The sqlinsert.ps1 works when i run it from powershell command :
PS c:\temp> .\sqlinsert -stmp 2020-11-20 00:00:00 -method 'eek' -msg 'uh hello'
In order to bind pipeline input to a parameter, you need to decorate it with a [Parameter] attribute and specify that it accepts pipeline input, like this:
param (
[string]$stmp,
[string]$method,
[Parameter(ValueFromPipeline = $true)]
[string]$msg
)
See the about_Functions_Advanced_Parameters help file for more details about how to modify the behavior of parameters
By design, for security reasons, PowerShell requires you to signal the intent to execute a script located in the current directory explicitly, using a path - .\sqlinsert.ps1 - rather than a mere file name - sqlinsert.ps1; that is what the suggestion following the error message is trying to tell you.
Note that you only need &, the call operator, if the script path is quoted and/or contains variable references - and .\sqlinsert.ps1 doesn't require quoting.
You can only use the automatic $_ variable, which represents the current input object from the pipeline inside a script block ({ ... }), such as one passed to the ForEach-Object cmdlet, which invokes that block for each object received via the pipeline.
Re the content of your script: Inside expandable strings ("..."), you cannot use # to refer to variables to be expanded (interpolated); use regular, $-prefixed variable references or $(...), the subexpression operator to embed expressions; also, it looks like you're inserting string values into the SQL table, so you'll have to enclose the expanded variable values in embedded '...'
$startTime = get-date
Get-Content "\\iisserver\logs\Logs-$("{0:yyyyMMdd}" -f (get-date)).txt" -Tail 1 -Wait |
ForEach-Object {
.\sqlinsert.ps1 -stmp ("{0:yyyy-MM-dd hh:mm:ss.fff}" -f (get-date)) -method "Error" -msg $_
}
The alternative to using a ForEach-Object call is to modify your script to directly receive its -msg argument from the pipeline, as shown in Mathias' answer, in which case you must omit the -msg $_ argument from your script call:
Get-Content ... |
.\sqlinsert.ps1 -stmp ("{0:yyyy-MM-dd hh:mm:ss.fff}" -f (get-date)) -method "Error"

Strange behavior of PSConsoleReadLine

Trying to seemingly generate text in Powershell console (result predetermined), did not like cls + Write-Host because of constant blinking, tried to use PSConsoleReadLine. Code below works in some situations, while it does not in others.
Code works when:
directly copy pasted into Powershell window
when stored in a script file and the file is called from Powershell window
Code does not work when:
launched in ISE (known issue)
right click on the script file / run with powershell
Tried to get around the error with Add-Type, but it ends with IOException and null reference instead. Is present in code below, but commented.
Tried to write a script in a way that relaunches itself in a new Powershell process to handle it, did not help.
Code itself (probably could reproduce the issue with a single line of code, but the guide says one line of a code is not enough):
function tryOne($oldExpression, $targetExpression, $currentPosition, $src)
{
While($targetExpression[$currentPosition] -cne $triedCharacter)
{
[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()#here problem
$triedCharacter = Get-Random -InputObject $src
$expression = $oldExpression + $triedCharacter
[Microsoft.PowerShell.PSConsoleReadLine]::Insert($expression)#here problem
Start-sleep -Milliseconds 50
}
Return $expression
}
cd $PSScriptRoot
#Add-Type -path "C:\Program Files\WindowsPowerShell\Modules\PSReadline\1.2\Microsoft.PowerShell.PSReadLine.dll"
#above helps with not found error, with it uncommented the errors at RevertLine and Insert are IOException and null reference
$targetExpression = "Not ideal"
$oldExpression = ""
$currentPosition = 0
#region AllowedCharacters
$src = New-Object System.Collections.ArrayList
for($i=65;$i-le 90;$i++)
{
$src.Add([char]$i) | Out-Null
}
for($i=97;$i-le 122;$i++)
{
$src.Add([char]$i) | Out-Null
}
for($i=0;$i-le 9;$i++)
{
$src.Add($i) | Out-Null
}
$src.Add(" ") | Out-Null #allowed characters in the target sentence - A-Z, a-z, 0-9, space
#endregion
While($currentPosition -lt ($targetExpression.length))
{
$oldExpression = tryOne $oldExpression $targetExpression $currentPosition $src
$currentPosition++;
}
I'd like to be able to launch the script via right click / run with Powershell.

Exit and Continue script (for example to take PATH environment change effect) [duplicate]

If I have an instance of PowerShell ISE running and I install something that modifies the PATH or I modify it in any way outside of PowerShell then I need to restart PowerShell for it to see the updated PATH variable.
Is there a way to reload the path from within PowerShell without restarting it?
Just to bring Rob's comment to light:
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
Try getting the machine path and assigning it to the session's path.
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine")
Easiest way, use Chocolatey (freeware). It works for both CMD and PowerShell. Then you will be able to reload PATH (with variable expansion) with a simple command:
refreshenv
Installation from cmd (requires administrator rights):
#"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
Example usage:
> SET JAVA_HOME=c:/java/jdk6
> SET PATH=%JAVA_HOME%/bin
> ECHO %PATH%
c:/java/jdk6/bin
> SET JAVA_HOME=c:/java/jdk8
> refreshenv
Refreshing environment variables from registry for cmd.exe. Please wait...Finished..
> echo %PATH%
c:/java/jdk8/bin
Based on mpen's answer, here is a PowerShell function:
function refresh-path {
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") +
";" +
[System.Environment]::GetEnvironmentVariable("Path","User")
}
Then just call refresh-path.
Just to add to other answers, you can make sure you don't add superfluous joins by filtering in case the user has an empty path.
$env:Path=(
[System.Environment]::GetEnvironmentVariable("Path","Machine"),
[System.Environment]::GetEnvironmentVariable("Path","User")
) -match '.' -join ';'
Or, more usefully, if you're running a script that adds to a different or multiple environment variables, use a function to reset them all
function resetEnv {
Set-Item `
-Path (('Env:', $args[0]) -join '') `
-Value ((
[System.Environment]::GetEnvironmentVariable($args[0], "Machine"),
[System.Environment]::GetEnvironmentVariable($args[0], "User")
) -match '.' -join ';')
}
resetEnv Path
resetEnv AppPath
If your path contains environment variables that weren't defined at the start of the session, you'll want to expand those too:
$env:Path = [System.Environment]::ExpandEnvironmentVariables([System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User"))
For me this was useful after installing NVM which defines and adds %NVM_HOME% to the path.
To take this to its logical conclusion you could use this recursive function to expand instead:
function Expand-EnvironmentVariablesRecursively($unexpanded) {
$previous = ''
$expanded = $unexpanded
while($previous -ne $expanded) {
$previous = $expanded
$expanded = [System.Environment]::ExpandEnvironmentVariables($previous)
}
return $expanded
}
And then use:
$env:Path = Expand-EnvironmentVariablesRecursively([System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User"))
I've opened an issue to add this solution into refreshenv from Chocolatey.

fuser equivalent in Powershell?

I want to delete a directory, but a process is using it.
mv : Access to the path 'C:\Users\mike\Documents\myapp\node_modules\' is denied.
Explorer mentions the dir is in use. Is there a Windows Powershell equivalent of fuser?
Note this is a powershell question. I don't want to launch a GUI app.
Try this one:
$lockedFolder="C:\Windows\System32"
Get-Process | %{$processVar = $_;$_.Modules | %{if($_.FileName -like "$lockedFolder*"){$processVar.Name + " PID:" + $processVar.id}}}
This looks for every process that's running in the folder. (or in subdirectories)
With this script, you'll get some more informations:
$lockedFolder="C:\Windows\System32"
Get-Process | %{$processVar = $_;$_.Modules | %{if($_.FileName -like "$lockedFolder*"){$processVar.Name + " PID:" + $processVar.id + " FullName: " + $_.FileName }}}
I think there is no equivalent to fuser, but there is a tool called handle.exe that has to be installed first.
PowerShell script to check an application that's locking a file?
Here's a modified version of #Eldo.Ob's excellent answer that handles relative files.
function fuser($relativeFile){
$file = Resolve-Path $relativeFile
foreach ( $Process in (Get-Process)) {
foreach ( $Module in $Process.Modules) {
if ( $Module.FileName -like "$file*" ) {
$Process | select id, path
}
}
}
}
In use:
> fuser .\node_modules\
Id Path
-- ----
2660 C:\Program Files\nodejs\node.exe

Reload the path in PowerShell

If I have an instance of PowerShell ISE running and I install something that modifies the PATH or I modify it in any way outside of PowerShell then I need to restart PowerShell for it to see the updated PATH variable.
Is there a way to reload the path from within PowerShell without restarting it?
Just to bring Rob's comment to light:
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
Try getting the machine path and assigning it to the session's path.
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine")
Easiest way, use Chocolatey (freeware). It works for both CMD and PowerShell. Then you will be able to reload PATH (with variable expansion) with a simple command:
refreshenv
Installation from cmd (requires administrator rights):
#"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
Example usage:
> SET JAVA_HOME=c:/java/jdk6
> SET PATH=%JAVA_HOME%/bin
> ECHO %PATH%
c:/java/jdk6/bin
> SET JAVA_HOME=c:/java/jdk8
> refreshenv
Refreshing environment variables from registry for cmd.exe. Please wait...Finished..
> echo %PATH%
c:/java/jdk8/bin
Based on mpen's answer, here is a PowerShell function:
function refresh-path {
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") +
";" +
[System.Environment]::GetEnvironmentVariable("Path","User")
}
Then just call refresh-path.
Just to add to other answers, you can make sure you don't add superfluous joins by filtering in case the user has an empty path.
$env:Path=(
[System.Environment]::GetEnvironmentVariable("Path","Machine"),
[System.Environment]::GetEnvironmentVariable("Path","User")
) -match '.' -join ';'
Or, more usefully, if you're running a script that adds to a different or multiple environment variables, use a function to reset them all
function resetEnv {
Set-Item `
-Path (('Env:', $args[0]) -join '') `
-Value ((
[System.Environment]::GetEnvironmentVariable($args[0], "Machine"),
[System.Environment]::GetEnvironmentVariable($args[0], "User")
) -match '.' -join ';')
}
resetEnv Path
resetEnv AppPath
If your path contains environment variables that weren't defined at the start of the session, you'll want to expand those too:
$env:Path = [System.Environment]::ExpandEnvironmentVariables([System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User"))
For me this was useful after installing NVM which defines and adds %NVM_HOME% to the path.
To take this to its logical conclusion you could use this recursive function to expand instead:
function Expand-EnvironmentVariablesRecursively($unexpanded) {
$previous = ''
$expanded = $unexpanded
while($previous -ne $expanded) {
$previous = $expanded
$expanded = [System.Environment]::ExpandEnvironmentVariables($previous)
}
return $expanded
}
And then use:
$env:Path = Expand-EnvironmentVariablesRecursively([System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User"))
I've opened an issue to add this solution into refreshenv from Chocolatey.