Skip to content

Instantly share code, notes, and snippets.

@Demonslay335
Created February 13, 2020 19:35
Show Gist options
  • Save Demonslay335/419adbae046729d8e33cdd191e281016 to your computer and use it in GitHub Desktop.
Save Demonslay335/419adbae046729d8e33cdd191e281016 to your computer and use it in GitHub Desktop.
FixProfile

Synopsis

Detects and fixes the The User Profile Service failed the logon. User profile cannot be loaded. error on login. This is caused by a registry problem that leaves the profile in a backup state.

Usage

Restart the system to Safe Mode, and log into an administrative account.

Simply run FixProfile.bat. The script will prompt for administrative access using UAC if needed.

@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /F "tokens=1,2 delims=#" %%A IN ('"prompt #$H#$E# & ECHO ON & FOR %%B IN (1) DO REM"') DO SET "DEL=%%A"
:: Elevation does not work in XP
VER | FIND /I "XP" > NUL
IF ERRORLEVEL 1 CALL :CHECK-ELEVATE
:: Process arguments
SET CONTINUE-FLAG=FALSE
FOR %%A IN (%*) DO (
IF /I "%%A"=="/patch" SET CONTINUE-FLAG=TRUE
)
:SEARCH-REGISTRY
SET K_="HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
FOR /F "tokens=* delims=" %%A IN ('"REG QUERY %K_% | FIND /I ".bak""') DO (
SET BAD-PROFILE-KEY=%%A
)
IF "%BAD-PROFILE-KEY%"=="" (
ECHO.No corrupted profiles found!
PAUSE
GOTO :EOF
)
SET GOOD-PROFILE-KEY=%BAD-PROFILE-KEY:.bak=%
FOR /F "tokens=3* delims= " %%A IN ('"REG QUERY "%BAD-PROFILE-KEY%" /V "ProfileImagePath""') DO SET PROFILE-PATH=%%A
FOR /F "tokens=3* delims=\" %%A IN ("%PROFILE-PATH%") DO SET PROFILE-NAME=%%A
ECHO.Found corrupted user: "%PROFILE-NAME%"
IF %CONTINUE-FLAG%==FALSE (
SET /P INPUT=Continue with patching this user? [Y/N]:
) ELSE SET INPUT=Y
IF /I %INPUT%==Y GOTO :FIX-PROFILE
GOTO :EOF
:FIX-PROFILE
ECHO.
ECHO.Copying bad profile key to .ba...
CALL :RENAME-REG-KEY "%GOOD-PROFILE-KEY%" "%GOOD-PROFILE-KEY%.ba"
IF ERRORLEVEL 1 GOTO :REG-ERROR
ECHO.
ECHO.Cleaning good profile key...
CALL :RENAME-REG-KEY "%BAD-PROFILE-KEY%" "%GOOD-PROFILE-KEY%"
IF ERRORLEVEL 1 GOTO :REG-ERROR
ECHO.
ECHO.Fixing backup profile key...
CALL :RENAME-REG-KEY "%GOOD-PROFILE-KEY%.ba" "%BAD-PROFILE-KEY%"
IF ERRORLEVEL 1 GOTO :REG-ERROR
:DONE
ECHO.
ECHO.
ECHO.Repair complete!
ECHO.Please restart to complete.
ECHO.
SET /P INPUT=Would you like to restart now? [Y/N]:
IF /I %INPUT%==Y SHUTDOWN /r /t 0
GOTO :EOF
:REG-ERROR
ECHO.There was an error in interacting with the registry. Unable to continue.
PAUSE
GOTO :EOF
:RENAME-REG-KEY
REG COPY "%~1" "%~2" /s /f
IF ERRORLEVEL 1 (
CALL :BEEP 3
ECHO.
ECHO.Error copying registry key "%~1"
PAUSE
EXIT /B 1
)
ECHO.Copy successful.
ECHO.
ECHO.Deleting old key.
REG DELETE "%~1" /f
IF ERRORLEVEL 1 (
CALL :BEEP 3
ECHO.
ECHO.Error deleting registry key "%~1"
PAUSE
EXIT /B 1
)
EXIT /B 0
::Beep an amount of times
:BEEP
IF "%1"=="" SET %1=3
FOR /L %I IN (1,1,%1) DO ECHO •
GOTO :EOF
:: Check whether running elevated
:: Code created by Michael Murgolo
:CHECK-ELEVATE
CALL :CREATE-ELEVATE-SCRIPTS
:: Check for Mandatory Label\High Mandatory Level
WHOAMI /groups | FIND "S-1-16-12288" > nul
IF "%ERRORLEVEL%" NEQ "0" (
ECHO Not running as elevated user.
ECHO Relaunching Elevated: "%~dpnx0" %*
SET CARET=^^
"%Temp%\elevate.cmd" CMD /C CD /D "%~dp0" !CARET!^& CALL "%~dpnx0" %*
GOTO :EOF
)
IF EXIST %ELEVATE_CMD% DEL %ELEVATE_CMD%
IF EXIST %ELEVATE_VBS% DEL %ELEVATE_VBS%
:: Elevate Sub-Routine
:CREATE-ELEVATE-SCRIPTS
SET ELEVATE_CMD="C:\Windows\Temp\elevate.cmd"
ECHO @SETLOCAL>%ELEVATE_CMD%
ECHO @ECHO OFF>>%ELEVATE_CMD%
ECHO. >>%ELEVATE_CMD%
ECHO :: Pass raw command line agruments and first argument to Elevate.vbs>>%ELEVATE_CMD%
ECHO :: through environment variables.>>%ELEVATE_CMD%
ECHO SET ELEVATE_CMDLINE=%%*>>%ELEVATE_CMD%
ECHO SET ELEVATE_APP=%%1>>%ELEVATE_CMD%
ECHO. >>%ELEVATE_CMD%
ECHO START wscript //nologo "%%~dpn0.vbs" %%*>>%ELEVATE_CMD%
SET ELEVATE_VBS="C:\Windows\Temp\elevate.vbs"
ECHO Set objShell ^= CreateObject^("Shell.Application"^)>%ELEVATE_VBS%
ECHO Set objWshShell ^= WScript.CreateObject^("WScript.Shell"^)>>%ELEVATE_VBS%
ECHO Set objWshProcessEnv ^= objWshShell.Environment^("PROCESS"^)>>%ELEVATE_VBS%
ECHO. >>%ELEVATE_VBS%
ECHO ' Get raw command line agruments and first argument from Elevate.cmd passed>>%ELEVATE_VBS%
ECHO ' in through environment variables.>>%ELEVATE_VBS%
ECHO strCommandLine ^= objWshProcessEnv^("ELEVATE_CMDLINE"^)>>%ELEVATE_VBS%
ECHO strApplication ^= objWshProcessEnv^("ELEVATE_APP"^)>>%ELEVATE_VBS%
ECHO strArguments ^= Right^(strCommandLine, ^(Len^(strCommandLine^) - Len^(strApplication^)^)^)>>%ELEVATE_VBS%
ECHO. >>%ELEVATE_VBS%
ECHO If ^(WScript.Arguments.Count ^>^= 1^) Then>>%ELEVATE_VBS%
ECHO strFlag ^= WScript.Arguments^(0^)>>%ELEVATE_VBS%
ECHO If ^(strFlag ^= "") OR (strFlag="help") OR (strFlag="/h") OR (strFlag="\h") OR (strFlag="-h"^) _>>%ELEVATE_VBS%
ECHO OR ^(strFlag ^= "\?") OR (strFlag = "/?") OR (strFlag = "-?") OR (strFlag="h"^) _>>%ELEVATE_VBS%
ECHO OR ^(strFlag ^= "?"^) Then>>%ELEVATE_VBS%
ECHO DisplayUsage>>%ELEVATE_VBS%
ECHO WScript.Quit>>%ELEVATE_VBS%
ECHO Else>>%ELEVATE_VBS%
ECHO objShell.ShellExecute strApplication, strArguments, "", "runas">>%ELEVATE_VBS%
ECHO End If>>%ELEVATE_VBS%
ECHO Else>>%ELEVATE_VBS%
ECHO DisplayUsage>>%ELEVATE_VBS%
ECHO WScript.Quit>>%ELEVATE_VBS%
ECHO End If>>%ELEVATE_VBS%
ECHO. >>%ELEVATE_VBS%
ECHO. >>%ELEVATE_VBS%
ECHO Sub DisplayUsage>>%ELEVATE_VBS%
ECHO. >>%ELEVATE_VBS%
ECHO WScript.Echo "Elevate - Elevation Command Line Tool for Windows Vista" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO "" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO "Purpose:" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO "--------" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO "To launch applications that prompt for elevation (i.e. Run as Administrator)" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO "from the command line, a script, or the Run box." ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO "" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO "Usage: " ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO "" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO " elevate application <arguments>" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO "" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO "" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO "Sample usage:" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO "" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO " elevate notepad ""C:\Windows\win.ini""" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO "" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO " elevate cmd /k cd ""C:\Program Files""" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO "" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO " elevate powershell -NoExit -Command Set-Location 'C:\Windows'" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO "" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO "" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO "Usage with scripts: When using the elevate command with scripts such as" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO "Windows Script Host or Windows PowerShell scripts, you should specify" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO "the script host executable (i.e., wscript, cscript, powershell) as the " ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO "application." ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO "" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO "Sample usage with scripts:" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO "" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO " elevate wscript ""C:\windows\system32\slmgr.vbs"" –dli" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO "" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO " elevate powershell -NoExit -Command & 'C:\Temp\Test.ps1'" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO "" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO "" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO "The elevate command consists of the following files:" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO "" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO " elevate.cmd" ^& vbCrLf ^& _>>%ELEVATE_VBS%
ECHO " elevate.vbs" ^& vbCrLf>>%ELEVATE_VBS%
ECHO. >>%ELEVATE_VBS%
ECHO End Sub>>%ELEVATE_VBS%
GOTO :EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment