Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help altering VB script 1

Status
Not open for further replies.

pinkpanther56

Technical User
Jun 15, 2005
807
GB
The script below deletes windows local profiles at boot time we use this to clear up any dead mandatory profiles. There is a section at the bottom that excludes certain profiles e.g administrator.

Can anyone make a suggestion on how the script could be altered to read the excluded profiles from a text file or .csv file so I could have a list of staff profiles that I don't want deleting, or even better if it could query Active Directory for group membership I know that last bit might be a tall order so I'm happy to start with a text file for now and work from there ;)

Any VB ninjas care to offer advice?

Thanks.

Code:
' Deletes local profiles from hard disk at MACHINE STARTUP this prevents logon problems when the profile is copied from the 'server

On Error Resume Next

Sub DeleteFolders(strRoot, strExclude)

On Error Resume Next

 	Dim fso, f, fc, folder, arrExcl, folderName, folderPath, i, isExcl

	arrExcl = Split(strExclude, ",")
	
   	Set fso = CreateObject("Scripting.FileSystemObject")
   	Set f = fso.GetFolder(strRoot)
	For Each folder in f.SubFolders

		folderName = LCase(folder.name)
		folderPath = LCase(folder.path)

		isExcl = False

		i = 0
		Do While i < UBound(arrExcl) + 1
	
			If Lcase(arrExcl(i)) = folderName Then 
				isExcl = True	
			End If

		i = i + 1
		Loop	

		If isExcl = False Then
			fso.DeleteFolder folderPath, True
		End If	
		
	Next   
End Sub


Call DeleteFolders("c:\documents and settings\", "administrator,All Users,Default User,")

'Usage: DeleteFolders("full path to folder, ending in \", "list of folders to be excluded, separated by a comma, no slashes")
 
'for an AD group you just need to adjust the population of dicUsersToNotDelete


Set FSO = CreateObject...
Set objTS = FSO.OpenTextFile("???", 1, False)
Set dicUsersToNotDelete = CreateObject("Scripting.Dictionary")
dicUsersToNotDelete.CompareMode = -1 '???
Do While Not objTS.AtEndOfStream
strLine = ""
strLine = objTS.ReadLine
If strLine <> "" Then
If Not dicUsersToNotDelete.Exists(strLine) Then
dicUsersToNotDelete.Add strLine, "hmm"
End If
End If
Loop
objTS.Close
Set objTS = Nothing

Call DeleteFolders(dicUsersToNotDelete...)

Sub DeleteFolders(ByVal dicUsersToNotDelete....)
Set f = fso.GetFolder(strRoot)
For Each folder in f.SubFolders
If Not dicUsersToNotDelete.Exists(folder.Name) Then
'oh i can delete
Else
'excluded
End If
 
sorry after the Call DeleteFolders you might want

Set dicUsersToNotDelete = Nothing
Set FSO = Nothing

you may also like to pass FSO to your Sub so you dont need to create it in there, i can never decide
 
Note:

I have a script to remove obsolete profiles also. Our environment uses Mandotory profiles via local default user profiles. One thing that I needed to address outside the scope of deleting profiles is to also delete SIDs from the registry's HKU and HKLM\..\ProfileGuid hives. Otherwise, profile corruption can be expected.

-Geates
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top