Public/Start-AutopilotOOBE.ps1
function Start-AutopilotOOBE { [CmdletBinding()] param ( [Parameter(ValueFromPipeline = $true)] [string]$CustomProfile, [ValidateSet ( 'GroupTag', 'AddToGroup', 'AssignedUser', 'AssignedComputerName', 'PostAction', 'Assign' )] [string[]]$Disabled, [ValidateSet ( 'GroupTag', 'AddToGroup', 'AssignedUser', 'AssignedComputerName', 'PostAction', 'Assign', 'Register', 'Run', 'Docs' )] [string[]]$Hidden, [string]$AddToGroup, [string[]]$AddToGroupOptions, [switch]$Assign, [string]$AssignedUser, [string]$AssignedUserExample = 'someone@example.com', [string]$AssignedComputerName, [string]$AssignedComputerNameExample = 'Azure AD Join Only', [string]$GroupTag, [string[]]$GroupTagOptions, [ValidateSet ( 'Quit', 'Restart', 'Shutdown', 'Sysprep', 'SysprepReboot', 'SysprepShutdown', 'GeneralizeReboot', 'GeneralizeShutdown' )] [string]$PostAction = 'Quit', [ValidateSet ( 'CommandPrompt', 'PowerShell', 'PowerShellISE', 'WindowsExplorer', 'WindowsSettings', 'NetworkingWireless', 'Restart', 'Shutdown', 'Sysprep', 'SysprepReboot', 'SysprepShutdown', 'SysprepAudit', 'EventViewer', 'GetAutopilotDiagnostics', 'MDMDiag', 'MDMDiagAutopilot', 'MDMDiagAutopilotTPM' )] [string]$Run = 'PowerShell', [string]$Docs, [string]$Title = 'Autopilot Manual Registration' ) #======================================================================= # Header and Json Import #======================================================================= Write-Host -ForegroundColor DarkGray "=========================================================================" Write-Host -ForegroundColor Green "Start-AutopilotOOBE" #======================================================================= # Variables #======================================================================= $JsonPath = "$env:ProgramData\OSDeploy\OSDeploy.AutopilotOOBE.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'))-AutopilotOOBE.log" Start-Transcript -Path (Join-Path "$env:SystemRoot\Temp" $Transcript) -ErrorAction Ignore #======================================================================= # Custom Profile Sample Variables #======================================================================= if ($CustomProfile -eq 'Sample') { $Title = 'Sample Autopilot Registration' $AddToGroup = 'Administrators' $AssignedUserExample = 'someone@osdeploy.com' $AssignedComputerName = 'OSD-' + ((Get-CimInstance -ClassName Win32_BIOS).SerialNumber).Trim() $PostAction = 'Shutdown' $Assign = $true $Run = 'PowerShell' $Docs = 'https://www.osdeploy.com/' $Hidden = 'GroupTag' } #======================================================================= # Custom Profile #======================================================================= if ($CustomProfile) { Write-Host -ForegroundColor DarkGray "=========================================================================" Write-Host -ForegroundColor Cyan "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Loading AutopilotOOBE Custom Profile $CustomProfile" $CustomProfileJson = Get-ChildItem "$($MyInvocation.MyCommand.Module.ModuleBase)\CustomProfile" *.json | Where-Object {$_.BaseName -eq $CustomProfile} | Select-Object -First 1 if ($CustomProfileJson) { Write-Host -ForegroundColor DarkGray "Saving Module CustomProfile to $JsonPath" if (!(Test-Path "$env:ProgramData\OSDeploy")) {New-Item "$env:ProgramData\OSDeploy" -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" $ImportAutopilotOOBE = @() $ImportAutopilotOOBE = Get-Content -Raw -Path $JsonPath | ConvertFrom-Json $ImportAutopilotOOBE.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 } } #======================================================================= # PSGallery #======================================================================= $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" Set-PSRepository -Name PSGallery -InstallationPolicy Trusted } #======================================================================= # Initialize Global Variable #======================================================================= $Global:AutopilotOOBE = [ordered]@{ AddToGroup = $AddToGroup AddToGroupOptions = $AddToGroupOptions Assign = $Assign AssignedUser = $AssignedUser AssignedUserExample = $AssignedUserExample AssignedComputerName = $AssignedComputerName AssignedComputerNameExample = $AssignedComputerNameExample Disabled = $Disabled GroupTag = $GroupTag GroupTagOptions = $GroupTagOptions Hidden = $Hidden PostAction = $PostAction Run = $Run Docs = $Docs Title = $Title } Write-Host -ForegroundColor DarkGray "Exporting Configuration $env:Temp\OSDeploy.AutopilotOOBE.json" @($Global:AutopilotOOBE.Keys) | ForEach-Object { if (-not $Global:AutopilotOOBE[$_]) { $Global:AutopilotOOBE.Remove($_) } } $Global:AutopilotOOBE | ConvertTo-Json | Out-File "$env:Temp\OSDeploy.AutopilotOOBE.json" -Force #======================================================================= # Date Time #======================================================================= Write-Host -ForegroundColor DarkGray "=========================================================================" Write-Host -ForegroundColor Cyan "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Verify Date and Time" Write-Host -ForegroundColor DarkCyan 'Make sure the Time is set properly in the System BIOS as this can cause issues' Get-Date Get-TimeZone #======================================================================= # Test-AutopilotNetwork #======================================================================= Write-Host -ForegroundColor DarkGray "=========================================================================" Write-Host -ForegroundColor Cyan "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Test-AutopilotNetwork" Write-Host -ForegroundColor DarkCyan 'Required Autopilot network addresses are being tested in a minimized window' Write-Host -ForegroundColor DarkCyan 'Use Alt+Tab to view progress' Start-Process PowerShell.exe -WindowStyle Minimized -ArgumentList "-NoExit -Command Test-AutopilotNetwork" #======================================================================= # Test-AutopilotRegistry #======================================================================= Write-Host -ForegroundColor DarkGray "=========================================================================" Write-Host -ForegroundColor Cyan "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Test-AutopilotRegistry" Write-Host -ForegroundColor DarkCyan 'Gathering Autopilot Registration information from the Registry' $Global:RegAutoPilot = Get-ItemProperty 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Provisioning\Diagnostics\AutoPilot' Write-Host -ForegroundColor Gray "IsAutoPilotDisabled: $($Global:RegAutoPilot.IsAutoPilotDisabled)" Write-Host -ForegroundColor Gray "CloudAssignedForcedEnrollment: $($Global:RegAutoPilot.CloudAssignedForcedEnrollment)" Write-Host -ForegroundColor Gray "CloudAssignedTenantDomain: $($Global:RegAutoPilot.CloudAssignedTenantDomain)" Write-Host -ForegroundColor Gray "CloudAssignedTenantId: $($Global:RegAutoPilot.CloudAssignedTenantId)" Write-Host -ForegroundColor Gray "CloudAssignedTenantUpn: $($Global:RegAutoPilot.CloudAssignedTenantUpn)" Write-Host -ForegroundColor Gray "CloudAssignedLanguage: $($Global:RegAutoPilot.CloudAssignedLanguage)" if ($Global:RegAutoPilot.CloudAssignedForcedEnrollment -eq 1) { Write-Host -ForegroundColor Gray "TenantId: $($Global:RegAutoPilot.TenantId)" Write-Host -ForegroundColor Gray "CloudAssignedMdmId: $($Global:RegAutoPilot.CloudAssignedMdmId)" Write-Host -ForegroundColor Gray "AutopilotServiceCorrelationId: $($Global:RegAutoPilot.AutopilotServiceCorrelationId)" Write-Host -ForegroundColor Gray "CloudAssignedOobeConfig: $($Global:RegAutoPilot.CloudAssignedOobeConfig)" Write-Host -ForegroundColor Gray "CloudAssignedTelemetryLevel: $($Global:RegAutoPilot.CloudAssignedTelemetryLevel)" Write-Host -ForegroundColor Gray "IsDevicePersonalized: $($Global:RegAutoPilot.IsDevicePersonalized)" Write-Host -ForegroundColor Gray "SetTelemetryLevel_Succeeded_With_Level: $($Global:RegAutoPilot.SetTelemetryLevel_Succeeded_With_Level)" Write-Host -ForegroundColor Gray "IsForcedEnrollmentEnabled: $($Global:RegAutoPilot.IsForcedEnrollmentEnabled)" Write-Host -ForegroundColor Green "This device has already been Autopilot Registered. Registration will not be enabled" Start-Sleep -Seconds 2 $Title = 'Autopilot Registration Information' $Disabled = 'GroupTag','AddToGroup','AssignedUser','AssignedComputerName','PostAction','Assign' $Hidden = 'GroupTag','AddToGroup','AssignedUser','AssignedComputerName','PostAction','Assign','Register' $Run = 'MDMDiagAutopilotTPM' } #======================================================================= # Launch #======================================================================= Write-Host -ForegroundColor DarkGray "=========================================================================" Write-Host -ForegroundColor Cyan "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) Starting AutopilotOOBE GUI" Start-Sleep -Seconds 2 & "$($MyInvocation.MyCommand.Module.ModuleBase)\Forms\Join-AutopilotOOBE.ps1" #======================================================================= } |