private/_GetUpdateInfo.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
function _GetUpdateInfo {
    [cmdletbinding()]
    Param(
        [Microsoft.Management.Infrastructure.CimSession]$Cimsession
    )
    Write-Verbose "[$($CimSession.ComputerName)] - Gathering Security Update information"
    try {
        $Updates = Get-CimInstance -CimSession $CimSession -ClassName Win32_QuickFixEngineering -ErrorAction Stop
        $Updates | ForEach-Object {
            [PSCustomObject]@{
                PSTypeName    = 'PSP.Inventory.SecurityUpdate'
                ComputerName  = $Cimsession.ComputerName
                KBFile        = $_.HotFixID
                Type          = $_.Description
                KBLink        = $_.Caption
                InstalledBy   = $_.InstalledBy
                InstallDate   = $_.InstalledOn
                InventoryDate = (Get-Date)
            }
        }
    }
    catch [Microsoft.Management.Infrastructure.CimException] {
        Write-Warning "[$($CimSession.ComputerName)] - Unable to access data over WinRM, trying DCOM instead"
        $CimOptions = New-CimSessionOption -Protocol DCOM

        $CimProperties = @{
            ErrorAction  = 'Stop'
            Computername = $Cimsession.ComputerName
            SessionOption = $CimOptions
        }
        try {
            $NewCimSession = New-CimSession @CimProperties
        }
        catch {
            Write-Warning "[$Computer] - cannot be reached. $($_.Exception.Message)"
        }
        Finally {
            $CimProperties.Remove('SessionOption') | Out-Null
        }
        try {
            $Updates = Get-CimInstance -CimSession $NewCimSession -ClassName Win32_QuickFixEngineering -ErrorAction Stop
            $Updates | ForEach-Object {
                [PSCustomObject]@{
                    PSTypeName    = 'PSP.Inventory.SecurityUpdate'
                    ComputerName  = $NewCimsession.ComputerName
                    KBFile        = $_.HotFixID
                    Type          = $_.Description
                    KBLink        = $_.Caption
                    InstalledBy   = $_.InstalledBy
                    InstallDate   = $_.InstalledOn
                    InventoryDate = (Get-Date)
                }
            }
        } catch {
            Write-Warning "[$Computer] - cannot be reached. $($_.Exception.Message)"
        }
    }
}