DaleHopper
Technical User
Hi, I'm trying to create a script to delete users in a specific Active Directory OU, i've found various scripts on the web and added some parts together to create exactly what i'm after.
The extras I am after are:
I'm wanting to move deleted users documents to a server location
I'm wanting to remove the share each deleted user has on my network which is a hidden share (User$), normally when i delete a user's homedir the share still exists despite the folder no longer existing.
I'm also wanting to delete another folder in a server location such as \\server2\mediafiles\username.
The script i'm currently working with (that doesn't work)
<Script>
Option Explicit
Dim strOU, objOU, objFSO, objUser, trgFolder
' Set Network
Set WshNetwork = WScript.CreateObject("WScript.Network")
' Specify the OU.
strOU = "ou=Staff,ou=Leavers,ou=People,dc=network,dc=local"
' Bind to the OU.
Set objOU = GetObject("LDAP://" & strOU)
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set HomeShare = objWMIService.ExecQuery _
("Select * from Win32_Share Where Name = (objUser.sAMAccountName)")
' Use FileSystemObject to delete folders.
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Filter on user objects.
objOU.Filter = Array("user")
'Set Target Path
trgFolder = "X:\"
' Enumerate users.
For Each objUser In objOU
' Skip computers (which have class user).
If (objUser.Class = "user") Then
' Delete user profile path.
If (objUser.profilePath <> "") Then
If (objFSO.FolderExists(objUser.profilePath) = True) Then
objFSO.DeleteFolder(objUser.profilePath)
End If
End If
' Delete user TS profile path.
If (objUser.msTSProfilePath <> "") Then
If (objFSO.FolderExists(objUser.msTSProfilePath) = True) Then
objFSO.DeleteFolder(objUser.msTSProfilePath)
End If
End If
' Delete user home directory.
If (objUser.homeDirectory <> "") Then
If (objFSO.FolderExists(objUser.homeDirectory) = True) Then
objFSO.CopyFolder((objUser.homeDirectory), trgFolder & (objUser.sAMAccountName), True) Then
objFSO.DeleteFolder(objUser.homeDirectory)
End If
End If
For Each objShare in HomeShare
objShare.Delete
' Delete the user object from AD.
objUser.DeleteObject (0)
End If
Next
WshNetwork.RemoveNetworkDrive "X:"
</Script>
If someone could tidy it up for me, let me know what's wrong i'd be very grateful.
The script is assuming I already have Drive X: mapped to a location to archive staff leavers folders
Many thanks
The extras I am after are:
I'm wanting to move deleted users documents to a server location
I'm wanting to remove the share each deleted user has on my network which is a hidden share (User$), normally when i delete a user's homedir the share still exists despite the folder no longer existing.
I'm also wanting to delete another folder in a server location such as \\server2\mediafiles\username.
The script i'm currently working with (that doesn't work)
<Script>
Option Explicit
Dim strOU, objOU, objFSO, objUser, trgFolder
' Set Network
Set WshNetwork = WScript.CreateObject("WScript.Network")
' Specify the OU.
strOU = "ou=Staff,ou=Leavers,ou=People,dc=network,dc=local"
' Bind to the OU.
Set objOU = GetObject("LDAP://" & strOU)
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set HomeShare = objWMIService.ExecQuery _
("Select * from Win32_Share Where Name = (objUser.sAMAccountName)")
' Use FileSystemObject to delete folders.
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Filter on user objects.
objOU.Filter = Array("user")
'Set Target Path
trgFolder = "X:\"
' Enumerate users.
For Each objUser In objOU
' Skip computers (which have class user).
If (objUser.Class = "user") Then
' Delete user profile path.
If (objUser.profilePath <> "") Then
If (objFSO.FolderExists(objUser.profilePath) = True) Then
objFSO.DeleteFolder(objUser.profilePath)
End If
End If
' Delete user TS profile path.
If (objUser.msTSProfilePath <> "") Then
If (objFSO.FolderExists(objUser.msTSProfilePath) = True) Then
objFSO.DeleteFolder(objUser.msTSProfilePath)
End If
End If
' Delete user home directory.
If (objUser.homeDirectory <> "") Then
If (objFSO.FolderExists(objUser.homeDirectory) = True) Then
objFSO.CopyFolder((objUser.homeDirectory), trgFolder & (objUser.sAMAccountName), True) Then
objFSO.DeleteFolder(objUser.homeDirectory)
End If
End If
For Each objShare in HomeShare
objShare.Delete
' Delete the user object from AD.
objUser.DeleteObject (0)
End If
Next
WshNetwork.RemoveNetworkDrive "X:"
</Script>
If someone could tidy it up for me, let me know what's wrong i'd be very grateful.
The script is assuming I already have Drive X: mapped to a location to archive staff leavers folders
Many thanks