Public/Functions/split/Get-OSDDriverWmiQ.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
<#
.SYNOPSIS
Returns a Computer Model WMI Query that can be used in Task Sequences
 
.DESCRIPTION
Returns a Computer Model WMI Query that can be used in Task Sequences
 
.LINK
https://osd.osdeploy.com/module/functions/driver/get-osddriverwmiq
 
.NOTES
19.12.6 David Segura @SeguraOSD
#>

function Get-OSDDriverWmiQ {
    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline = $true)]
        [Object[]]$InputObject,

        #Select a Computer Manufacturer OSDGroup
        #Default is DellModel
        [Parameter(ValueFromPipelineByPropertyName)]
        [ValidateSet ('DellModel','HpModel')]
        [string]$OSDGroup,

        #Select whether the Query is based off Model or SystemId SystemSku Product
        #Default is Model
        [ValidateSet ('Model','SystemId')]
        [string]$Result = 'Model',

        #Open a Text File with the WMI Query after completion
        [System.Management.Automation.SwitchParameter]$ShowTextFile
    )

    begin {
        #=================================================
        # Information
        #=================================================
        Write-Verbose 'Get-OSDDriverWmiQ: Results are saved in the Global Variable $GetOSDDriverWmiQ for this PowerShell session'
        $OSDComputerModels = @()
        $OSDModelPacks = @()
    }

    process {
        if ($InputObject) {
            $OSDModelPacks += $InputObject
            $OSDComputerModels = foreach ($ModelPack in $OSDModelPacks) {
                foreach ($item in $ModelPack.Model) {
                    $ObjectProperties = @{
                        Model = $item
                    }
                    New-Object -TypeName PSObject -Property $ObjectProperties
                }
            }
        } else {
            $OSDModelPacks = @()
            $OSDModelPacks = Get-OSDDriver $OSDGroup | Sort-Object Model -Unique
            $OSDModelPacks = $OSDModelPacks | Select-Object Make, Model, Generation, SystemSku | Out-GridView -PassThru -Title 'Select Computer Models to Generate a WMI Query'
        }
    }

    end {
        $Items = @()
        #=================================================
        # Model
        #=================================================
        if ($Result -eq 'Model') {
            foreach ($Item in $OSDModelPacks.Model) {$Items += $Item}
            $Items = $Items | Sort-Object -Unique
            $WmiQueryFullName = Join-Path -Path $env:TEMP -ChildPath "WmiQuery.txt"
            $WmiCodeString = [System.Text.StringBuilder]::new()
            [void]$WmiCodeString.AppendLine('SELECT Model FROM Win32_ComputerSystem WHERE')

            foreach ($Item in $Items) {
                [void]$WmiCodeString.AppendLine("Model = '$($Item)'")
    
                if ($Item -eq $Items[-1]){
                    #"last item in array is $Item"
                } else {
                    [void]$WmiCodeString.Append('OR ')
                }
            }
            $WmiCodeString.ToString() | Out-File -FilePath $WmiQueryFullName -Encoding UTF8 -Width 2000 -Force
            if ($ShowTextFile.IsPresent) {
                notepad.exe $WmiQueryFullName
            }
            $global:GetOSDDriverWmiQ = $WmiCodeString.ToString()
            Return $global:GetOSDDriverWmiQ
        }
        #=================================================
        # Dell SystemId
        #=================================================
        if ($Result -eq 'SystemId' -and $OSDGroup -eq 'DellModel') {
            foreach ($Item in $OSDModelPacks.SystemSku) {$Items += $Item}
            $Items = $Items | Sort-Object -Unique
            $WmiQueryFullName = Join-Path -Path $env:TEMP -ChildPath "WmiQuery.txt"
            $WmiCodeString = [System.Text.StringBuilder]::new()
            [void]$WmiCodeString.AppendLine('SELECT SystemSku FROM Win32_ComputerSystem WHERE')
        
            foreach ($Item in $Items) {
                [void]$WmiCodeString.AppendLine("SystemSku = '$($Item)'")
    
                if ($Item -eq $Items[-1]){
                    #"last item in array is $Item"
                } else {
                    [void]$WmiCodeString.Append('OR ')
                }
            }
            $WmiCodeString.ToString() | Out-File -FilePath $WmiQueryFullName -Encoding UTF8 -Width 2000 -Force
            if ($ShowTextFile.IsPresent) {
                notepad.exe $WmiQueryFullName
            }
            Return $WmiCodeString.ToString()
        }
        #=================================================
        # HP SystemId
        #=================================================
        if ($Result -eq 'SystemId' -and $OSDGroup -eq 'HpModel') {
            foreach ($Item in $OSDModelPacks.SystemSku) {$Items += $Item}

            $Items = $Items | Sort-Object -Unique
            $WmiQueryFullName = Join-Path -Path $env:TEMP -ChildPath "WmiQuery.txt"
            $WmiCodeString = [System.Text.StringBuilder]::new()
            [void]$WmiCodeString.AppendLine('SELECT Product FROM Win32_BaseBoard WHERE')
            foreach ($Item in $Items) {
                [void]$WmiCodeString.AppendLine("Product = '$($Item)'")
    
                if ($Item -eq $Items[-1]){
                    #"last item in array is $Item"
                } else {
                    [void]$WmiCodeString.Append('OR ')
                }
            }
            $WmiCodeString.ToString() | Out-File -FilePath $WmiQueryFullName -Encoding UTF8 -Width 2000 -Force
            if ($ShowTextFile.IsPresent) {
                notepad.exe $WmiQueryFullName
            }
            $global:GetOSDDriverWmiQ = $WmiCodeString.ToString()
            Return $global:GetOSDDriverWmiQ
        }
    }
}