Local variable in a session via a job

I have a PS scheduled job that executes a script that opens a remote session. I define a variable in the script ($var), and latter I use it in the remote session. Why can’t I use this in the ps job? I can when running the script directly. The credentials for the job are the same as the logged in user.

Invoke-Command -Session $session -ScriptBlock {New-item "$using:var" -ItemType directory}

Of course you can use the variable created in a remote system using a session and access the variable in another execution using the same session. But where is the variable created, local machine or in the remote machine ?

It is the local machine. And it works when I run the script directly, rather than feed it to a ps scheduled job. So it must be something in the job.

PS Job creates a separate PowerShell.exe for each trigger. Hence the variables won’t persisted. I would suggest you to share the code/sample here which will make this clear.

Your problem is the limitation of scopes. Specifically, “An item you include in a scope is visible in the scope in which it was created and in any child scope”.

If you run a job, and the job executes a script, and the script creates a variable, that variable is available for the script and any child processes of the script (including your remote session), but it is not available to the job that executed the script because the script is a child of the job.

If you create the variable in the job, it should be available for the script and anything that the script does. Alternatively, you could try making the variable global, but this might cause problems if your script executes multiple times (resulting in each instance trying to write to the same variable). You could also explicitly make the script pass the variable up to the job.

These should help you in your use case…

About Remote Variables
You can use variables in commands that you run on remote computers. Simply assign a value to the variable and then use the variable in place of the value.

By default, the variables in remote commands are assumed to be defined in the session in which the command runs. You can also use variables that are defined in the local session, but you must identify them as local variables in the command.

Access Local Variables in a Remote Session with PowerShell
https://mikefrobbins.com/2017/11/09/access-local-variables-in-a-remote-session-with-powershell

Using a local variable in a remote PowerShell session
https://4sysops.com/archives/using-a-local-variable-in-a-remote-powershell-session

PowerShell: Passing variables to remote commands

Powershell pass variable to start-job

https://stackoverflow.com/questions/10075943/powershell-pass-variable-to-start-job

Use the -ArgumentList parameter on Start-Job e.g.:
Start-Job -Scriptblock {param($p) "`$p is $p"} -Arg 'Server1'

# this
$pingblock = {param($servername) pathping $servername | Out-File C:\...\ServerPing.txt}
Start-Job $pingblock -Arg Server1


# or 
Start-Job -ScriptBlock {Get-ChildItem  $args[0],$args[1] } -ArgumentList $a,$b

How to pass variables to Start-Job?
https://stackoverflow.com/questions/41764926/how-to-pass-variables-to-start-job

This is how you can pass a value inside Start-Job.

You have to invoke it from inside. This is my blog link for the same: Start-Job Passing value

$ini='$var="'+$args[0]+'"'
$a={
 Function Get-add()
        {
        "this  is the value of $var"

        }
 }
start-job -InitializationScript $a -ScriptBlock {param($ini)iex $ini;get-add } -ArgumentList $ini |Wait-Job | Receive-Job

See also:

About Remote Variables
You can use variables in commands that you run on remote computers. Simply assign a value to the variable and then use the variable in place of the value.

By default, the variables in remote commands are assumed to be defined in the session in which the command runs. You can also use variables that are defined in the local session, but you must identify them as local variables in the command.

Access Local Variables in a Remote Session with PowerShell
https://mikefrobbins.com/2017/11/09/access-local-variables-in-a-remote-session-with-powershell

Using a local variable in a remote PowerShell session
https://4sysops.com/archives/using-a-local-variable-in-a-remote-powershell-session

PowerShell: Passing variables to remote commands

Ended up using the script params and -argumentlist as below. Thanks.

Invoke-Command -Session $session -ScriptBlock {param($var) New-item $var -ItemType directory} -ArgumentList $var