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

LVL 1
amaru96Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
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.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
But you can always work with the remote session itself, if you can't change the script to give back the result.
$SUG = Invoke-Command -Session $session -ScriptBlock { $SUG }

Open in new window

But of course it is much better to change the invoked script directly:
$session = New-PSSession -ComputerName $computer -ConfigurationName Microsoft.Powershell32 -Credential $cred
$SUG = 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):"
	Get-CMSoftwareUpdateGroup | select LocalizedDisplayName
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
amaru96Author Commented:
Thanks Qlemo, that worked great.
footechCommented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.