Reputation: 77
I am trying to access the array variable outside the invoke command. I tried the below code, where I cannot access the remote array variable from my local session.
$serverlist = @("server1", "server2")
foreach ($server in $serverlist) {
#Write-Host $computer
$vinodh = Invoke-Command -ComputerName $server -ScriptBlock {
$testVar = @("Stack", "over", "flow")
}
}
foreach ($vars in $testVar) {
Write-Host $vars # Unable to get the values as stack,over, flow
}
Actual results: unable to get values.
I expect the output as
stack over flow
Upvotes: 0
Views: 738
Reputation: 2208
Variables set inside the remote session were not populated to the local powershell session (About Scopes). You could return
the values from the invoked session for using later.
$ReturnValues = Invoke-Command -ComputerName $Server -ScriptBlock {
$testVar=@("Stack","over","flow")
return $testVar #return data
}
foreach ($ReturnValue in $ReturnValues)
{
$ReturnValue
}
Upvotes: 1