Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell: Script Module variables not available outside of ISE

Tags:

powershell

I have been working on updating some of the scripts I have written. I am still learning PowerShell so I like to go back over my old ones and see if I can improve things.
I stumbled upon Modules and really like the idea of using them to store different actions that can then be called on at anytime.

I wrote a simple script to test (or so I thought). I should mention I am storing my modules on a server share.

Write-Host "Testing for available variables"
Write-Host "Loading Modules"
Import-Module <path to my module on server share>.psm1
Write-Host ""
Write-Host "Done"
Write-Host "Print loaded modules"
Get-Module
Write-Host ""
Write-Host "Done"
Write-Host "================="
Write-Host ""
Write-Host "Printing variables"
Write-Host ""
Write-Host "Variable: arch: " $arch
Write-Host "Variable: WowNode: " $WowNode
Write-Host ""
Write-Host "Done"

My module contained

If ( "${env:ProgramFiles(x86)}" -ne "" )
 {
    $script:arch = 64
    $script:WowNode = "\Wow6432Node\"
 }
 ELSE
 {
    $script:arch = 32
    $script:WowNode = "\"
 }

When I run my script using Windows Powershell ISE everything functions how I thought it would. I see the contents of the variables and they print out correctly.
When I run my script from a Powershell Command Line window, the module gets loaded, but the variables are blank (nothing prints out).
I have been digging around Google and the only thing I saw that could maybe be causing this was related to the scope I am using to define my variables.
Defining them as $script:xx puts them into the Global scope (I'm pretty sure) which makes them available throughout your script. So I think I am defining my variables correctly. But I still haven't had any luck.

I am probably missing something obvious, but for the life of me I can't find it.
Any suggestions on what I can do to troubleshoot this issue?
I have confirmed I am running version 2.0 of PowerShell since I read that Modules are only available in version 2.0.

like image 354
bourne Avatar asked Apr 17 '13 21:04

bourne


1 Answers

From what I can think of right now(without testing), the problem is the scope as you say. There is a difference between running the script in ISE and in a console. I'm guessing when you're running the script in ISE, that you open the script and press the run button. This will execute the script line by line in the console, AS IF you were typing them directly into the console. This makes the script itself run in the global (session) variable scope. Since the script was in global, your module will be the only one running in script scope.

However, when you run the script in a powershell console; like:

PS > .\myscript.ps1

Then your script is run in the script scope(every variable is deleted when the script is done). Now, the module will run in it's own script scope, BELOW the scope that your mainscript runs in. When the module is finished with being imported, the module's script scope is deleted, and your main script will look in it's own scope, where you variables do not exist.

Global (session)
 - Script (mainscript)
   - Script (module)
     #Module completes, and scope is deleted
   #You look for $script:arch , which was actually deleted right above.
   #Powershell searches through the mainscript's scope, but finds nothing.

To solve this, try storing the values in the global (session) scope, like this $global:arch. Specify global as the scope when you create the variable(required), and when you read from it(recommended).

like image 142
Frode F. Avatar answered Oct 26 '22 16:10

Frode F.