Rej Rej - 1 year ago 78
PowerShell Question

PS - Parameter's default value in Visual Studio 2015

I'm making a PowerShell script, and I've found something strange happening with my Visual Studio 2015. When I'm trying to assign value to sciript's parameter, it becomes blank. However in clean powershell console, everything is ok. Here is my code, and results for VS and clean PS:

[String]$parameter = $PSScriptRoot

$var = "\file.txt"
echo "PSScriptRoot: $PSScriptRoot"
echo "Parameter: $parameter"

$var1 = $PSScriptRoot + $var
$var2 = $parameter + "\file.txt"
$var3 = $parameter + $var
-Join ($parameter, $var)

echo "var1: $var1"
echo $var1.GetType()
echo "var2: $var2"
echo $var2.GetType()
echo "var3: $var3"
echo $var3.GetType()
echo "var: $var"
echo $var.GetType()
echo "parameter: $parameter"

Results VS2015:

PSScriptRoot: C:\Users\Ja\Documents\Visual Studio 2015\Projects\teststemp\teststemp
var1: C:\Users\Ja\Documents\Visual Studio 2015\Projects\teststemp\teststemp\file.txt

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
var2: \file.txt
True True String System.Object
var3: \file.txt
True True String System.Object
var: \file.txt
True True String System.Object

Results clean PowerShell:

PSScriptRoot: C:\users\ja\documents\Visual Studio 2015\Projects\teststemp\teststemp
Parameter: C:\users\ja\documents\Visual Studio 2015\Projects\teststemp\teststemp
C:\users\ja\documents\Visual Studio 2015\Projects\teststemp\teststemp\file.txt
var1: C:\users\ja\documents\Visual Studio 2015\Projects\teststemp\teststemp\file.txt
var2: C:\users\ja\documents\Visual Studio 2015\Projects\teststemp\teststemp\file.txt
var3: C:\users\ja\documents\Visual Studio 2015\Projects\teststemp\teststemp\file.txt
var: \file.txt
parameter: C:\users\ja\documents\Visual Studio 2015\Projects\teststemp\teststemp

Does anyone knows, why it's happening? Switching to Release mode didn't helped.

Rej Rej
Answer Source

So I found the answer by myself.

The main problem is, that any IDE runs code with some kind of "virtual container", made specially for it, and because of that, some built-in parameters cannot be initialised with correct values, because those values doesn't exist.

In this case, when I try to get $PSScriptRoot variable value, it returns nothing, because the script is running from non-existing directory. And in clean PowerShell console, execution looks like:

& ("C:\Scripts\script.ps1")

So the script is not running from this "virtual container", but from specified directory, and $PSScriptRoot now have value.

Because of this, there is problem with debugging scripts like this one. As far as I know, you cannot add block of if statement before parameters block, to check, if $PSScriptRoot has value, like that:

if ($PSScriptRoot -eq $null) {
    $PSScriptRoot = "C:\something"

     [String]$cfgFile = $PSScriptRoot + "\cfg.txt"

This will return a lot of exceptions and errors.

I've found solution for this:

         [String]$cfgFile = $PSScriptRoot + "\cfg.txt"

So we can just give value in parameters, and in release version delete additional unneeded parameter.