8

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 Answers3

3

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)

TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87
  • 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. –  Jul 16 '14 at 11:55
  • Yes maybe although I haven't tried - http://powershell.com/cs/blogs/tips/archive/2012/10/26/executing-code-locally-and-remotely-using-local-variables.aspx seems like it would help. – TessellatingHeckler Jul 16 '14 at 12:08
  • 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. –  Jul 16 '14 at 12:25
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
Elijan9
  • 1,269
  • 2
  • 17
  • 18
0

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?

Community
  • 1
  • 1
jonatasmello
  • 86
  • 1
  • 5