Public/Catalog/Get-CatalogHPPlatformList.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<#
.SYNOPSIS
Converts the HP Platform list to a PowerShell Object. Useful to get the computer model name for System Ids
 
.DESCRIPTION
Converts the HP Platform list to a PowerShell Object. Useful to get the computer model name for System Ids
Requires Internet Access to download platformList.cab
 
 
.EXAMPLE
Get-CatalogHPPlatformList
Don't do this, you will get a big list.
 
.EXAMPLE
$Result = Get-CatalogHPPlatformList
Yes do this. Save it in a Variable
 
.EXAMPLE
Get-CatalogHPPlatformList | Out-GridView
Displays all the HP System Ids with the applicable computer model names in GridView
 
.LINK
https://osd.osdeploy.com/module/functions
 
.NOTES
#>

function Get-CatalogHPPlatformList {
    [CmdletBinding()]
    
    #=================================================
    # Paths
    #=================================================
    $CatalogState           = 'Online' #Online, Build, Local, Offline
    $CatalogOnlinePath      = 'https://ftp.hp.com/pub/caps-softpaq/cmit/imagepal/ref/platformList.cab'
    $CatalogBuildPath       = Join-Path $env:TEMP 'platformList.xml'
    $CatalogLocalPath          = Join-Path $env:TEMP 'CatalogHPPlatformList.xml'
    $CatalogOfflinePath     = "$($MyInvocation.MyCommand.Module.ModuleBase)\Files\Catalogs\CatalogHPPlatformList.xml"
    $CatalogLocalCabName      = [string]($CatalogOnlinePath | Split-Path -Leaf)
    $CatalogLocalCabPath     = Join-Path $env:TEMP $CatalogLocalCabName
    #=================================================
    # Test CatalogState Local
    #=================================================
    if (Test-Path $CatalogLocalPath) {

        #Get-Item to determine the age
        $GetItemCatalogLocalPath = Get-Item $CatalogLocalPath

        #If the local is older than 12 hours, delete it
        if (((Get-Date) - $GetItemCatalogLocalPath.LastWriteTime).TotalHours -gt 12) {
            Write-Verbose "Removing previous Offline Catalog"
        }
        else {
            $CatalogState = 'Local'
        }
    }
    #=================================================
    # Test CatalogState Online
    #=================================================
    if ($CatalogState -eq 'Online') {
        if (Test-WebConnection -Uri $CatalogOnlinePath) {
            #Catalog is online and can be downloaded
        }
        else {
            $CatalogState = 'Offline'
        }
    }
    #=================================================
    # CatalogState Online
    # Need to get the Online Catalog to Local
    #=================================================
    if ($CatalogState -eq 'Online') {
        Write-Verbose "Source: $CatalogOnlinePath"
        Write-Verbose "Destination: $CatalogLocalCabPath"
        (New-Object System.Net.WebClient).DownloadFile($CatalogOnlinePath, $CatalogLocalCabPath)

        #Make sure the file downloaded
        if (Test-Path $CatalogLocalCabPath) {
            Write-Verbose "Expand: $CatalogLocalCabPath"
            Expand "$CatalogLocalCabPath" "$CatalogBuildPath" | Out-Null

            if (Test-Path $CatalogBuildPath) {
                $CatalogState = 'Build'
            }
            else {
                Write-Verbose "Could not expand $CatalogLocalCabPath"
                $CatalogState = 'Offline'
            }
        }
        else {
            $CatalogState = 'Offline'
        }
    }
    #=================================================
    # CatalogState Build
    #=================================================
    if ($CatalogState -eq 'Build') {
        Write-Verbose "Reading the System Catalog at $CatalogBuildPath"
        [xml]$XmlCatalogContent = Get-Content $CatalogBuildPath -ErrorAction Stop
        $CatalogVersion = $XmlCatalogContent.ImagePal.DateLastModified | Get-Date -Format yy.MM.dd
        $Platforms = $XmlCatalogContent.ImagePal.Platform

        Write-Verbose "Building the System Catalog"
        
        $Result = foreach($platform in $Platforms){             
            $ObjectProperties = [Ordered]@{
                CatalogVersion            = $CatalogVersion
                SystemId                  = $platform.SystemId
                SupportedModel            = [array]($platform.ProductName.'#text')
                #LatestWin10SupportedBuild = $platform.OS | Sort-Object -Property OSBuildId -Descending | Select-Object -First 1 -ExpandProperty OSReleaseIdDisplay
            }
            New-Object -TypeName PSObject -Property $ObjectProperties
        }
        
    
        Write-Verbose "Exporting Offline Catalog to $CatalogLocalPath"
        $Result = $Result | Sort-Object -Property SystemId
        $Result | Export-Clixml -Path $CatalogLocalPath
    }
    #=================================================
    # CatalogState Local
    #=================================================
    if ($CatalogState -eq 'Local') {
        Write-Verbose "Reading the Local System Catalog at $CatalogLocalPath"
        $Result = Import-Clixml -Path $CatalogLocalPath
    }
    #=================================================
    # CatalogState Offline
    #=================================================
    if ($CatalogState -eq 'Offline') {
        Write-Verbose "Reading the Offline System Catalog at $CatalogOfflinePath"
        $Result = Import-Clixml -Path $CatalogOfflinePath
    }
    #=================================================
    # Compatible
    #=================================================
    if ($PSBoundParameters.ContainsKey('Compatible')) {
        $MyComputerProduct = Get-MyComputerProduct
        Write-Verbose "Filtering XML for items compatible with Product $MyComputerProduct"
        $Result = $Result | Where-Object {$_.SystemID -eq $MyComputerProduct}
    }
    #=================================================
    # Component
    #=================================================
    $Result | Sort-Object -Property SystemId
    #=================================================
}