# oobedeploy.hwz.osdcloud.ch $Global:Transcript = "$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Start-OOBEDeploy.log" Start-Transcript -Path (Join-Path "$env:ProgramData\Microsoft\IntuneManagementExtension\Logs\OSD\" $Global:Transcript) -ErrorAction Ignore $Title = "OOBE Deploy" $host.UI.RawUI.WindowTitle = $Title [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 [System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials $env:APPDATA = "C:\Windows\System32\Config\SystemProfile\AppData\Roaming" $env:LOCALAPPDATA = "C:\Windows\System32\Config\SystemProfile\AppData\Local" $Env:PSModulePath = $env:PSModulePath+";C:\Program Files\WindowsPowerShell\Scripts" $env:Path = $env:Path+";C:\Program Files\WindowsPowerShell\Scripts" #Write-Host -ForegroundColor DarkGray "=========================================================================" #Write-Host -ForegroundColor Green "Installing OSD PowerShell Module" #Install-Module OSD -Force Write-Host -ForegroundColor DarkGray "=========================================================================" Write-Host -ForegroundColor Green "Importing OSD PowerShell Module" Import-Module OSD -Force Write-Host -ForegroundColor DarkGray "=========================================================================" Write-Host -ForegroundColor Green "Start-OOBEDeploy" $ProgramDataOSDeploy = "$env:ProgramData\OSDeploy" $JsonPath = "$ProgramDataOSDeploy\OSDeploy.OOBEDeploy.json" #================================================= # Import Json #================================================= if (Test-Path $JsonPath) { Write-Host -ForegroundColor Green "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 #================================================= $PSGalleryIP = (Get-PSRepository -Name PSGallery).InstallationPolicy if ($PSGalleryIP -eq 'Untrusted') { Write-Host -ForegroundColor DarkGray "=========================================================================" Write-Host -ForegroundColor Green "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Set-PSRepository -Name PSGallery -InstallationPolicy Trusted" 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 } #================================================= # AddNetFX3 #================================================= if ($AddNetFX3) { Write-Host -ForegroundColor DarkGray "=========================================================================" Write-Host -ForegroundColor Green "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Invoke-oobeAddNetFX3" $Title = 'Invoke-oobeAddNetFX3' $host.ui.RawUI.WindowTitle = "Running $Title" $host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.size(2000,2000) $AddWindowsCapability = Get-MyWindowsCapability -Match 'NetFX' -Detail foreach ($Item in $AddWindowsCapability) { if ($Item.State -ne 'Install') { Write-Host -ForegroundColor DarkCyan "$($Item.DisplayName)" $Item | Add-WindowsCapability -Online -ErrorAction Ignore | Out-Null } else { Write-Host -ForegroundColor DarkGray "$($Item.DisplayName)" } } } #================================================= # Remove-Appx #================================================= if ($RemoveAppx) { Write-Host -ForegroundColor DarkGray "=========================================================================" Write-Host -ForegroundColor Green "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Invoke-oobeRemoveAppx -RemoveAppx" $Title = 'Invoke-oobeRemoveAppx' $host.ui.RawUI.WindowTitle = "Running $Title" $host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.size(2000,2000) function Remove-AppxOnline { [CmdletBinding()] param ( #Appx Packages selected in GridView will be removed from the Windows Image [System.Management.Automation.SwitchParameter]$GridRemoveAppx, #Appx Provisioned Packages selected in GridView will be removed from the Windows Image [System.Management.Automation.SwitchParameter]$GridRemoveAppxPP, #Appx Packages matching the string will be removed [string[]]$Name ) process { #================================================= # AppxPackage #================================================= if (Get-Command Get-AppxPackage) { if ($GridRemoveAppx.IsPresent) { Get-AppxPackage | Select-Object * | Where-Object {$_.NonRemovable -ne $true} | Out-GridView -PassThru -Title "Select Appx Packages to Remove from Online Windows Image" | ForEach-Object { Write-Verbose "$($_.Name): Removing Appx Package $($_.PackageFullName)" -Verbose Remove-AppPackage -AllUsers -Package $_.PackageFullName -Verbose } } } #================================================= # AppxProvisionedPackage #================================================= if (Get-Command Get-AppxProvisionedPackage) { if ($GridRemoveAppxPP.IsPresent) { Get-AppxProvisionedPackage -Online | Select-Object DisplayName, PackageName | Out-GridView -PassThru -Title "Select Appx Provisioned Packages to Remove from Online Windows Image" | ForEach-Object { Write-Verbose "$($_.DisplayName): Removing Appx Provisioned Package $($_.PackageName)" -Verbose Remove-AppProvisionedPackage -Online -AllUsers -PackageName $_.PackageName } } } #================================================= # RemoveAppx #================================================= foreach ($Item in $Name) { if (Get-Command Get-AppxPackage) { if ((Get-Command Get-AppxPackage).Parameters.ContainsKey('AllUsers')) { Get-AppxPackage -AllUsers | Select-Object * | Where-Object {$_.NonRemovable -ne $true} | Where-Object {$_.Name -Match $Item} | ForEach-Object { Write-Host -ForegroundColor DarkCyan $_.Name if ((Get-Command Remove-AppxPackage).Parameters.ContainsKey('AllUsers')) { Try {Remove-AppxPackage -AllUsers -Package $_.PackageFullName | Out-Null} Catch { #Write-Warning "AllUsers Appx Package $($_.PackageFullName) did not remove successfully" } } else { Try {Remove-AppxPackage -Package $_.PackageFullName | Out-Null} Catch { #Write-Warning "Appx Package $($_.PackageFullName) did not remove successfully" } } } } else { Get-AppxPackage | Select-Object * | Where-Object {$_.NonRemovable -ne $true} | Where-Object {$_.Name -Match $Item} | ForEach-Object { Write-Host -ForegroundColor DarkCyan $_.Name if ((Get-Command Remove-AppxPackage).Parameters.ContainsKey('AllUsers')) { Try {Remove-AppxPackage -AllUsers -Package $_.PackageFullName | Out-Null} Catch { #Write-Warning "AllUsers Appx Package $($_.PackageFullName) did not remove successfully" } } else { Try {Remove-AppxPackage -Package $_.PackageFullName | Out-Null} Catch { #Write-Warning "Appx Package $($_.PackageFullName) did not remove successfully" } } } } } if (Get-Command Get-AppxProvisionedPackage) { Get-AppxProvisionedPackage -Online | Where-Object {$_.DisplayName -Match $Item} | ForEach-Object { Write-Host -ForegroundColor DarkCyan $_.Name if ((Get-Command Remove-AppxProvisionedPackage).Parameters.ContainsKey('AllUsers')) { Try {Remove-AppxProvisionedPackage -Online -AllUsers -PackageName $_.PackageName | Out-Null} Catch { #Write-Warning "AllUsers Appx Provisioned Package $($_.PackageName) did not remove successfully" } } else { Try {Remove-AppxProvisionedPackage -Online -PackageName $_.PackageName | Out-Null} Catch { #Write-Warning "Appx Provisioned Package $($_.PackageName) did not remove successfully" } } } } } } end {} } foreach ($Item in $RemoveAppx) { Remove-AppxOnline -Name $Item } } #================================================= # Update Drivers #================================================= if ($UpdateDrivers) { Write-Host -ForegroundColor DarkGray "=========================================================================" Write-Host -ForegroundColor Green "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Invoke-oobeUpdateDrivers" $Title = 'Invoke-oobeUpdateDrivers' $host.ui.RawUI.WindowTitle = "Running $Title" $host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.size(2000,2000) if (!(Get-Module PSWindowsUpdate -ListAvailable -ErrorAction Ignore)) { try { Write-Host -ForegroundColor DarkCyan "Installing PSWindowsUpdate module" Install-Module PSWindowsUpdate -Force Import-Module PSWindowsUpdate -Force } catch { Write-Warning 'Unable to install PSWindowsUpdate Driver Updates' } } if (Get-Module PSWindowsUpdate -ListAvailable -ErrorAction Ignore) { Install-WindowsUpdate -UpdateType Driver -AcceptAll -IgnoreReboot } } #================================================= # Update Windows #================================================= if ($UpdateWindows) { Write-Host -ForegroundColor DarkGray "=========================================================================" Write-Host -ForegroundColor Cyan "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Invoke-oobeUpdateWindows" $Title = 'Invoke-oobeUpdateWindows' $host.ui.RawUI.WindowTitle = "Running $Title" $host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.size(2000,2000) if (!(Get-Module PSWindowsUpdate -ListAvailable)) { try { Write-Host -ForegroundColor DarkCyan "Installing PSWindowsUpdate module" 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 #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' Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -IgnoreReboot -NotTitle 'Preview' -NotKBArticleID 'KB890830','KB5005463','KB4481252' } } Stop-Transcript