Advertisement
pillbox1234567

Untitled

Feb 3rd, 2020
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 5.66 KB | None | 0 0
  1. @echo off
  2. cls
  3. setlocal enabledelayedexpansion
  4. ::Set some variables
  5.     ::Plaintext name of the program to be installed.
  6.     set appname=
  7.     ::Base directory for the script (the folder the script is in).
  8.     set root=
  9.     ::Path to elevate.exe
  10.     set elevate=\\techshare01.pisd.local\tech\bin\elevate.exe
  11. ::End
  12. ::Title the window
  13.     title %appname% Installation Script
  14.     if [%1]==[elevated] title %appname% Installation Script (elevated)
  15. ::End
  16. ::Check for elevation
  17.     if [%1]==[elevated] goto RUN
  18.     ::If not elevated already, elevate the process
  19.     echo Elevating the session to use administrator rights. DO NOT CLOSE THIS WINDOW!
  20.     %elevate% %0 elevated
  21.     goto END
  22. ::End
  23. :RUN
  24.     ::Get list of logged in users to be appended to the list of skipped users
  25.     set getloggedinusers=powershell -command "$tmp=((query user).Trim() -replace '\s{2,}', ',').Trim() -replace '>',''|convertfrom-csv;$users='';foreach($t in $tmp){$users+=$t.username+','};$users.trimend(',')"
  26.     FOR /F "tokens=*" %%g IN ('%getloggedinusers%') do (SET loggedinusers=%%g)
  27.  
  28.     @ECHO OFF
  29.  
  30.     :: --------------------------------------
  31.     ::
  32.     :: Windows 7 User Profile Cleaning Script
  33.     :: Version 3.1
  34.     ::
  35.     :: Written by Mike Stone
  36.     :: mikestone@gmail.com
  37.     :: http://mstoneblog.wordpress.com
  38.     ::
  39.     :: --------------------------------------
  40.     ::
  41.     :: Welcome!  This script is designed to automate the process of flushing
  42.     :: user profiles within Windows 7, while at the same time preserving
  43.     :: profiles of your choosing, including domain users.
  44.     ::
  45.     :: This script is written as an example of wanting all domain users wiped
  46.     :: except for the one called "pctest".
  47.     ::
  48.     :: Portions of the script that will require manual edits will be preceded
  49.     :: by instructions with these "double colon" comment marks.
  50.     ::
  51.     :: Please let me know how well (or not well) this works for you or any
  52.     :: features you can think of that could be added.
  53.     ::
  54.     :: --------------------------------------
  55.     color 0C
  56.     title Windows 7 User Profile Cleaning
  57.     echo WARNING: This script will delete ALL non-tech user profiles!
  58.     cls
  59.     color 03
  60.     echo USER PROFILE CLEANUP INITIATED
  61.     :: ----------
  62.     :: Add any users you wish to exclude from the wipe to the "userpreserve"
  63.     :: line below and separate them by commas.  Be careful - these are
  64.     :: case-sensitive.
  65.     :: ----------
  66.  
  67.     :USERPRESERVE
  68.     set userpreserve="Administrator,All Users,UpdatusUser,Default,Default User,Public,%username%,%loggedinusers%"
  69.     echo %userpreserve%
  70.     FOR /f "tokens=*" %%a IN ('reg query "hklm\software\microsoft\windows nt\currentversion\profilelist"^|find /i "s-1-5-21"') DO CALL :REGCHECK "%%a"
  71.     GOTO VERIFY
  72.  
  73.     :REGCHECK
  74.     set SPACECHECK=
  75.     FOR /f "tokens=3,4" %%b in ('reg query %1 /v ProfileImagePath') DO SET USERREGPATH=%%b %%c
  76.     FOR /f "tokens=2" %%d in ('echo %USERREGPATH%') DO SET SPACECHECK=%%d
  77.     IF ["%SPACECHECK%"]==[""] GOTO REGCHECK2
  78.     GOTO USERCHECK
  79.  
  80.     :REGCHECK2
  81.     FOR /f "tokens=3" %%g in ('reg query %1 /v ProfileImagePath') DO SET USERREGPATH=%%g
  82.     GOTO USERCHECK
  83.  
  84.     :USERCHECK
  85.     FOR /f "tokens=3 delims=\" %%e in ('echo %USERREGPATH%') DO SET USERREG=%%e
  86.     FOR /f "tokens=1 delims=." %%f IN ('echo %USERREG%') DO SET USERREGPARSE=%%f
  87.     ECHO %USERPRESERVE%|find /I "%USERREGPARSE%" > NUL
  88.     IF ERRORLEVEL=1 GOTO CLEAN
  89.     IF ERRORLEVEL=0 GOTO SKIP
  90.  
  91.     :SKIP
  92.     ECHO Skipping user clean for %USERREG%
  93.     GOTO :EOF
  94.  
  95.     :CLEAN
  96.     ECHO Cleaning user profile for %USERREG%
  97.     rmdir "C:\Users\%USERREG%" /s /q > NUL
  98.     ECHO Cleaning user registry for %USERREG%
  99.     reg delete %1 /f
  100.     IF EXIST "C:\Users\%USERREG%" GOTO RETRYCLEAN1
  101.     GOTO :EOF
  102.  
  103.     :RETRYCLEAN1
  104.     ECHO Retrying clean of user profile %USERREG%
  105.     rmdir "C:\Users\%USERREG%" /s /q > NUL
  106.     IF EXIST "C:\Users\%USERREG%" GOTO RETRYCLEAN2
  107.     GOTO :EOF
  108.  
  109.     :RETRYCLEAN2
  110.     ECHO Retrying clean of user profile %USERREG%
  111.     rmdir "C:\Users\%USERREG%" /s /q > NUL
  112.     GOTO :EOF
  113.  
  114.     :VERIFY
  115.     FOR /f "tokens=*" %%g IN ('reg query "hklm\software\microsoft\windows nt\currentversion\profilelist"^|find /i "s-1-5-21"') DO CALL :REGCHECKV "%%g"
  116.     GOTO REPORT
  117.  
  118.     :REGCHECKV
  119.     set SPACECHECKV=
  120.     FOR /f "tokens=3,4" %%h in ('reg query %1 /v ProfileImagePath') DO SET USERREGPATHV=%%h %%i
  121.     FOR /f "tokens=2" %%j in ('echo %USERREGPATHV%') DO SET SPACECHECKV=%%j
  122.     IF ["%SPACECHECKV%"]==[""] GOTO REGCHECKV2
  123.     GOTO USERCHECKV
  124.  
  125.     :REGCHECKV2
  126.     FOR /f "tokens=3" %%k in ('reg query %1 /v ProfileImagePath') DO SET USERREGPATHV=%%k
  127.     GOTO USERCHECKV
  128.  
  129.     :USERCHECKV
  130.     FOR /f "tokens=3 delims=\" %%l in ('echo %USERREGPATHV%') DO SET USERREGV=%%l
  131.     FOR /f "tokens=1 delims=." %%m IN ('echo %USERREGV%') DO SET USERREGPARSEV=%%m
  132.     ECHO %USERPRESERVE%|find /I "%USERREGPARSEV%" > NUL
  133.     IF ERRORLEVEL=1 GOTO VERIFYERROR
  134.     IF ERRORLEVEL=0 GOTO :EOF
  135.  
  136.     :VERIFYERROR
  137.     SET USERERROR=YES
  138.     GOTO :EOF
  139.  
  140.     :REPORT
  141.     IF [%USERERROR%]==[YES] (
  142.             set RESULT=FAILURE
  143.     )       ELSE (
  144.             set RESULT=SUCCESS
  145.     )
  146.  
  147.     :: ----------
  148.     :: This is fairly optional - it's just something I added so
  149.     :: that I could keep an eye on the labs remotely to make
  150.     :: sure there weren't masses of critical errors with the
  151.     :: script failing.
  152.     ::
  153.     :: If you don't want it, just comment-out or remove the
  154.     :: "net use" line.
  155.     ::
  156.     :: If you do want it, then make the necessary modifications
  157.     :: to the net use to map an appropriate sharepoint.
  158.     :: ----------
  159.  
  160.     net use t: \\server\share
  161.  
  162.     IF EXIST "t:\labreport.txt" (
  163.         GOTO REPORTGEN
  164.     ) ELSE (
  165.         GOTO EXIT
  166.     )
  167.  
  168.     :REPORTGEN
  169.     FOR /F "tokens=*" %%n in ('echo %date:~10,4%-%date:~4,2%-%date:~7,2% %time:~0,2%-%time:~3,2%-%time:~6,2%') DO SET TDATETIME=%%n
  170.     FOR /F "tokens=14" %%o in ('ipconfig^|find "IPv4 Address"') DO SET IPNUMBER=%%o
  171.     ECHO. %RESULT%  %COMPUTERNAME%  %IPNUMBER%  %TDATETIME%>>"t:\labreport.txt"
  172.     net use t: /delete
  173.     GOTO EXIT
  174.  
  175.     :EXIT
  176.  
  177.     :EOF
  178.  
  179.  
  180. :END
  181. endlocal
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement