Link to home
Start Free TrialLog in
Avatar of amaru96
amaru96

asked on

Access a variable created within a remote session

Hi guys, is there a way I can access a variable which is created and populated within a remote PS session?

How can I access the $SUG variable in the below code?

$session = New-PSSession -ComputerName $computer -ConfigurationName Microsoft.Powershell32 -Credential $cred
Invoke-Command -Session $session -ArgumentList $computer -ScriptBlock {
    param($computer=$computer)
     
	import-module ($Env:SMS_ADMIN_UI_PATH.Substring(0,$Env:SMS_ADMIN_UI_PATH.Length-5) + '\ConfigurationManager.psd1')
	$PSD = Get-PSDrive -PSProvider CMSite
	CD "$($PSD):"
	$SUG = Get-CMSoftwareUpdateGroup | select LocalizedDisplayName
	exit
}

Do stuff with the $SUG variable here....

Open in new window

Avatar of footech
footech
Flag of United States of America image

You either have to work with the variable within the remote session (that's where it exists), or output it, which will mean that its contents are sent back to the local machine, and you can deal with the output there, but you can't directly access the variable of the remote session in the local session.
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of amaru96
amaru96

ASKER

Thanks Qlemo, that worked great.
That's actually what I mentioned as the second option.  "...or output it, which will mean that its contents are sent back to the local machine, and you can deal with the output there.."

But I'm glad Qlemo's example made it clear to you.