locked
Hashtable Repeating Objects RRS feed

  • Question


  • Process { foreach($Computer in $ComputerName) { Try { $OS = Get-Wmiobject -ComputerName $Computer -Class Win32_OperatingSystem -ErrorAction Stop If ($OS.Version -match '5.2') { $2003Update = Get-EventLog -ComputerName $Computer -LogName system -InstanceId 19 -Newest 3 | where {$_.Message -match 'Update for Windows 2003'} $2003UpdateName = (@($2003Update | foreach {($_.Message.tostring().split(':')[2]).Trim()}) -join ',') } If ($OS.Version -ge '6.1') { $NewUpdates = Get-EventLog -ComputerName $Computer -LogName system -InstanceId 19 -Newest 3 -EntryType Information | where {$_.Message -match 'Quality'} $NewUpdateName = (@($NewUpdates | foreach {($_.Message.tostring().split(':')[2]).Trim()}) -join ',') } $Output=New-Object psobject -Property @{ "ServerName" = $computer; "OS Name" = $OS.Caption; "New Cumulative" = $NewUpdateName; "Old Update" = $2003UpdateName; } $Output } } catch {}

    I am not sure why this is happening but I get repeat results for Updates when I run above code for multiple computers.

    What I get is shown below and I cant figure out why? Please help.

    ServerName           : Server1
    OS Name              : Microsoft(R) Windows(R) Server 2003, Enterprise Edition
    Old Update        : Security Update for Windows Server 2003 (KBXXXXXXX), Security Update for Windows Server 2003 (KBYYYYYYY),Security Update for Windows Server 2003 (KBZZZZZZZ)

    ServerName           : Server2
    OS Name              : Microsoft Windows Server 2008 R2 Enterprise 
    New Cumulative   : 2018-02 Security Only Quality Update for Windows Server 2008 R2 for x64-based Systems (KB4074587),2018-02 Security Monthly Quality Rollup for Windows Server 2008 R2 for x64-based Systems (KB4074598)
    Old Update        : Security Update for Windows Server 2003 (KBXXXXXXX), Security Update for Windows Server 2003 (KBYYYYYYY),Security Update for Windows Server 2003 (KBZZZZZZZ)

    Why is the code repeating 'Old update' for Server 2 when I have put in an IF statement at the top.

    I know I can write the code better but the question right now is repetition.



    • Edited by strike3test Thursday, March 15, 2018 10:18 PM
    Thursday, March 15, 2018 10:17 PM

Answers

  • This is how you need to do this to get consistent output:

    Process {
        foreach($Computer in $ComputerName) {
            Try {
                $results = [pscustomobject]@{
                    ServerName    = $computer
                    OSName        = ''
                    NewCumulative = ''
                    OldUpdate     = ''
                }
                $OS = Get-Wmiobject -ComputerName $Computer -Class Win32_OperatingSystem -ErrorAction Stop
                $results.OSName = $OS.Caption
                If ($OS.Version -match '5.2') {
                    $results.NewCumulative = Get-EventLog -ComputerName $Computer -LogName system -InstanceId 19 -Newest 3 | where {$_.Message -match 'Update for Windows 2003'}
                    $results.OldUpdate = (@($2003Update | foreach {($_.Message.tostring().split(':')[2]).Trim()}) -join ',')
                 }
                                
                    If ($OS.Version -ge '6.1') {
                        $results.NewCumulative =  Get-EventLog -ComputerName $Computer -LogName system -InstanceId 19 -Newest 3 -EntryType Information | where {$_.Message -match 'Quality'}
                        $results.OldUpdate = (@($NewUpdates | foreach {($_.Message.tostring().split(':')[2]).Trim()}) -join ',')
                    }
    $results } catch { Throw $_ } } }


    \_(ツ)_/


    • Edited by jrv Thursday, March 15, 2018 10:29 PM
    • Marked as answer by strike3test Friday, March 16, 2018 1:38 AM
    Thursday, March 15, 2018 10:29 PM