-
Notifications
You must be signed in to change notification settings - Fork 10
/
OOBE.ps1
329 lines (315 loc) · 12.8 KB
/
OOBE.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
[CmdletBinding()]
param()
#region Initialize
#Start the Transcript
$Transcript = "$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-OSDCloud.log"
$null = Start-Transcript -Path (Join-Path "$env:SystemRoot\Temp" $Transcript) -ErrorAction Ignore
#=================================================
# oobeCloud Settings
#=================================================
$Global:oobeCloud = @{
oobeSetDisplay = $true
oobeSetRegionLanguage = $true
oobeSetDateTime = $true
oobeRegisterAutopilot = $false
oobeRegisterAutopilotCommand = 'Get-WindowsAutopilotInfo -Online -GroupTag Demo -Assign'
oobeRemoveAppxPackage = $true
oobeRemoveAppxPackageName = 'CommunicationsApps','OfficeHub','People','Skype','Solitaire','Xbox','ZuneMusic','ZuneVideo'
oobeAddCapability = $true
oobeAddCapabilityName = 'GroupPolicy','ServerManager','VolumeActivation'
oobeUpdateDrivers = $true
oobeUpdateWindows = $true
oobeRestartComputer = $true
oobeStopComputer = $false
}
function Step-KeyboardLanguage {
Write-Host -ForegroundColor Green "Set keyboard language to de-CH"
Start-Sleep -Seconds 5
$LanguageList = Get-WinUserLanguageList
$LanguageList.Add("de-CH")
Set-WinUserLanguageList $LanguageList -Force | Out-Null
Start-Sleep -Seconds 5
$LanguageList = Get-WinUserLanguageList
$LanguageList.Remove(($LanguageList | Where-Object LanguageTag -like 'en-US'))
Set-WinUserLanguageList $LanguageList -Force | Out-Null
}
function Step-oobeSetDisplay {
[CmdletBinding()]
param ()
if (($env:UserName -eq 'defaultuser0') -and ($Global:oobeCloud.oobeSetDisplay -eq $true)) {
Write-Host -ForegroundColor Yellow 'Verify the Display Resolution and Scale is set properly'
Start-Process 'ms-settings:display' | Out-Null
$ProcessId = (Get-Process -Name 'SystemSettings').Id
if ($ProcessId) {
Wait-Process $ProcessId
}
}
}
function Step-oobeSetRegionLanguage {
[CmdletBinding()]
param ()
if (($env:UserName -eq 'defaultuser0') -and ($Global:oobeCloud.oobeSetRegionLanguage -eq $true)) {
Write-Host -ForegroundColor Yellow 'Verify the Language, Region, and Keyboard are set properly'
Start-Process 'ms-settings:regionlanguage' | Out-Null
$ProcessId = (Get-Process -Name 'SystemSettings').Id
if ($ProcessId) {
Wait-Process $ProcessId
}
}
}
function Step-oobeSetDateTime {
[CmdletBinding()]
param ()
if (($env:UserName -eq 'defaultuser0') -and ($Global:oobeCloud.oobeSetDateTime -eq $true)) {
Write-Host -ForegroundColor Yellow 'Verify the Date and Time is set properly including the Time Zone'
Write-Host -ForegroundColor Yellow 'If this is not configured properly, Certificates and Domain Join may fail'
Start-Process 'ms-settings:dateandtime' | Out-Null
$ProcessId = (Get-Process -Name 'SystemSettings').Id
if ($ProcessId) {
Wait-Process $ProcessId
}
}
}
function Step-oobeExecutionPolicy {
[CmdletBinding()]
param ()
if ($env:UserName -eq 'defaultuser0') {
if ((Get-ExecutionPolicy) -ne 'RemoteSigned') {
Write-Host -ForegroundColor Cyan 'Set-ExecutionPolicy RemoteSigned'
Set-ExecutionPolicy RemoteSigned -Force
}
}
}
function Step-oobePackageManagement {
[CmdletBinding()]
param ()
if ($env:UserName -eq 'defaultuser0') {
if (Get-Module -Name PowerShellGet -ListAvailable | Where-Object {$_.Version -ge '2.2.5'}) {
Write-Host -ForegroundColor Cyan 'PowerShellGet 2.2.5 or greater is installed'
}
else {
Write-Host -ForegroundColor Cyan 'Install-Package PackageManagement,PowerShellGet'
Install-Package -Name PowerShellGet -MinimumVersion 2.2.5 -Force -Confirm:$false -Source PSGallery | Out-Null
Write-Host -ForegroundColor Cyan 'Import-Module PackageManagement,PowerShellGet'
Import-Module PackageManagement,PowerShellGet -Force
}
}
}
function Step-oobeTrustPSGallery {
[CmdletBinding()]
param ()
if ($env:UserName -eq 'defaultuser0') {
$PSRepository = Get-PSRepository -Name PSGallery
if ($PSRepository)
{
if ($PSRepository.InstallationPolicy -ne 'Trusted')
{
Write-Host -ForegroundColor Cyan 'Set-PSRepository PSGallery Trusted'
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
}
}
}
}
function Step-oobeInstallModuleAutopilot {
[CmdletBinding()]
param ()
if ($env:UserName -eq 'defaultuser0') {
$Requirement = Import-Module WindowsAutopilotIntune -PassThru -ErrorAction Ignore
if (-not $Requirement)
{
Write-Host -ForegroundColor Cyan 'Install-Module AzureAD,Microsoft.Graph.Intune,WindowsAutopilotIntune'
Install-Module WindowsAutopilotIntune -Force
}
}
}
function Step-oobeInstallModuleAzureAd {
[CmdletBinding()]
param ()
if ($env:UserName -eq 'defaultuser0') {
$Requirement = Import-Module AzureAD -PassThru -ErrorAction Ignore
if (-not $Requirement)
{
Write-Host -ForegroundColor Cyan 'Install-Module AzureAD'
Install-Module AzureAD -Force
}
}
}
function Step-oobeInstallScriptAutopilot {
[CmdletBinding()]
param ()
if ($env:UserName -eq 'defaultuser0') {
$Requirement = Get-InstalledScript -Name Get-WindowsAutoPilotInfo -ErrorAction SilentlyContinue
if (-not $Requirement)
{
Write-Host -ForegroundColor Cyan 'Install-Script Get-WindowsAutoPilotInfo'
Install-Script -Name Get-WindowsAutoPilotInfo -Force
}
}
}
function Step-oobeRegisterAutopilot {
[CmdletBinding()]
param (
[System.String]
$Command
)
if (($env:UserName -eq 'defaultuser0') -and ($Global:oobeCloud.oobeRegisterAutopilot -eq $true)) {
Step-oobeInstallModuleAutopilot
Step-oobeInstallModuleAzureAd
Step-oobeInstallScriptAutopilot
Write-Host -ForegroundColor Cyan 'Registering Device in Autopilot in new PowerShell window ' -NoNewline
$AutopilotProcess = Start-Process PowerShell.exe -ArgumentList "-Command $Command" -PassThru
Write-Host -ForegroundColor Green "(Process Id $($AutopilotProcess.Id))"
Return $AutopilotProcess
}
}
function Step-oobeRemoveAppxPackage {
if (($env:UserName -eq 'defaultuser0') -and ($Global:oobeCloud.oobeRemoveAppxPackage -eq $true)) {
Write-Host -ForegroundColor Cyan 'Removing Appx Packages'
foreach ($Item in $Global:oobeCloud.oobeRemoveAppxPackageName) {
if (Get-Command Get-AppxProvisionedPackage) {
Get-AppxProvisionedPackage -Online | Where-Object {$_.DisplayName -Match $Item} | ForEach-Object {
Write-Host -ForegroundColor DarkGray $_.DisplayName
if ((Get-Command Remove-AppxProvisionedPackage).Parameters.ContainsKey('AllUsers')) {
Try
{
$null = Remove-AppxProvisionedPackage -Online -AllUsers -PackageName $_.PackageName
}
Catch
{
Write-Warning "AllUsers Appx Provisioned Package $($_.PackageName) did not remove successfully"
}
}
else {
Try
{
$null = Remove-AppxProvisionedPackage -Online -PackageName $_.PackageName
}
Catch
{
Write-Warning "Appx Provisioned Package $($_.PackageName) did not remove successfully"
}
}
}
}
}
}
}
function Step-oobeAddCapability {
if (($env:UserName -eq 'defaultuser0') -and ($Global:oobeCloud.oobeAddCapability -eq $true)) {
Write-Host -ForegroundColor Cyan "Add-WindowsCapability"
foreach ($Item in $Global:oobeCloud.oobeAddCapabilityName) {
$WindowsCapability = Get-WindowsCapability -Online -Name "*$Item*" -ErrorAction SilentlyContinue | Where-Object {$_.State -ne 'Installed'}
if ($WindowsCapability) {
foreach ($Capability in $WindowsCapability) {
Write-Host -ForegroundColor DarkGray $Capability.DisplayName
$Capability | Add-WindowsCapability -Online | Out-Null
}
}
}
}
}
function Step-oobeUpdateDrivers {
[CmdletBinding()]
param ()
if (($env:UserName -eq 'defaultuser0') -and ($Global:oobeCloud.oobeUpdateDrivers -eq $true)) {
Write-Host -ForegroundColor Cyan 'Updating Windows Drivers'
if (!(Get-Module PSWindowsUpdate -ListAvailable -ErrorAction Ignore)) {
try {
Install-Module PSWindowsUpdate -Force
Import-Module PSWindowsUpdate -Force
}
catch {
Write-Warning 'Unable to install PSWindowsUpdate Driver Updates'
}
}
if (Get-Module PSWindowsUpdate -ListAvailable -ErrorAction Ignore) {
Start-Process PowerShell.exe -ArgumentList "-Command Install-WindowsUpdate -UpdateType Driver -AcceptAll -IgnoreReboot" -Wait
}
}
}
function Step-oobeUpdateWindows {
[CmdletBinding()]
param ()
if (($env:UserName -eq 'defaultuser0') -and ($Global:oobeCloud.oobeUpdateWindows -eq $true)) {
Write-Host -ForegroundColor Cyan 'Updating Windows'
if (!(Get-Module PSWindowsUpdate -ListAvailable)) {
try {
Install-Module PSWindowsUpdate -Force
Import-Module PSWindowsUpdate -Force
}
catch {
Write-Warning 'Unable to install PSWindowsUpdate Windows Updates'
}
}
if (Get-Module PSWindowsUpdate -ListAvailable -ErrorAction Ignore) {
#Write-Host -ForegroundColor DarkCyan 'Add-WUServiceManager -MicrosoftUpdate -Confirm:$false'
Add-WUServiceManager -MicrosoftUpdate -Confirm:$false | Out-Null
#Write-Host -ForegroundColor DarkCyan 'Install-WindowsUpdate -UpdateType Software -AcceptAll -IgnoreReboot'
#Install-WindowsUpdate -UpdateType Software -AcceptAll -IgnoreReboot -NotTitle 'Malicious'
#Write-Host -ForegroundColor DarkCyan 'Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -IgnoreReboot'
Start-Process PowerShell.exe -ArgumentList "-Command Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -IgnoreReboot -NotTitle 'Preview' -NotKBArticleID 'KB890830','KB5005463','KB4481252'" -Wait
}
}
}
function Invoke-Webhook {
$BiosSerialNumber = Get-MyBiosSerialNumber
$ComputerManufacturer = Get-MyComputerManufacturer
$ComputerModel = Get-MyComputerModel
$URI = 'https://XXXX.webhook.office.com/webhookb2/YYYY'
$JSON = @{
"@type" = "MessageCard"
"@context" = "<http://schema.org/extensions>"
"title" = 'OSDCloud Information'
"text" = "The following client has been successfully deployed:<br>
BIOS Serial Number: **$($BiosSerialNumber)**<br>
Computer Manufacturer: **$($ComputerManufacturer)**<br>
Computer Model: **$($ComputerModel)**"
} | ConvertTo-JSON
$Params = @{
"URI" = $URI
"Method" = 'POST'
"Body" = $JSON
"ContentType" = 'application/json'
}
Invoke-RestMethod @Params | Out-Null
}
function Step-oobeRestartComputer {
[CmdletBinding()]
param ()
if (($env:UserName -eq 'defaultuser0') -and ($Global:oobeCloud.oobeRestartComputer -eq $true)) {
Write-Host -ForegroundColor Cyan 'Build Complete!'
Write-Warning 'Device will restart in 30 seconds. Press Ctrl + C to cancel'
Stop-Transcript
Start-Sleep -Seconds 30
Restart-Computer
}
}
function Step-oobeStopComputer {
[CmdletBinding()]
param ()
if (($env:UserName -eq 'defaultuser0') -and ($Global:oobeCloud.oobeStopComputer -eq $true)) {
Write-Host -ForegroundColor Cyan 'Build Complete!'
Write-Warning 'Device will shutdown in 30 seconds. Press Ctrl + C to cancel'
Stop-Transcript
Start-Sleep -Seconds 30
Stop-Computer
}
}
#endregion
# Execute functions
Step-KeyboardLanguage
Step-oobeExecutionPolicy
Step-oobePackageManagement
Step-oobeTrustPSGallery
Step-oobeSetDisplay
Step-oobeSetRegionLanguage
Step-oobeSetDateTime
Step-oobeRegisterAutopilot
Step-oobeRemoveAppxPackage
Step-oobeAddCapability
Step-oobeUpdateDrivers
Step-oobeUpdateWindows
Invoke-Webhook
Step-oobeRestartComputer
Step-oobeStopComputer
#=================================================