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