Guest User

OCPN v2 Dependancy: DeleteProfiles.vbs

a guest
Jun 25th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '***************************************************************************
  2. '*
  3. '*  Delete Profiles script written by Joe Shonk ( [email protected] )
  4. '*  Version 1.9
  5. '*
  6. '*  Syntax: cscript.exe DeleteProfiles.vbs [/H] [/E | /I <PROFILENAME>] [/C] [/R]
  7. '*              [/D <DAYS>] [/L <FILENAME>] [/V]
  8. '*
  9. '*  This script is provided as-is, no warrenty is provided or implied.
  10. '*  The author is NOT responsible for any damages or data loss that may occur
  11. '*  through the use of this script.  Always test, test, test before
  12. '*  rolling anything into a production environment.
  13. '*
  14. '*  This script is free to use for both personal and business use, however,
  15. '*  it may not be sold or included as part of a package that is for sale.
  16. '*
  17. '*  A Service Provider may include this script as part of their service
  18. '*  offering/best practices provided they only charge for their time
  19. '*  to implement and support.
  20. '*
  21. '*  For distribution and updates go to: http://www.theshonkproject.com
  22. '*
  23. '***************************************************************************
  24. On Error Resume Next
  25.  
  26. Const DeleteReadOnly = TRUE
  27. Const HKEY_LOCAL_MACHINE = &H80000002
  28. Const SIDExclusionList = "|S-1-5-18|S-1-5-19|S-1-5-20|"
  29.  
  30. '***************************************************************************
  31. '*  To add your own profiles to the exclusion list simply add the
  32. '*  account to the end of the strProfileExclusionList.  Note: Each account
  33. '*  is delimited by a | (pipe) and is all lowercase
  34. '*
  35.  
  36. Dim strProfileExclusionList
  37. Dim strComputer, strLogFileName, strDocAndSettingsLocation
  38. Dim strKeyPath, arrValueNames, arrValueTypes, arrSubKeys
  39. Dim i, strHiveExclusionList, strHiveOpenSkipped, strHiveValue
  40. Dim strSubKey, strGuid, strUserName, strProfileImagePath
  41. Dim dwProfileExclusion, dwSIDExclusion, dwHiveOpenExclusion
  42. Dim flgLogFile, flgWriteConsole, flgVerboseLog, flgAllowExecute, flgHelp
  43. Dim dwArgCount, strNextArg, strCurrentArg, flgCustomExclusions, flgCustomExMatch
  44. Dim strCustomExclusions, dateCurrentTime, flgCustomInclusions, strCustomInclusions
  45. Dim flgCustomInMatch, flgExDateMatch, flgDateExclusion, dwDateExclusion
  46.  
  47. strComputer = "."
  48. strProfileExclusionList = "|administrator|all users|default user|localservice|networkservice|public|ctx_smauser|ctx_cpuuser|ctx_cpsvcuser|ctx_streamingsvc|ctx_configmgr|"
  49.  
  50. Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
  51. Set objFSO = CreateObject("Scripting.FileSystemObject")
  52. Set objArgs = WScript.Arguments
  53.  
  54. strLogFileName = ""
  55. dateCurrentTime = now()
  56. dwArgCount = 0
  57. flgHelp = False
  58. flgLogFile = False
  59. flgCustomExclusions = False
  60. flgWriteConsole = False
  61. flgVerboseLog = False
  62. flgAllowExecute = True
  63.  
  64. dwArgCount = objArgs.Count
  65. For i = 0 to dwArgCount - 1
  66.     strCurrentArg = lcase(objArgs(i))
  67.     Select Case strCurrentArg
  68.         Case "-v", "-verbose", "/v", "/verbose"
  69.             flgVerboseLog = True
  70.         Case "-c", "-console", "/c", "/console"
  71.             flgWriteConsole = True
  72.         Case "-r", "-readonly", "-read", "/r", "/readonly", "/read"
  73.             flgAllowExecute = False
  74.             flgWriteConsole = True
  75.         Case "-l", "-log", "/l", "/log"
  76.             If i < (dwArgCount - 1) then
  77.                 strNextArg = lcase(objArgs(i + 1))
  78.                 If (left(strNextArg, 1) <> "/") and (left(strNextArg, 1) <> "-") then
  79.                     flgLogFile = True
  80.                     strLogFileName = strNextArg
  81.                     i = i + 1
  82.                 Else
  83.                     wscript.echo "Warning: Log Switch Used but No Log Filename Specified."
  84.                 End if
  85.             Else
  86.                 wscript.echo "Warning: Log Switch Used but No Log Filename Specified."
  87.             End if
  88.         Case "-e", "-exclude", "/e", "/exclude"
  89.             If i < (dwArgCount - 1) then
  90.                 strNextArg = lcase(objArgs(i + 1))
  91.                 If (left(strNextArg, 1) <> "/") and (left(strNextArg, 1) <> "-") then
  92.                     flgCustomExclusions = True
  93.                     strCustomExclusions = replace(strNextArg, ",", "|", 1)
  94.                     i = i + 1
  95.                 Else
  96.                     wscript.echo "Error: Exclude Switch Used but no Argument Specified."
  97.                     flgHelp = True
  98.                 End if
  99.             Else
  100.                 wscript.echo "Error: Exclude Switch Used but no Argument Specified."
  101.                 flgHelp = True
  102.             End if
  103.         Case "-i", "-include", "/i", "/include"
  104.             If i < (dwArgCount - 1) then
  105.                 strNextArg = lcase(objArgs(i + 1))
  106.                 If (left(strNextArg, 1) <> "/") and (left(strNextArg, 1) <> "-") then
  107.                     flgCustomInclusions = True
  108.                     strCustomInclusions = replace(strNextArg, ",", "|", 1)
  109.                     i = i + 1
  110.                 Else
  111.                     wscript.echo "Error: Include Switch Used but no Argument Specified."
  112.                     flgHelp = True
  113.                 End if
  114.             Else
  115.                 wscript.echo "Error: Include Switch Used but no Argument Specified."
  116.                 flgHelp = True
  117.             End if
  118.         Case "-d", "-days", "-day", "/d", "/days", "/day"
  119.             If i < (dwArgCount - 1) then
  120.                 strNextArg = lcase(objArgs(i + 1))
  121.                 If (left(strNextArg, 1) <> "/") and (left(strNextArg, 1) <> "-") then
  122.                     dwDateExclusion = cdbl(strNextArg)
  123.                     i = i + 1
  124.                     If (dwDateExclusion > 0) and (dwDateExclusion < 999999) then
  125.                         flgDateExclusion = True
  126.                     Else
  127.                         flgHelp = True
  128.                         wscript.echo "Error: Invalid Number of Days Specified."
  129.                     End if
  130.                 Else
  131.                     wscript.echo "Error: Day Switch Used but no Argument Specified."
  132.                     flgHelp = True
  133.                 End if
  134.             Else
  135.                 wscript.echo "Error: Day Switch Used but no Argument Specified."
  136.                 flgHelp = True
  137.             End if
  138.         Case "-h", "-help", "/h", "/help", "-?", "/?"
  139.             flgHelp = True
  140.         Case Else
  141.             wscript.echo "Unrecognized option: " & objArgs(i)
  142.             flgHelp = True
  143.     End Select
  144. next
  145. If flgCustomExclusions and flgCustomInclusions then
  146.     wscript.echo "Error: Cannot Specify a Custom Exclusion List with a Custom Inclusion List."
  147.     flgHelp = True
  148. End if
  149.  
  150. If flgHelp then
  151.     wscript.echo "Help"
  152.     wscript.echo ""
  153.     wscript.echo "DeleteProfiles.vbs - v1.9"
  154.     wscript.echo "-------------------------"
  155.     wscript.echo ""
  156.     wscript.echo "cscript.exe DeleteProfiles.vbs [/H] [/E | /I <PROFILENAME>] [/C] [/R]"
  157.     wscript.echo "    [/D <DAYS>] [/L <FILENAME>] [/V]"
  158.     wscript.echo ""
  159.     wscript.echo "Command Line Options:"
  160.     wscript.echo "  /C            : Write Log to the Console"
  161.     wscript.echo "  /D <Days>     : Delete Profiles Older than x Days"
  162.     wscript.echo "  /E <Profile>  : Exclude Profiles from Deletion"
  163.     wscript.echo "                :   Wildcard * Supported. Use ',' or '|' as a Delimiter for"
  164.     wscript.echo "                :   Multiple Entries. No Spaces Between Entries."
  165.     wscript.echo "  /I <Profile>  : Only Delete Included Profiles (Wildcard * Supported)"
  166.     wscript.echo "                :   Wildcard * Supported. Use ',' or '|' as a Delimiter for"
  167.     wscript.echo "                :   Multiple Entries. No Spaces Between Entries."
  168.     wscript.echo "  /L <FileName> : Create Log File"
  169.     wscript.echo "  /H            : Help (This Screen)"
  170.     wscript.echo "  /R            : Run Script in Read Only Mode (No System Changes)"
  171.     wscript.echo "  /V            : Verbose Logging"
  172.     wscript.echo ""
  173.     wscript.quit
  174. End if
  175.  
  176. If flgLogFile then Set objLogFile = objFSO.CreateTextFile(strLogFileName)
  177.  
  178. WriteHeader
  179.  
  180. '**********************************************************************************
  181. '*   Enumerate a list of loaded Registry Hives.  Delimited by the | character
  182. strHiveExclusionList = "|"
  183. strHiveOpenSkipped = "|"
  184. strKeyPath = "SYSTEM\CurrentControlSet\Control\hivelist"
  185. objReg.EnumValues HKEY_LOCAL_MACHINE, strKeyPath, arrValueNames, arrValueTypes
  186. For i=0 To UBound(arrValueNames)
  187.     strHiveValue = trim(arrValueNames(i))
  188.     strHiveExclusionList = strHiveExclusionList & Right(strHiveValue, len(strHiveValue) - instrrev(strHiveValue, "\")) & "|"
  189. Next
  190.  
  191. '**********************************************************************************
  192. '*   Enumerate a list of known profiles from the registry
  193. strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
  194. objReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys
  195.  
  196. '**********************************************************************************
  197. '*   Parse through the Profile list and Delete the Registry entries and Files associated to the Profile
  198. '*   Provided the profile is not listed in an Exclusion list
  199. WriteLog "Checking Profile List"
  200. WriteLog "---------------------"
  201. If NOT flgAllowExecute then WriteLog "READ ONLY MODE. No changes made."
  202. If flgDateExclusion then WriteLog "Purge Profiles Older than " & dwDateExclusion & " Days."
  203. WriteLog ""
  204.  
  205. For Each subkey In arrSubKeys
  206.     strSubKey = ""
  207.     strGuid = ""
  208.     strUserName = ""
  209.     strProfileImagePath = ""
  210.     strSubKey = trim(subkey)
  211.     If (instr(SIDExclusionList, "|" & strSubKey & "|") = 0) and (strSubKey <> "") then
  212.         strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" & strSubKey
  213.         objReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,"Guid", strGuid
  214.         objReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,"ProfileImagePath", strProfileImagePath
  215.  
  216.         strUserName = Right(strProfileImagePath, len(strProfileImagePath) - instrrev(strProfileImagePath, "\"))
  217.         WriteLog "Profile"
  218.         If flgVerboseLog then WriteLog "  SID         : " & strSubKey
  219.         If flgVerboseLog then WriteLog "  GUID        : " & strGuid
  220.         WriteLog "  Profile Path: " & strProfileImagePath
  221.         WriteLog "  UserName    : " & strUserName
  222.    
  223.         dwProfileExclusion = instr(strProfileExclusionList, "|" & trim(lcase(strUserName)) & "|")
  224.         dwSIDExclusion = instr(strHiveExclusionList, "|" & strSubKey & "|")
  225.  
  226.     If flgCustomExclusions then
  227.             flgCustomExMatch = TestCase(lcase(strUserName), lcase(strCustomExclusions))
  228.  
  229.             If flgCustomExMatch then
  230.                 strProfileExclusionList = strProfileExclusionList & trim(lcase(strUserName)) & "|"
  231.                 dwProfileExclusion = 1
  232.             End if
  233.         End if
  234.  
  235.     If flgCustomInclusions then
  236.             flgCustomInMatch = TestCase(lcase(strUserName), lcase(strCustomInclusions))
  237.  
  238.             If flgCustomInMatch = 0 then
  239.                 strProfileExclusionList = strProfileExclusionList & trim(lcase(strUserName)) & "|"
  240.                 dwProfileExclusion = 1
  241.             End if
  242.         End if
  243.  
  244.        
  245.     If flgDateExclusion then
  246.             flgExDateMatch = 0
  247.             If objFSO.FileExists(strProfileImagePath & "\NTUSER.DAT") then
  248.                 Set objFile = objFSO.GetFile(strProfileImagePath & "\NTUSER.DAT")
  249.                 flgExDateMatch = TestDateExclusion(objFile.DateLastModified, DateCurrentTime, dwDateExclusion)
  250.             Else
  251.                 WriteLog "  NTUSER.DAT Does not Exist"
  252.                 If objFSO.FileExists(strProfileImagePath & "\NTUSER.MAN") then
  253.                     WriteLog "  NTUSER.MAN Found"
  254.                     Set objFile = objFSO.GetFile(strProfileImagePath & "\NTUSER.MAN")
  255.                     flgExDateMatch = TestDateExclusion(objFile.DateLastModified, DateCurrentTime, dwDateExclusion)
  256.                 Else
  257.                     WriteLog "  NTUSER.MAN Does not Exist"
  258.                 End if
  259.             End if
  260.             If flgExDateMatch then
  261.                 strProfileExclusionList = strProfileExclusionList & trim(lcase(strUserName)) & "|"
  262.                 dwProfileExclusion = 1
  263.             End if
  264.         End if
  265.         If (dwProfileExclusion = 0) and (dwSIDExclusion = 0) then
  266.             WriteLog "  Profile OK to Delete"
  267.             If flgDateExclusion then WriteLog "  Profile Matches Age Requirement"
  268.             If flgCustomInclusions and flgCustomInMatch then WriteLog "  Profile Matches Inclusion List"
  269.  
  270.             strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" & strSubKey
  271.             DeleteKey HKEY_LOCAL_MACHINE, strKeyPath
  272.  
  273.             strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\" & strSubKey
  274.             DeleteKey HKEY_LOCAL_MACHINE, strKeyPath
  275.  
  276.             strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\" & strSubKey
  277.             DeleteKey HKEY_LOCAL_MACHINE, strKeyPath
  278.  
  279.             strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\" & strSubKey
  280.             DeleteKey HKEY_LOCAL_MACHINE, strKeyPath
  281.  
  282.             If strGuid <> "" then
  283.                 strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\PolicyGuid\" & strGuid
  284.                 DeleteKey HKEY_LOCAL_MACHINE, strKeyPath
  285.  
  286.                 strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileGuid\" & strGuid
  287.                 DeleteKey HKEY_LOCAL_MACHINE, strKeyPath
  288.             Else
  289.                 WriteLog "  Guid is Blank, Deleting Registry Keys based of Guid has been skipped."
  290.             End if
  291.  
  292.             If objFSO.FolderExists(strProfileImagePath) then
  293.             WriteLog "  Folder Exists - Deleting"
  294.                 If flgAllowExecute then objFSO.DeleteFolder(strProfileImagePath), DeleteReadOnly
  295.             Else
  296.                 WriteLog "  Folder Does not Exist"
  297.             End if
  298.         Else
  299.             If dwProfileExclusion then
  300.                 WriteLog "  Profile not Deleted --- Username in Profile Exclusion List"
  301.                 If flgCustomExclusions and flgCustomExMatch then _
  302.                   WriteLog "  Profile not Deleted --- Username Matched Custom Exclusion List"
  303.                 If flgCustomInclusions and (flgCustomInMatch = 0) then _
  304.                   WriteLog "  Profile not Deleted --- Username Did not Match Custom Inclusion List"
  305.                 If flgDateExclusion and flgExDateMatch then _
  306.                   WriteLog "  Profile not Deleted --- Profile is not Older than " & dwDateExclusion & " Days"
  307.             End if
  308.             If dwSIDExclusion then
  309.                 WriteLog "  Profile not Deleted --- User Hive is currently loaded"
  310.                 strHiveOpenSkipped = strHiveOpenSkipped & trim(lcase(strUserName)) & "|"
  311.             End if
  312.         End if
  313.     End if
  314. Next
  315.  
  316. '**********************************************************************************
  317. '*   Get Document and Settings Directory Location from the Registry
  318. strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
  319. objReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,"ProfilesDirectory", strDocAndSettingsLocation
  320. WriteLog ""
  321. WriteLog "Documents and Settings Path: " & strDocAndSettingsLocation
  322. WriteLog ""
  323. WriteLog "Checking For Orphaned Profile Directories"
  324. WriteLog "-----------------------------------------"
  325. Set objFolder = objFSO.GetFolder(strDocAndSettingsLocation)
  326. Set colSubfolders = objFolder.Subfolders
  327.  
  328. '**********************************************************************************
  329. '*   Parse through the directory a check For orphaned profile folders and Delete
  330. For Each objSubfolder in colSubfolders
  331.     strUserName = lcase(Right(objSubfolder.Path, len(objSubfolder.Path) - instrrev(objSubfolder.Path, "\")))
  332.     dwProfileExclusion = instr(strProfileExclusionList, "|" & trim(lcase(strUserName)) & "|")
  333.     dwHiveOpenExclusion = instr(strHiveOpenSkipped, "|" & trim(lcase(strUserName)) & "|")
  334.     If (dwProfileExclusion = 0) and (dwHiveOpenExclusion = 0) then
  335.         WriteLog "Deleting Orphaned Profile Directory: " & objSubfolder.Path
  336.         If flgAllowExecute then objFSO.DeleteFolder(objSubfolder.Path), DeleteReadOnly
  337.     Else
  338.         If dwHiveOpenExclusion then
  339.           WriteLog "Hive Loaded      -- Skippped Delete: " & objSubfolder.Path
  340.         End if
  341.         If dwProfileExclusion then
  342.             WriteLog "Profile Excluded -- Skippped Delete: " & objSubfolder.Path
  343.         End if
  344.     End if
  345. Next
  346.  
  347.  
  348. WriteFooter
  349. If flgLogFile then objLogFile.Close
  350. objReg = Nothing
  351. objFSO = Nothing
  352. objArgs = Nothing
  353.  
  354. '**********************************************************************************
  355. '*   Deletes All Subkeys and Values within a Given Registry Key
  356. Sub DeleteKey(dwHiveType, strDeleteKeyPath)
  357.     Dim dwReturn, arrDeleteSubKeys, strDeleteSubKey
  358.     dwReturn = objReg.EnumKey(dwHiveType, strDeleteKeyPath, arrDeleteSubKeys)
  359.     If (dwReturn = 0) And IsArray(arrDeleteSubKeys) Then
  360.         For Each strDeleteSubKey In arrDeleteSubKeys
  361.             DeleteKey dwHiveType, strDeleteKeyPath & "\" & strDeleteSubKey
  362.         Next
  363.     End if
  364.     If flgAllowExecute then objReg.DeleteKey dwHiveType, strDeleteKeyPath
  365.     If flgVerboseLog then WriteLog "  Deleting: " & strDeleteKeyPath
  366. End Sub
  367.  
  368.  
  369. '**********************************************************************************
  370. '*   Test a List of Wildcard Items Against a Value
  371. Function TestCase(strTestCase, strWildCardTests)
  372.     TestCase = 0
  373.  
  374.     dwAllItems = 0
  375.     dwSoftLeft = 0
  376.     dwSoftRight = 0
  377.  
  378.     arrWildCards = split(strWildCardTests, "|")
  379.  
  380.     For each strWildCard in arrWildCards
  381.         If strWildcard = "*" then
  382.             dwAllItems = 1
  383.         Else
  384.             If left(strWildcard, 1) = "*" then
  385.                 dwSoftLeft = 1
  386.                 strWildcard = right(strWildcard, len(strWildcard) - 1)
  387.             End if
  388.             If right(strWildcard, 1) = "*" then
  389.                 dwSoftRight = 1
  390.                 strWildcard = left(strWildcard, len(strWildcard) - 1)
  391.             End if
  392.         End if    
  393.         If strWildcard = "" then dwAllItems = 1
  394.  
  395.         If dwAllItems then
  396.             TestCase = 1
  397.         Else
  398.             dwLeftOk = 0
  399.             dwRightOk = 0
  400.             dwOffSet = instr(strTestCase, strWildcard)
  401.  
  402.             If dwOffSet then
  403.                 If (dwSoftLeft = 0) and (dwOffSet = 1) then dwLeftOk = 1
  404.                 If dwSoftLeft then dwLeftOk = 1
  405.             End if
  406.             dwRightOffSet = len(strTestCase) - dwOffset - len(strWildcard) + 1
  407.             If dwRightOffSet = 0 then dwRightOk = 1
  408.             If dwRightOffset > 0 and dwSoftRight = 1 then dwRightOk = 1
  409.             If dwLeftOk and dwRightOk then TestCase = 1
  410.         End if
  411.     Next
  412. End Function
  413.  
  414. '**********************************************************************************
  415. '*  Test Profile Date Against the Current Date.  Then Compare Against out Value  
  416. Function TestDateExclusion(dateTestCase, dateTestCurrentTime, dwTestNumDays)
  417.     TestDateExclusion = 0
  418.     dwNumDays = DateDiff("d", dateTestCase, dateTestCurrentTime)
  419.     If dwNumDays <= dwTestNumDays then TestDateExclusion = 1
  420.     If flgVerboseLog then WriteLog "  Profile Age : " & dwNumDays & " Days"
  421. End Function
  422.  
  423. '**********************************************************************************
  424. '*   Log Header
  425. Sub WriteHeader
  426.     WriteLog "---"
  427.     WriteLog "-- Profile Deletion Script Executed: " & dateCurrentTime
  428.     WriteLog "---"
  429.     WriteLog ""
  430. End Sub
  431.  
  432. '**********************************************************************************
  433. '*   Log Footer
  434. Sub WriteFooter
  435.     WriteLog ""
  436.     WriteLog "---"
  437.     WriteLog "-- Profile Deletion Script Completed."
  438.     WriteLog "---"
  439. End Sub
  440.  
  441. '**********************************************************************************
  442. '*   Write String to Log File
  443. Sub WriteLog(strString)
  444.     If flgLogFile then objLogFile.Writeline strString
  445.     If flgWriteConsole then wscript.echo strString
  446. End Sub
Advertisement
Add Comment
Please, Sign In to add comment