[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" = "" "title" = 'OSDCloud Information' "text" = "The following client has been successfully deployed:
BIOS Serial Number: **$($BiosSerialNumber)**
Computer Manufacturer: **$($ComputerManufacturer)**
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 #=================================================