b3bel
b3bel

Reputation: 415

PowerShell: Script cannot set a parameter

For a script that should be compatible to powerShell 2, I have a param called $exeLoc, declared as follows: Param( [parameter()] [alias("el")] $exeLoc= '......\sw' )

I try to set that parameter, from relative to absolute in a function, as follows:

Function FromRelToAbs()
{
   Push-Location $exeLoc
   $Global:exeLoc = (Join-path $PWD -ChildPath '\Vis.exe' )
  Pop-Location
}

However, after calling the function above, the value of $exeLoc does not change.

The above code works perfectly on powersell v3 AND in powershell v2 ISE. It does not work properly on a powershell v2 window ( not ISE )

Any ideas ?

Upvotes: 0

Views: 351

Answers (1)

Rakhesh Sasidharan
Rakhesh Sasidharan

Reputation: 1063

I take it your script is something along these lines:

param($x = 2)

write-output "[script] Value of x is 2"

function blah {
  $global:x = 5
  write-output "[blah] Setting x as 5"
}

Write-Output "Calling function blah"
blah
Write-Output "[script] Value of x is $x"

You set the variable in the script; and within the script is a function that refers to the variable in the global scope and sets its value.

If I try the above on my machine (PowerShell v2) it does set the variable correctly. Maybe I understood your script wrong?

Upvotes: 0

Related Questions