Create a local variable with data gathered from a pssession

I have two separate forests not joined via a trust relationship. Both forests use the same WSUS server for patch management. I am creating a script that queries the WSUS database for the different target groups so that I can use the PSWINDOWSUPDATE module to target specific patch groups. The WSUS server is domain joined to one of the forests. From the untrusted forest, I want to connect to the WSUS server to query the target group. I cant figure out how to specify alternate credential with the [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer .net class. So I used new-pssession with secure cred to connect to the server and run the query. This creates the variable on the WSUS server itself rather than where I am running the script. I want to put the target computers obtained by the pssession to the wsus server in a variable within the script to use when I invoke-windowsupdate from the module. Since the pssession is where the query originated, how can I get the returned results into a variable within the script?

It is in early stages and a rough draft now, but I am pasting the relevant portion of the code below.

$wsuscred = Get-Credential

new-pssession -ComputerName -Credential $wsuscred -Name wsusconnect

$computers = @()

$wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer("",$true, 8531)
$Group1 = $wsus.GetComputerTargetGroups() | where {$_.Name -eq "Patch group 1"}
$computers+= $Group1.getcomputertargets().fulldomainname

remove-pssession -session wsusconnect

you can do an Export-CliXML of the variables to an XML file and the do Import-CliXML in the required script.

Invoke-Command -Session $Session -ScriptBlock { Get-Process | Export-CliXML -Path c:\temp\variables.xml }

#The in the other script you can jus do
$Variables = Import-CliXML -Path c:\temp\variables.xml


If you are using the same session for executing the second script, then the variables created will be persistent in the session.

Invoke-Command -Session $Session -ScriptBlock { $r = Get-Process }
Invoke-Command -Session $Session -ScriptBlock { $r }

It is as simple as declaring it using the session. Consider the below code.

$TestVariable = $env:COMPUTERNAME
$targetServer = 'serverName'
$pssess = New-PSSession -ComputerName $targetServer
$TestVariable = Invoke-Command -Session $pssess -ScriptBlock {$env:COMPUTERNAME}

You will see the value of $TestVariable is updated with the hostname it obtained through the command invoked to that PSSession. I obtained information from a remote host to populate a variable for use within the current session. I hope you find this information helpful.