Tested on Windows 2000, XP SP3, 2003, Vista SP2, Windows 7 SP0 x64.
There is one parameter to be given - name of the variable to which the SID will be written (name of the variable can not be one of these mentioned being used by the code).
@echo off :: works globally; uses vars: sid; ocparam; buff; skip; var0; retv :: contains GTweak procedures: OsCheck (modified); RegQueryValue; IsReadable; IsRunWithElevatedRights :: requires reg.exe; find.exe and OS >= Windows 2000 call:GetCurrentUserSID securityid echo CurrentUserSID = [%securityid%] pause exit :GetCurrentUserSID if not exist "%windir%\system32\reg.exe" (exit/b1) reg add "HKLM\SOFTWARE\Microsoft\WBEM" /v "" /d "" /f>nul 2>&1||exit/b1 if not exist "%windir%\system32\find.exe" (exit/b1) set sid= call:OsCheck set buff=%RANDOM%%RANDOM%%RANDOM% reg add "HKCU" /v "removeme_markerval" /t "REG_SZ" /d "%buff%" /f>nul 2>&1||exit/b1 for /f "tokens=2* delims=\" %%G in ('reg query HKU') do (call:checkifsid %%G&&goto gcusid_0) :gcusid_0 set %1=%sid% for %%A in (buff sid skip) do set %%A= exit/b0 :checkifsid set var0=%* if not "%var0:~0,1%"=="S" (goto checkifsid_out) if /i "%var0:~-8%"=="_Classes" (goto checkifsid_out) for /f "tokens=1-8 delims=-" %%G in ('echo %var0%') do ( if not "%%G"=="" ( if not "%%H"=="" ( if not "%%I"=="" ( if not "%%J"=="" ( if not "%%K"=="" ( if not "%%L"=="" ( if not "%%M"=="" ( if not "%%N"=="" ( call:testsid ) ) ) ) ) ) ) ) ) :checkifsid_out set var0= if defined sid (exit/b0) else (exit/b1) :RegQueryValue call:IsReadable %1 %2||exit/b1 for /f "%skip%" %%G in ('reg query %1 /v %2') do ( set retv=%%G ) exit/b0 :IsReadable reg query %1 /v %2>nul 2>&1&&exit/b0 exit/b1 :testsid call:RegQueryValue "HKU\%var0%" "removeme_markerval" if "%retv%"=="%buff%" ( reg delete "HKU\%var0%" /v "removeme_markerval" /f>nul 2>&1 set sid=%var0% ) set retv= exit/b0 :OsCheck for /f "tokens=3*" %%G in ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v "ProductName"^|find "ProductName"') do set ocparam=%%G %%H echo.%ocparam%|find "Windows 7">nul 2>&1&&set skip= tokens=3&&exit/b0 echo.%ocparam%|find "XP">nul 2>&1&&set skip=skip=4 tokens=3&&exit/b0 echo.%ocparam%|find "Vista">nul 2>&1&&set skip= tokens=3&&exit/b0 echo.%ocparam%|find "2003">nul 2>&1&&set skip= tokens=3&&exit/b0 echo.%ocparam%|find "2000">nul 2>&1&&set skip=skip=4 tokens=3&&exit/b0 echo.%ocparam%|find "2008">nul 2>&1&&set skip= tokens=3&&exit/b0 set skip= tokens=3 set ocparam= exit/b0
Update (October the 6th, 2013): I enhanced the script. You needn't worry about variables - now the script works locally. Tested/working in Windows 2000, Windows XP, Windows 7 and Windows 8.
@echo off :: requires reg.exe and OS >= Windows 2000 call:GetCurrentUserSID securityid if %errorlevel%==0 (echo CurrentUserSID = [%securityid%]) if %errorlevel%==1 (echo Required component missing.) if %errorlevel%==2 (echo Required registry writes could not be performed or insufficient privileges.) if %errorlevel%==3 (echo Unknown error, SID not found?) pause goto:eof :GetCurrentUserSID setlocal if not exist "%windir%\system32\reg.exe" (set err=1&goto gcusid_endproc) reg add "HKLM\SOFTWARE\Microsoft\WBEM" /v "" /d "" /f>nul 2>&1||set err=2&&goto gcusid_endproc set buff=%RANDOM%%RANDOM%%RANDOM% reg add "HKCU" /v "removeme_markerval" /t "REG_SZ" /d "%buff%" /f>nul 2>&1||set err=2&&goto gcusid_endproc for /f "tokens=2* delims=\" %%G in ('reg query HKU') do (if not defined sid_found (call:checkifsid %%G)) if defined sid (set err=0) else (set err=3) :gcusid_endproc endlocal&set %1=%sid%&exit/b%err% :checkifsid set var0=%* if "%var0:~0,1%"=="S" (if not "%var0:~-8%"=="_Classes" (for /f "tokens=1-8 delims=-" %%G in ("%var0%") do (if not "%%G"=="" (if not "%%H"=="" (if not "%%I"=="" (if not "%%J"=="" (if not "%%K"=="" (if not "%%L"=="" (if not "%%M"=="" (if not "%%N"=="" (for /f "tokens=3" %%O in ('reg query "HKU\%var0%" /v "removeme_markerval"') do (if "%%O"=="%buff%" (set sid=%var0%&set sid_found=1® delete "HKU\%var0%" /v "removeme_markerval" /f>nul 2>&1))))))))))))) exit/b0
No comments:
Post a Comment