0

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?

Dmitry Kazakov
  • 1,639
  • 3
  • 27
  • 47
  • Possible duplicate of [How to store variables to disk so you can use them next time the PowerShell script runs?](http://stackoverflow.com/questions/1606138/how-to-store-variables-to-disk-so-you-can-use-them-next-time-the-powershell-scri) – Bacon Bits Sep 11 '16 at 12:19
  • Are you sure that it works using Add-JobTracker ? – Dmitry Kazakov Sep 11 '16 at 13:11
  • I don't think it is duplicate. My question is related to the background job using Add-JobTracker as I could retrieve the values in the Page Load event, but I could not in the background job using Add-JobTracker. – Dmitry Kazakov Sep 11 '16 at 13:30
  • "*Possible* duplicate". Every time I've loaded variables from a file, I've used the CliXml cmdlets or ConvertFrom-StringData. Since that was the title of your question, I assumed that was your primary question. Every time I've used Jobs -- I've never used JobTracker and don't particularly care for Sapien -- I've passed variables with `-ArgumentList` and written my jobs accordingly. When I've used RunSpaces for multithreading, I've used Command.Parameters. I've never tried using global variables because I've found scoping in PowerShell to be relatively flaky and unintuitive. – Bacon Bits Sep 11 '16 at 13:43
  • I will test CliXml in my environment and reply here with the details. Thank you. – Dmitry Kazakov Sep 11 '16 at 14:04

0 Answers0