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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete local user directories 1

Status
Not open for further replies.

jongag1

Technical User
Feb 1, 2002
65
NL
Hello,

We have an office with 50 workstations. We use a file/print server for the users documents and other files.
Our users can login at every workstation. Because of that on each workstation are a lot of directories for each user in the C:\Documents and Settings\ directory.

I would like a vbs script, which runs once a month at login, to delete all the user directories (except for the Administrator, All Users and Default User directory) on the worksgtation of the users which doesn't excist anymore in the active directory.

Is that possible?

Kind regards,
Age
 
Code:
'==========================================================================
'
' VBScript Source File -- 
'
' NAME: CleanLocalProfiles.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL   : [URL unfurl="true"]http://www.thespidersparlor.com[/URL]	
' Copywrite (c) 2006 All rights reserved
' DATE  : 06/2/2006
'
' COMMENT: Cleans the local user profiles directory.
'          Checks for current users and deletes folders 
'          of users no longer in AD.  
'          Use with caution, ensure you have a good backup.  Data loss may occur.
'          This script and many more can be found in the Admin Script Pack
'          by The Spider's Parlor [URL unfurl="true"]http://www.thespidersparlor.com/vbscript[/URL]
'=====================================
On Error Resume Next
Path1 = "C:\Documents and Settings\"

Dim fso 
Dim oFolder
Dim oFile
Dim oSubFolder

Set fso = createobject("Scripting.FileSystemObject")
Set objDomain = getObject("LDAP://rootDse")
DomainString = objDomain.Get("dnsHostName")
If Err <> 0 Then
   'Domain not present to validate users against.  Exit script to prevent data loss.
   WScript.Quit
End If

Set oFolder = fso.GetFolder(Path1)
Set colSubfolders = oFolder.Subfolders

For Each oSubfolder in colSubfolders
    'First check To see if the folder is from multiple profiles in form username.domainname
    'Use the folder name to get the user ID
	If InStr(oSubFolder.Name,".") Then
	   splitArray = Split(oSubFolder.Name,".")
	   UserString = splitArray(0)
	Else
	   UserString = oSubFolder.Name
	End If
	
	Select Case lcase(UserString)
		Case "administrator", "all users", "default user", "localservice", "networkservice","markmac","markdmac"
		     'Do nothing
		Case Else
	   		'Now bind to the user object to see if a valid user.  If not delete folder.

	   		Err = GetObject("WinNT://" & DomainString & "/" & UserString)
			If Err <> 0 Then
				fso.DeleteFolder(Path1 & oSubFolder.Name),True
				WScript.Echo oSubFolder.Name
			End If
	End Select
	
Next

Set oSubFolder = Nothing
Set oFolder = Nothing
Set fso = Nothing

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Sorry forgot to remove my own two local user account names from the script above. Remove "markmac" and "markdmac" from your version.

Do not remove the other accounts as they are hidden system folders. Machines running SQL will also have a SQL_SVC that you may wish to exclude.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Hello Mark,

I tested the script on our test pc.
Unfortunately it delted also the folders of accounts which excist in the AD. Except for the account which was loged into the pc. The script produced a popup with the username in it.

In the AD we work with several folders and subfolders.
Can it perhaps be that your script is not looking in the subfolders?

Our Active directory structure is something like below:

AD
-domain.nl
--agents
---agents no internet
--staff
--teamleaders

Kind regards,
Age
 
This line:
Err = GetObject("WinNT://" & DomainString & "/" & UserString)

Does the binding. That should work no matter where in the AD structure your users are.

You can comment out the line:
fso.DeleteFolder(Path1 & oSubFolder.Name),True

for a read only run of the script to see what it will delete. Add in a line to determine what the value of the Err is after the binding and post the results back.

[red]'Clear any Errors before we do the binding[/red]
[red]Err.Clear[/red]
[red]'[/red]fso.DeleteFolder(Path1 & oSubFolder.Name),True
WScript.Echo oSubFolder.Name
[red]Wscript.Echo Err.Number & " " & Err.Description[/red]



I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Tested it on my pc. First I put in a fake folder with the name 'test'

I got 2 popups; 1st was 'test' and the second was '0'
The zero means no errors I guess?

I will now test it on a client pc.

Kind regards,
Age

 
Strange, on my pc it only showed a popup with the fake (not excisting account :)) and afterwards a zero. It did not poped up my own user account.

On an other pc it poped up all the directories, so the excisting and the not excisting accounts. Afterwards all zero's.

Can it perhaps be that my account is an administrator account? Because on the pc I tested it we logged in with a normal user account.

I will now log in with my account on that pc. I will let you know if it than works different.

Kind regards,
Age
 
On the other pc with my account is also gives all the popups... I guess there is something different between my pc and the client pc's. But I don't know what it can be.

Kind regards,
Age
 
I thought all account have local admin rights.

I noticed in our login script the following:

Set UserObj = GetObject("WinNT://" & nUser.UserDomain & "/" & nUser.Username)

Perhaps our AD is different than normal?

Kind regards,
Age
 
I have been testing on my side and this works. Sorry it is not working for you.

it should only give a pop up for those directories it will delete. Those users should NOT have an account in AD. Are you seeing different results?

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
On my pc it gives the popup off the not-excisting account in the AD.

On the test pc and the other client pc it gives popups for directories of excisting accounts in the AD and off not-excisting account in the AD. On that pc it doesn't mind if I use my login account or a normal user account.

On the test pc it delted the directories with a normal user account.

So on my pc your script is working great, but on the others I am affraid it is not. Deleting works, but it looks like it doesn't check the AD properly.

I would like to thank you for your help thought!

Kind regards,
Age
 
I am interested in debugging this further.

under this line:
DomainString = objDomain.Get("dnsHostName")

Add this:
Wscript.Echo DomainString

Then run the script again on the client PC and let me knwo if it is successfully finding your domain name or not.

thanks.

4:39 AM my time. Time to get some sleep. will check on you in the morning (later today).

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
It shows the domain correctly: srvfile01.domain.nl

Sleep well!

Kind regards,
Age
 
I remembered we also had a problem connecting to the AD with a php script. The problem there was that our domain includes the .nl in it.

We now connect with (in php):
var $_account_suffix="@domain.nl";
var $_base_dn = "DC=domain,DC=nl";
var $_domain_controllers = array ("srvfile01.domain.nl");

We had to add the 'DC=nl' part.

Don't know if this info can help you...

Kind regards,
Age
 
I can email you the output off a script we have. It search for an user in the AD using the php script above. If found it displayes the information which is stored in the AD.

Maybe it is usefull for you.

Regards,
Age
 
I don't do PHP so that would not help me. When you log into your PC, what is the netbios friendly name that shows up in the drop down list?

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Just saw your post before the last one.
The friendly name is QUALITYCONTACTS

Regards,
Age
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top