|
| 1 | +# script to fix SCCM W10 Upgrade TS |
| 2 | +# more info at http://www.thesccm.com/fix-windows-10-in-place-upgrade-task-sequence-infinity-restart-loop/ |
| 3 | + |
| 4 | +# Error pref needed for try/catch blocks to work |
| 5 | +$ErrorActionPreference = "stop" |
| 6 | + |
| 7 | +$logPath = "C:\Logs" |
| 8 | +$LogFile = $logPath + "\FixBootLoop.log" |
| 9 | + |
| 10 | +$registryPath = 'HKLM:\SYSTEM\Setup' |
| 11 | +$Key1Name = 'CmdLine' |
| 12 | +$Key1Value = '' |
| 13 | +$Key2Name = 'SetupType' |
| 14 | + |
| 15 | +function Log ($message) { |
| 16 | + "$([DateTime]::Now)] $message" | Out-File $LogFile -Append -Force |
| 17 | +} |
| 18 | +log "Fix-BootLoop script started" |
| 19 | +function Test-RegistryValue { |
| 20 | + |
| 21 | + [string]$Path, |
| 22 | + [string]$Value |
| 23 | + |
| 24 | + try { |
| 25 | + Get-ItemProperty -Path $Path -Name $Value -ErrorAction Stop | Out-Null |
| 26 | + return $true |
| 27 | + } |
| 28 | + |
| 29 | + catch { |
| 30 | + return $false |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +# check to see if CmdLine registry key exists |
| 35 | +If (Test-RegistryValue -Path $registryPath -Value $Key1Name) { |
| 36 | + # key exists, so get its value |
| 37 | + |
| 38 | + Try |
| 39 | + { |
| 40 | + $Key1Value = Get-ItemProperty -Path $registryPath | Select-Object -ExpandProperty "$Key1Name" |
| 41 | + } |
| 42 | + Catch |
| 43 | + { |
| 44 | + log "$registryPath\$Key1Name doesn't exist. Error: $($_.Exception.Message)" |
| 45 | + } |
| 46 | + |
| 47 | + # check if the value of CmdLine key is empty. Being empty is ok as long as SetupType is eq to 2 |
| 48 | + If (!($Key1Value -eq "")) { |
| 49 | + # ok, the Key value is not empty. B/c of this SCCM issue, even if C:\Windows\SMSTSPostUpgrade is a valid path right now, it won't be by the end of the TS |
| 50 | + # That folder gets prematurely removed. So always set C:\Windows\Setup\Scripts as the correct CmdLine path. |
| 51 | + log "CmdLine key value is currently $Key1Value" |
| 52 | + $NewValue = 'C:\Windows\Setup\Scripts\setupcomplete.cmd' |
| 53 | + If (Test-Path $NewValue) { |
| 54 | + # NewValue path exists, so let's use it |
| 55 | + log "Setting CmdLine reg key to $NewValue" |
| 56 | + Try |
| 57 | + { |
| 58 | + Set-ItemProperty -Path $registryPath -Name $Key1Name -Value $NewValue |
| 59 | + } |
| 60 | + Catch |
| 61 | + { |
| 62 | + log "Could not Set-Itemproperty on Path $registryPath with Key $Key1Name and Value $NewValue. Error: $($_.Exception.Message)" |
| 63 | + } |
| 64 | + |
| 65 | + } |
| 66 | + Else { |
| 67 | + # NewValue path doesn't exist, so we've got to just clear out the registry entries |
| 68 | + # that are causing the issue |
| 69 | + # Set CmdLine reg_sz key to empty string |
| 70 | + log "CmdLine reg key needs to be updated but $NewValue doesn't exist, so we're just setting CmdLine to an empty string and SetupType to 0" |
| 71 | + Try |
| 72 | + { |
| 73 | + Set-ItemProperty -Path $registryPath -Name $Key1Name -Value '' |
| 74 | + } |
| 75 | + Catch |
| 76 | + { |
| 77 | + log "Could not Set-Itemproperty on Path $registryPath with Key $Key1Name and empty string value. Error: $($_.Exception.Message)" |
| 78 | + } |
| 79 | + # Set SetupType reg_dword key to 0 |
| 80 | + Try |
| 81 | + { |
| 82 | + Set-ItemProperty -Path $registryPath -Name $Key2Name -Value 0 |
| 83 | + } |
| 84 | + Catch |
| 85 | + { |
| 86 | + log "Could not Set-Itemproperty on Path $registryPath with Key $Key2Name and Value of 0. Error: $($_.Exception.Message)" |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + ElseIf ((Get-ItemProperty -Path $registryPath | Select-Object -ExpandProperty $Key2Name) -ne '0') { |
| 91 | + # this means that CmdLine reg_sz is empty (which is fine), but SetupType is !=0 (not fine) |
| 92 | + log "The CmdLine key was empty but the SetupType was not 0. Setting SetupType dword back to 0" |
| 93 | + Try |
| 94 | + { |
| 95 | + Set-ItemProperty -Path $registryPath -Name $Key2Name -Value 0 |
| 96 | + } |
| 97 | + Catch |
| 98 | + { |
| 99 | + log "Could not Set-Itemproperty on Path $registryPath with Key $Key2Name and Value of 0. Error: $($_.Exception.Message)" |
| 100 | + } |
| 101 | + } |
| 102 | + Else { |
| 103 | + log "No incorrect registry keys detected. Exiting with no changes" |
| 104 | + } |
| 105 | +} |
| 106 | +Else { |
| 107 | + log "$registryPath\$Key1Name didn't exist. Exiting with no changes" |
| 108 | +} |
| 109 | +log "Finished" |
0 commit comments