9

Running the following code produces an error because the variable used for path is resolved as null event though it defined in the script:

$ServerName = "test01"
$RemotePath = "C:\Test\"
$TestScriptBlock = { copy-item -Path $RemotePath  -Destination C:\backup\ -Force -Recurse } 
$CurrentSession = New-PSSession -ComputerName $ServerName

Invoke-Command -Session $CurrentSession -ScriptBlock $TestScriptBlock 

How do I call the $RemotePath defined in the parent script from within the ScriptBlock? I need to use $RemotePath in other parts of the parent script. Note, this value doesn't change, so it can be a constant.

UPDATE -- WORKING SOLUTION

You have to pass in variable as parameter to the scriptblock:

$ServerName = "test01"
$RemotePath = "C:\Test\"
$TestScriptBlock = { param($RemotePath) copy-item -Path $RemotePath  -Destination C:\backup\ -Force -Recurse } 
$CurrentSession = New-PSSession -ComputerName $ServerName

Invoke-Command -Session $CurrentSession -ScriptBlock $TestScriptBlock -ArgumentList $RemotePath 
3
  • use can try using $global:RemotePath = "C:\Test\" as global variable which can be accessed anywhere in entire script file Commented Jul 16, 2014 at 12:03
  • tried that, it doesn't work. Commented Jul 16, 2014 at 12:26
  • Does $using:RemotePath work? Commented May 13, 2016 at 14:07

3 Answers 3

4

You've got two scripts there, not one. The $TestScriptBlock is a separate script nested inside the main one, you send it to the remote computer, and that remote computer doesn't have $RemotePath configured. Try:

$ServerName = "test01"

$TestScriptBlock = {
    $RemotePath = "C:\Test\"
    copy-item -Path $RemotePath  -Destination C:\backup\ -Force -Recurse 
}

$CurrentSession = New-PSSession -ComputerName $ServerName

Invoke-Command -Session $CurrentSession -ScriptBlock $TestScriptBlock

(I would probably call it $LocalPath then, though)

Sign up to request clarification or add additional context in comments.

3 Comments

Anyway of having a defining one global variable within this parent script so that I can use it in both the scriptblock and the parent script? Otherwise I'll need to have duplicate variables to maintain in the script because I need to use it in other locations outside of the script block. Note, this value doesn't change, so it can be a constant.
Yes maybe although I haven't tried - powershell.com/cs/blogs/tips/archive/2012/10/26/… seems like it would help.
I got it to work thanks to your link in your comment. I'll add an update above so that others can see the solution. Thanks.
2

Try this syntax:

$globalvariable1 = "testoutput01"
$globalvariable2 = "testoutput02"

$Scriptblock = {
    Write-Host $using:globalvariable1
    Write-Host $using:globalvariable2
}

$serverName = Domain\HostNameofServer

Invoke-Command -ComputerName $serverName -ScriptBlock $ScriptBlock -ArgumentList $globalvariable1, $globalvariable2

Comments

1

The execution happens in a different session and all the variables in the current scope are not copied to the remote session by default. You can use either parameters or "using:" as explained here:

How can I pass a local variable to a script block executed on a remote machine with Invoke-Command?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.