Resources/Development/Start-WimBotCLI.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
function Start-DEVWimRobotCLI {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]$ImagePath,

        [Int32]$Index = 1,

        [System.Management.Automation.SwitchParameter]$Update
    )
    function Show-WimRobotTime {
        [CmdletBinding()]
        param ()
        #=================================================
        # Show-ActionTime
        #=================================================
        $Global:WimRobotTime = Get-Date
        Write-Host -ForegroundColor DarkGray "$(($Global:WimRobotTime).ToString('yyyy-MM-dd-HHmmss')) " -NoNewline
        #=================================================
    }
    #=================================================
    # Block
    #=================================================
    Show-WimRobotTime; Write-Host 'Verify Admin Rights'
    Block-StandardUser
    Show-WimRobotTime; Write-Host 'Verify Windows 10 or Higher'
    Block-WindowsVersionNe10
    #=================================================
    # Test if ImagePath exists
    #=================================================
    Show-WimRobotTime; Write-Host "Test-Path $ImagePath"
    if (!(Test-Path $ImagePath)) {
        Show-WimRobotTime; Write-Warning "Unable to find ImagePath at $ImagePath"
        Break
    }
    #=================================================
    # GetItem
    #=================================================
    Show-WimRobotTime; Write-Host "Get-Item -Path $ImagePath"

    try {
        $GetImagePath = Get-Item -Path $ImagePath -ErrorAction Stop
    }
    catch {
        Show-WimRobotTime; Write-Warning $_.Exception.Message
        Break
    }
    #=================================================
    # Test if file is a WIM
    #=================================================
    if ($GetImagePath.Extension -ne '.wim') {
        Show-WimRobotTime; Write-Warning "ImagePath is not a .wim file"
        Break
    }
    #=================================================
    # Test WindowsImage
    #=================================================
    Show-WimRobotTime; Write-Host "Get-WindowsImage -ImagePath $ImagePath -Index $Index"
    try {
        $GetWindowsImage = Get-WindowsImage -ImagePath $ImagePath -Index $Index -ErrorAction Stop
    }
    catch {
        Show-WimRobotTime; Write-Warning $_.Exception.Message
        Break
    }
    $GetWindowsImage | Select-Object -Property *
    #=================================================
    # Verify WindowsImage
    #=================================================
    if ($GetWindowsImage.InstallationType -ne '10') {
        Show-WimRobotTime; Write-Warning 'MajorVersion 10 is required'
        Break
    }
    if ($GetWindowsImage.InstallationType -ne 'Client') {
        Show-WimRobotTime; Write-Warning 'InstallationType Server is required'
        Break
    }
    #=================================================
    # Mount WindowsImage
    #=================================================








    



    Break
    $MountPath = (Get-Item -Path $Input | Select-Object FullName).FullName
    Write-Verbose "Path: $MountPath" -Verbose





    #=================================================
    # Validate Mount Path
    #=================================================
    if (-not (Test-Path $Input -ErrorAction SilentlyContinue)) {
        Write-Warning "Update-MyWindowsImage: Unable to locate Mounted WindowsImage at $Input"
        Break
    }
    #=================================================
    # Get Registry Information
    #=================================================
    $global:GetRegCurrentVersion = Get-RegCurrentVersion -Path $Input
    #=================================================
    # Require OSMajorVersion 10
    #=================================================
    if ($global:GetRegCurrentVersion.CurrentMajorVersionNumber -ne 10) {
        Write-Warning "Update-MyWindowsImage: OS MajorVersion 10 is required"
        Break
    }

    Write-Verbose -Verbose $global:GetRegCurrentVersion.ReleaseId
    #=================================================
    # Get-WSUSXML and Filter Results
    #=================================================
    $global:GetWSUSXML = Get-WSUSXML -Catalog Windows | Sort-Object UpdateGroup -Descending

    if ($global:GetRegCurrentVersion.ReleaseId -gt 0) {
        $global:GetWSUSXML = $global:GetWSUSXML | Where-Object {$_.UpdateBuild -eq $global:GetRegCurrentVersion.DisplayVersion}
    }
    else {
        $global:GetWSUSXML = $global:GetWSUSXML | Where-Object {$_.UpdateBuild -eq $global:GetRegCurrentVersion.ReleaseId}
    }

    if ($global:GetRegCurrentVersion.BuildLabEx -match 'amd64') {
        $global:GetWSUSXML = $global:GetWSUSXML | Where-Object {$_.UpdateArch -eq 'x64'}
    } else {
        $global:GetWSUSXML = $global:GetWSUSXML | Where-Object {$_.UpdateArch -eq 'x86'}
    }
    if ($global:GetRegCurrentVersion.InstallationType -match 'WindowsPE') {
        $global:GetWSUSXML = $global:GetWSUSXML | Where-Object {$_.UpdateOS -eq 'Windows 10'}
        $global:GetWSUSXML = $global:GetWSUSXML | Where-Object {$_.UpdateGroup -notmatch 'Adobe'}
        $global:GetWSUSXML = $global:GetWSUSXML | Where-Object {$_.UpdateGroup -notmatch 'DotNet'}
    }
    if ($global:GetRegCurrentVersion.InstallationType -match 'Core') {
        $global:GetWSUSXML = $global:GetWSUSXML | Where-Object {$_.UpdateGroup -notmatch 'Adobe'}
    }
    if ($global:GetRegCurrentVersion.InstallationType -match 'Client') {
        $global:GetWSUSXML = $global:GetWSUSXML | Where-Object {$_.UpdateOS -notmatch 'Server'}
    }
    if ($global:GetRegCurrentVersion.InstallationType -match 'Server') {
        $global:GetWSUSXML = $global:GetWSUSXML | Where-Object {$_.UpdateOS -match 'Server'}
    }

    #Don't install Optional Updates
    $global:GetWSUSXML = $global:GetWSUSXML | Where-Object {$_.UpdateGroup -ne ''}

    if ($Update -ne 'Check' -and $Update -ne 'All') {
        $global:GetWSUSXML = $global:GetWSUSXML | Where-Object {$_.UpdateGroup -match $Update}
    }
    #=================================================
    # Get-SessionsXml
    #=================================================
    $global:GetSessionsXml = Get-SessionsXml -Path "$Input" | Where-Object {$_.targetState -eq 'Installed'} | Sort-Object id
    #=================================================
    # Apply Update
    #=================================================
    foreach ($item in $global:GetWSUSXML) {
        if (! ($Force.IsPresent)) {
            if ($global:GetSessionsXml | Where-Object {$_.KBNumber -match "$($item.FileKBNumber)"}) {
                Write-Verbose "Installed: $($item.Title) $($item.FileName)" -Verbose
                Continue
            } else {
                Write-Warning "Not Installed: $($item.Title) $($item.FileName)"
            }
        }

        if ($Update -eq 'Check') {Continue}
                
<# if ($BitsTransfer.IsPresent) {
            $UpdateFile = Save-OSDDownload -SourceUrl $item.OriginUri -BitsTransfer -Verbose
        } else {
            $UpdateFile = Save-OSDDownload -SourceUrl $item.OriginUri -Verbose
        } #>

        $UpdateFile = Save-WebFile -SourceUrl $item.OriginUri
        $CurrentLog = "$env:TEMP\OSD\$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Update-MyWindowsImage.log"

        if (! (Test-Path "$env:TEMP\OSD")) {New-Item -Path "$env:TEMP\OSD" -Force | Out-Null}

        if (Test-Path $UpdateFile.FullName) {
            #Write-Verbose "Add-WindowsPackage -PackagePath $($UpdateFile.FullName) -Path $Input" -Verbose
            Try {
                Write-Verbose "Add-WindowsPackage -Path $Input -PackagePath $($UpdateFile.FullName)" -Verbose
                Add-WindowsPackage -Path $Input -PackagePath $UpdateFile.FullName -LogPath $CurrentLog | Out-Null
            }
            Catch {
                if ($_.Exception.Message -match '0x800f081e') {
                Write-Verbose "Update-MyWindowsImage: 0x800f081e The package is not applicable to this image" -Verbose}
                Write-Verbose $CurrentLog -Verbose
            }
        } else {
            Write-Warning "Unable to download $($UpdateFile.FullName)"
        }
    }
    #=================================================
    # Return for PassThru
    #=================================================
    Get-WindowsImage -Mounted | Where-Object {$_.Path -eq $MountPath}
    #=================================================
}