Combine 2 PSObjects

Welcome Forums General PowerShell Q&A Combine 2 PSObjects

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

 
Member
3 years, 9 months ago.

  • Author
    Posts
  • #23591

    Participant
    Points: 0
    Rank: Member

    I am making a WSUS Report that consists of 2 different type's of queries. I am saving both results to separate $variables. I am looking for a way to combine both variables by matching the comnputername . This is what I have so far

    Import-Module UpdateServices
    
    #Connect To WSUS
    $wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer()
    
    $Servers = ipcsv  "C:\PS\CSVs\downstream.csv"
    
    
    #get LastReported
    $lastreport = foreach ($s in  $Servers.server) {
    
    #Remote Server
    $DownStreamWsus = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer("$($s)",$False,'8530')
    
    $DownStreamWsus.GetComputerTargets() | 
    ? { 
    $_.RequestedTargetGroupName -eq  'General Workstations'  -or 
    $_.RequestedTargetGroupName -eq  'Test Workstations'  -or 
    $_.RequestedTargetGroupName -eq  'Voice Mail Workstations'  
    
    }  |  select IPAddress,FullDomainName,RequestedTargetGroupName,LastReportedStatusTime | sort FullDomainName -Descending 
    
    }
    
    
    #-Requires both an update scope and a computer scope
    $updateScope = New-Object Microsoft.UpdateServices.Administration.UpdateScope
    $computerScope = New-Object Microsoft.UpdateServices.Administration.ComputerTargetScope
    
    #get Counts
    $counts =$wsus.GetSummariesPerComputerTarget($updateScope,$computerScope) | ForEach {
    
        $object = [pscustomobject] @{
            Computername = $wsus.GetComputerTarget($_.ComputerTargetID).FullDomainName
            Installed = $_.Installedcount       
            Failed = $_.Failedcount
            Downloaded = $_.DownloadedCount
            NotInstalled = $_.NotInstalledCount
            Unknown = $_.UnknownCount
            PendingReboot = $_.InstalledPendingRebootCount
        }
        $object.pstypenames.insert(0,"wsus.clientupdate.statistics")
        $object
    }
    
    
    #Build Object
    
    foreach ($l in $lastreport){
    
    foreach ($c in $counts){
    
    if ($($l.FullDomainName) -match $($c.computername) ) {
    
    
    [pscustomobject] @{
            IP = $i.IPAddress
            Computername = $l.FullDomainName
            Group = $l.RequestedTargetGroupName
            LastReported = $l. LastReportedStatusTime 
            Failed = $c.Failed
            NotInstalled = $c.NotInstalled
            PendingReboot = $c.PendingReboot
           }
        }
      }
    }
    
    
    
  • #23592

    Member
    Points: 0
    Rank: Member

    Have a look at the Join-Object function from the PowerShell blog: http://blogs.msdn.com/b/powershell/archive/2012/07/13/join-object.aspx

    Just keep in mind that doing this in PowerShell can be pretty slow, and if you're working with large datasets you'd be better off putting the information into a database (possibly with indexes) and letting it handle the joins.

  • #23593

    Participant
    Points: 0
    Rank: Member

    I made an adjustment and It is now working.

  • #23594

    Participant
    Points: 0
    Rank: Member

    ok I will Thank you Dave

  • #23595

    Participant
    Points: 0
    Rank: Member

    hey Dave The way i have it written its creating a lot of duplicate records do you know? This part is creating the dupes

    foreach ($l in $lastreport){
     
    foreach ($c in $counts){
     
    if ($($l.FullDomainName) -match $($c.computername) ) {
     
     
    [pscustomobject] @{
            IP = $i.IPAddress
            Computername = $l.FullDomainName
            Group = $l.RequestedTargetGroupName
            LastReported = $l. LastReportedStatusTime 
            Failed = $c.Failed
            NotInstalled = $c.NotInstalled
            PendingReboot = $c.PendingReboot
           }
        }
      }
    }
    
  • #23596

    Member
    Points: 0
    Rank: Member

    No idea. I'd need to see example data and the results that you're getting (and how that differs from what you expected to get.)

The topic ‘Combine 2 PSObjects’ is closed to new replies.