Create a local variable with data gathered from a pssession

Welcome Forums General PowerShell Q&A Create a local variable with data gathered from a pssession

This topic contains 2 replies, has 3 voices, and was last updated by

 
Participant
3 weeks, 3 days ago.

  • Author
    Posts
  • #131018

    Participant
    Points: 33
    Rank: Member

    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 wsusserver.domain.com -Credential $wsuscred -Name wsusconnect
    
    
    $computers = @()
    
    
    [reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration") 
    $wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer("wsusserver.domain.com",$true, 8531)
    $Group1 = $wsus.GetComputerTargetGroups() | where {$_.Name -eq "Patch group 1"}
    $computers+= $Group1.getcomputertargets().fulldomainname
    
    $computers
    remove-pssession -session wsusconnect
  • #131019

    Participant
    Points: 1,086
    Helping Hand
    Rank: Community Hero

    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
    
    $Variables
    

    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 }
    
  • #131060

    Participant
    Points: 232
    Helping Hand
    Rank: Participant

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

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

    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.

You must be logged in to reply to this topic.