Public/Functions/split/Start-OOBEDeploy.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
function Start-OOBEDeploy {
    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline = $true)]
        [string]$CustomProfile,
        [System.Management.Automation.SwitchParameter]$AddNetFX3,
        [System.Management.Automation.SwitchParameter]$AddRSAT,
        [System.Management.Automation.SwitchParameter]$Autopilot,
        [string]$ProductKey,
        [string[]]$RemoveAppx,
        [System.Management.Automation.SwitchParameter]$UpdateDrivers,
        [System.Management.Automation.SwitchParameter]$UpdateWindows,
        [ValidateSet('Enterprise')]
        [string]$SetEdition
    )
    #=================================================
    # Block
    #=================================================
    Block-StandardUser
    Block-WindowsVersionNe10
    Block-PowerShellVersionLt5
    #=================================================
    # Start
    #=================================================
    if ($env:SystemDrive -eq 'X:') {
        #=================================================
        # WinPE
        #=================================================
        Write-Host -ForegroundColor DarkGray "========================================================================="
        Write-Host -ForegroundColor Green "Start-OOBEDeploy in WinPE"
        $ProgramDataOSDeploy = 'C:\ProgramData\OSDeploy'
        $JsonPath = "$ProgramDataOSDeploy\OSDeploy.OOBEDeploy.json"
    }
    else {
        #=================================================
        # WinOS
        #=================================================
        Write-Host -ForegroundColor DarkGray "========================================================================="
        Write-Host -ForegroundColor Green "Start-OOBEDeploy"
        $ProgramDataOSDeploy = "$env:ProgramData\OSDeploy"
        $JsonPath = "$ProgramDataOSDeploy\OSDeploy.OOBEDeploy.json"
        #=================================================
        # Transcript
        #=================================================
        Write-Host -ForegroundColor DarkGray "========================================================================="
        Write-Host -ForegroundColor Cyan "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Start-Transcript"
        $Transcript = "$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-OOBEDeploy.log"
        Start-Transcript -Path (Join-Path "$env:SystemRoot\Temp" $Transcript) -ErrorAction Ignore
        #=================================================
        # Window Title
        #=================================================
        $Global:OOBEDeployWindowTitle = "Running Start-OOBEDeploy $env:SystemRoot\Temp\$Transcript"
        $host.ui.RawUI.WindowTitle = $Global:OOBEDeployWindowTitle
    }
    #=================================================
    # Custom Profile Sample Variables
    #=================================================
    if ($CustomProfile -eq 'Sample') {
        $AddNetFX3 = $true
        $AddRSAT = $true
        $Autopilot = $true
        $UpdateDrivers = $true
        $UpdateWindows = $true
        $RemoveAppx = @('CommunicationsApps','OfficeHub','People','Skype','Solitaire','Xbox','ZuneMusic','ZuneVideo')
        $SetEdition = 'Enterprise'
    }
    #=================================================
    # Custom Profile
    #=================================================
    if ($CustomProfile) {
        Write-Host -ForegroundColor DarkGray "========================================================================="
        Write-Host -ForegroundColor Cyan "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Loading OOBEDeploy Custom Profile $CustomProfile"

        $CustomProfileJson = Get-ChildItem "$($MyInvocation.MyCommand.Module.ModuleBase)\Resources\CustomProfile\OOBEDeploy" *.json | Where-Object {$_.BaseName -eq $CustomProfile} | Select-Object -First 1

        if ($CustomProfileJson) {
            Write-Host -ForegroundColor DarkGray "Saving Module CustomProfile to $JsonPath"
            if (!(Test-Path "$ProgramDataOSDeploy")) {New-Item "$ProgramDataOSDeploy" -ItemType Directory -Force | Out-Null}
            Copy-Item -Path $CustomProfileJson.FullName -Destination $JsonPath -Force -ErrorAction Ignore
        }
    }
    #=================================================
    # Import Json
    #=================================================
    if (Test-Path $JsonPath) {
        Write-Host -ForegroundColor DarkGray "Importing Configuration $JsonPath"
        $ImportOOBEDeploy = @()
        $ImportOOBEDeploy = Get-Content -Raw -Path $JsonPath | ConvertFrom-Json
    
        $ImportOOBEDeploy.PSObject.Properties | ForEach-Object {
            if ($_.Value -match 'IsPresent=True') {
                $_.Value = $true
            }
            if ($_.Value -match 'IsPresent=False') {
                $_.Value = $false
            }
            if ($null -eq $_.Value) {
                Continue
            }
            Set-Variable -Name $_.Name -Value $_.Value -Force
        }
    }
    #=================================================
    # WinOS PSGallery
    #=================================================
    if ($env:SystemDrive -ne 'X:') {
        $PSGalleryIP = (Get-PSRepository -Name PSGallery).InstallationPolicy
        if ($PSGalleryIP -eq 'Untrusted') {
            Write-Host -ForegroundColor DarkGray "========================================================================="
            Write-Host -ForegroundColor Cyan "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Set-PSRepository -Name PSGallery -InstallationPolicy Trusted"
            if ($env:UserName -eq 'defaultuser0') {
                Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
            }
            else {
                Write-Warning 'OOBE defaultuser0 is required to run the following command'
                Write-Host -ForegroundColor Cyan 'Set-PSRepository -Name PSGallery -InstallationPolicy Trusted'
            }
        }
    }
    #=================================================
    # Initialize Global Variable
    #=================================================
    $Global:OOBEDeploy = [ordered]@{
        AddNetFX3 = $AddNetFX3
        AddRSAT = $AddRSAT
        Autopilot = $Autopilot
        CustomProfile = $CustomProfile
        ProductKey = $ProductKey
        RemoveAppx = $RemoveAppx
        SetEdition = $SetEdition
        UpdateDrivers = $UpdateDrivers
        UpdateWindows = $UpdateWindows
    }

    if ($env:SystemDrive -eq 'X:') {
        Write-Host -ForegroundColor DarkGray "Exporting Configuration $ProgramDataOSDeploy\OSDeploy.OOBEDeploy.json"
        @($Global:OOBEDeploy.Keys) | ForEach-Object { 
            if (-not $Global:OOBEDeploy[$_]) { $Global:OOBEDeploy.Remove($_) } 
        }
        $Global:OOBEDeploy | ConvertTo-Json | Out-File "$ProgramDataOSDeploy\OSDeploy.OOBEDeploy.json" -Encoding ascii -Width 2000 -Force
    }
    else {
        Write-Host -ForegroundColor DarkGray "Exporting Configuration $env:Temp\OSDeploy.OOBEDeploy.json"
        @($Global:OOBEDeploy.Keys) | ForEach-Object { 
            if (-not $Global:OOBEDeploy[$_]) { $Global:OOBEDeploy.Remove($_) } 
        }
        $Global:OOBEDeploy | ConvertTo-Json | Out-File "$env:Temp\OSDeploy.OOBEDeploy.json" -Encoding ascii -Width 2000 -Force
        #=================================================
        # ProductKey
        #=================================================
        if ($SetEdition -eq 'Enterprise') {$ProductKey = 'NPPR9-FWDCX-D2C8J-H872K-2YT43'}
        if ($ProductKey) {
            Write-Host -ForegroundColor DarkGray "========================================================================="
            Write-Host -ForegroundColor Cyan "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Set-WindowsEdition (ChangePK)"
            if ($env:UserName -eq 'defaultuser0') {
                Invoke-Exe changepk.exe /ProductKey $ProductKey
                Get-WindowsEdition -Online
            }
            else {
                Write-Warning 'OOBE defaultuser0 is required to run this group'
                Start-Sleep -Seconds 5
            }
        }
        #=================================================
        # Autopilot
        #=================================================
        if ($Autopilot) {
            Write-Host -ForegroundColor DarkGray "========================================================================="
            Write-Host -ForegroundColor Cyan "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) AutopilotOOBE"
            Write-Host -ForegroundColor DarkCyan "Install-Module AutopilotOOBE -Force"
            Write-Warning "AutopilotOOBE will open in a new PowerShell Window while OOBEDeploy continues in the background"
            if (($env:UserName -eq 'defaultuser0') -or (!(Get-Module AutopilotOOBE -ListAvailable -ErrorAction Ignore))) {
                Install-Module AutopilotOOBE -Force
            }
            if ($CustomProfile) {
                Start-Process PowerShell.exe -ArgumentList "-Command Start-AutopilotOOBE -CustomProfile $CustomProfile"
            }
            else {
                Start-Process PowerShell.exe -ArgumentList "-Command Start-AutopilotOOBE"
            }
        }
        #=================================================
        # AddNetFX3
        #=================================================
        if ($AddNetFX3) {
            Write-Host -ForegroundColor DarkGray "========================================================================="
            Write-Host -ForegroundColor Cyan "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Invoke-oobeAddNetFX3"
            Invoke-oobeAddNetFX3
            $host.ui.RawUI.WindowTitle = $Global:OOBEDeployWindowTitle
        }
        #=================================================
        # AddRSAT
        #=================================================
        if ($AddRSAT) {
            Write-Host -ForegroundColor DarkGray "========================================================================="
            Write-Host -ForegroundColor Cyan "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Invoke-oobeAddRSAT"
            Invoke-oobeAddRSAT
            $host.ui.RawUI.WindowTitle = $Global:OOBEDeployWindowTitle
        }
        #=================================================
        # Remove-AppxOnline
        #=================================================
        if ($RemoveAppx) {
            Write-Host -ForegroundColor DarkGray "========================================================================="
            Write-Host -ForegroundColor Cyan "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Invoke-oobeRemoveAppx -RemoveAppx"
            Invoke-oobeRemoveAppx -RemoveAppx $RemoveAppx
            $host.ui.RawUI.WindowTitle = $Global:OOBEDeployWindowTitle
        }
        #=================================================
        # UpdateDrivers
        #=================================================
        if ($UpdateDrivers) {
            Write-Host -ForegroundColor DarkGray "========================================================================="
            Write-Host -ForegroundColor Cyan "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Invoke-oobeUpdateDrivers"
            Write-Host -ForegroundColor DarkCyan 'Device Drivers are being updated in a minimized window'
            Write-Host -ForegroundColor DarkCyan 'Use Alt+Tab to switch Windows and view progress'
            #Invoke-oobeUpdateDrivers
            Start-Process PowerShell.exe -Wait -WindowStyle Minimized -ArgumentList "-Command Invoke-oobeUpdateDrivers"
        }
        #=================================================
        # Windows Update Software
        #=================================================
        if ($UpdateWindows) {
            Write-Host -ForegroundColor DarkGray "========================================================================="
            Write-Host -ForegroundColor Cyan "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Invoke-oobeUpdateWindows"
            Write-Host -ForegroundColor DarkCyan 'Windows Updates are being updated in a minimized window'
            Write-Host -ForegroundColor DarkCyan 'Use Alt+Tab to switch Windows and view progress'
            #Invoke-oobeUpdateWindows
            Start-Process PowerShell.exe -Wait -WindowStyle Minimized -ArgumentList "-Command Invoke-oobeUpdateWindows"
        }
        #=================================================
        # Restart
        #=================================================
        Write-Host -ForegroundColor DarkGray "========================================================================="
        $host.ui.RawUI.WindowTitle = "Start-OOBEDeploy $env:SystemRoot\Temp\$Transcript"
        Write-Warning "Restart-Computer before completing OOBE"
        Write-Host -ForegroundColor DarkGray "========================================================================="
        #=================================================
    }
}