title: How to load variables from external file into PowerShell background job using Add-JobTracker from SAPIEN PowerShell Studio?
body: <p>I use SAPIEN PowerShell Studio 2015 and <code>JobTracker</code> 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:</p>
  
  <p>1 Dot sourcing</p>
  
  <p>2 Powershell module <code>Psm1</code> with <code>Import-Module</code> function and <code>global</code> definition of the variables</p>
  
  <p>If I load it into the main part of the script it works, but it does not work inside <code>Add-JobTracker</code> block. </p>
  
  <p>Here is the code: </p>
  
  <h1>Variables.ps1</h1>
  
  <pre><code>$TestVariable1 = "TestValue"
  </code></pre>
  
  <h1>Variables.psm1</h1>
  
  <pre><code>$global:TestVariable1 = "TestValue"
  </code></pre>
  
  <h1>Dot Sourcing in the Form Load Event (works fine)</h1>
  
  <pre><code>$formMain_Load = {
      $VariablesScript = $PSScriptRoot + "\Variables.ps1"
      . $VariablesScript
      Add-Type -AssemblyName System.Windows.Forms
      $Form = New-Object system.Windows.Forms.Form
      $Form.Text = $TestVariable1 #&lt;-- TestVariable1 is correct
      $Form.ShowDialog()
  }
  </code></pre>
  
  <h1>Import-Module using <code>psm1</code> module in the Form Load Event (works fine)</h1>
  
  <pre><code>$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 #&lt;-- TestVariable1 is correct
      $Form.ShowDialog()
  }
  }
  </code></pre>
  
  <h1>Dot Sourcing in the Add-JobTracker (does not work)</h1>
  
  <pre><code>#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 #&lt;-- TestVariable1 is null
          $Form.ShowDialog()
  ...
  </code></pre>
  
  <h1>Import-Module using <code>psm1</code> module in the Add-JobTracker (does not work)</h1>
  
  <pre><code>#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 #&lt;-- TestVariable1 is null
          $Form.ShowDialog() 
  ...
  </code></pre>
  
  <p>If I define the <code>Variable1</code> in the <code>Add-JobTracker</code> block it works fine.</p>
  
  <p>But how can I load this variable into <code>Add-JobTracker</code> from external file?</p>