Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

How to pass the value out of the script, I am not sure how to use global or reference variable.

$
0
0
New-Variable -Name GlobalVar -Value "Initial" -Scope "Global"
write "GlobalVar is $GlobalVar"

$script = 
{
	param(
		$server,
		$mycred
	)
	$GlobalVar="Different"
}
# Passing of the parameters to the block through argumentlist.
Invoke-Command -credential $mycred -ComputerName $server -ScriptBlock $script -ArgumentList $server,$mycred

write "GlobalVar is $GlobalVar"

Output is 
GlobalVar is Initial
GlobalVar is Initial

It should be as below as the $GlobalVar is modified in the script block...
GlobalVar is Initial
GlobalVar is Different


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>