I use SAPIEN PowerShell Studio 2015 and JobTracker
to run the background job in my Powershell script. I need to load the variables from external file. I tried PowerShell 4 and 5 versions. I tried to achieve my goal by using:
1 Dot sourcing
2 Powershell module Psm1
with Import-Module
function and global
definition of the variables
If I load it into the main part of the script it works, but it does not work inside Add-JobTracker
block.
Here is the code:
Variables.ps1
$TestVariable1 = "TestValue"
Variables.psm1
$global:TestVariable1 = "TestValue"
Dot Sourcing in the Form Load Event (works fine)
$formMain_Load = {
$VariablesScript = $PSScriptRoot + "\Variables.ps1"
. $VariablesScript
Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object system.Windows.Forms.Form
$Form.Text = $TestVariable1 #<-- TestVariable1 is correct
$Form.ShowDialog()
}
Import-Module using psm1
module in the Form Load Event (works fine)
$formMain_Load = {
$VariablesScript = $PSScriptRoot + "\Variables.psm1"
Import-Module $VariablesScript
Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object system.Windows.Forms.Form
$Form.Text = $global:TestVariable1 #<-- TestVariable1 is correct
$Form.ShowDialog()
}
}
Dot Sourcing in the Add-JobTracker (does not work)
#Create a New Job using the Job Tracker
Add-JobTracker -Name "Job1" `
-JobScript {
#--------------------------------------------------
#TODO: Set a script block
#Pass any arguments using the ArgumentList parameter
Param ($Argument1)
$VariablesScript = $PSScriptRoot + "\Variables.ps1"
. $VariablesScript
Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object system.Windows.Forms.Form
$Form.Text = $TestVariable1 #<-- TestVariable1 is null
$Form.ShowDialog()
...
Import-Module using psm1
module in the Add-JobTracker (does not work)
#Create a New Job using the Job Tracker
Add-JobTracker -Name "Job1" `
-JobScript {
#--------------------------------------------------
#TODO: Set a script block
#Pass any arguments using the ArgumentList parameter
Param ($Argument1)
$VariablesScript = $PSScriptRoot + "\Variables.psm1"
Import-Module $VariablesScript
Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object system.Windows.Forms.Form
$Form.Text = $global:TestVariable1 #<-- TestVariable1 is null
$Form.ShowDialog()
...
If I define the Variable1
in the Add-JobTracker
block it works fine.
But how can I load this variable into Add-JobTracker
from external file?