Hi, I want to start this as a new thread as I spent many hours and still couldn't work this one out.
The script below deletes all the userprofiles and userdata of all those accounts in a specified AD OU.
However I login to the File server that contains all the user profiles and userdata and run the script. I receive the following error:
------------------------------
Line 34
Char 6
Error: permission denied.
------------------------------
There are many AD accounts in the specified OU and I do not know which users data I am denied access. Is there a way I can see an error code and description of which user data I am denied?
My code is below. I already spent all day, so many thanks for any help:
-----------------------------------------------
Option Explicit
Dim strOU, objOU, objFSO, objUser, strUserData, wshShell
Dim wsh: Set wshShell = CreateObject("WScript.Shell")
' Specify the OU.
strOU = "ou=2010EmailForLife,ou=EmailForLife,ou=Student Leavers,ou=People,dc=rcm,dc=ac,dc=uk"
' Bind to the OU.
Set objOU = GetObject("LDAP://" & strOU)
' Use FileSystemObject to delete folders.
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Filter on user objects.
objOU.Filter = Array("user")
' Enumerate users.
For Each objUser In objOU
' Skip computers (which have class user).
If (objUser.Class = "user") Then
' Take Ownership of profile then Delete user profile path.
If (objUser.profilePath <> "\\rcm-file\stuprofs$\") Then
If objFSO.FolderExists(objUser.profilePath) Then
wshShell.Run "CMD /K TAKEOWN /F """ & objUser.profilePath & """ /R /D Y", 1, True
objFSO.DeleteFolder objUser.profilePath, True
End If
End If
End If
' Delete userdata.
strUserData = "\\rcm-file\studuser$\" & objUser.sAMAccountName
If (objFSO.FolderExists(strUserData) = True) Then
objFSO.DeleteFolder(strUserData)
End If
' Delete userprofile if path not specified in the AD field.
strUserData = "\\rcm-file\stuprofs$\" & objUser.sAMAccountName
If (objFSO.FolderExists(strUserData) = True) Then
wshShell.Run "CMD /K TAKEOWN /F """ & strUserData & """ /R /D Y", 1, True
objFSO.DeleteFolder(strUserData)
End If
Next
The script below deletes all the userprofiles and userdata of all those accounts in a specified AD OU.
However I login to the File server that contains all the user profiles and userdata and run the script. I receive the following error:
------------------------------
Line 34
Char 6
Error: permission denied.
------------------------------
There are many AD accounts in the specified OU and I do not know which users data I am denied access. Is there a way I can see an error code and description of which user data I am denied?
My code is below. I already spent all day, so many thanks for any help:
-----------------------------------------------
Option Explicit
Dim strOU, objOU, objFSO, objUser, strUserData, wshShell
Dim wsh: Set wshShell = CreateObject("WScript.Shell")
' Specify the OU.
strOU = "ou=2010EmailForLife,ou=EmailForLife,ou=Student Leavers,ou=People,dc=rcm,dc=ac,dc=uk"
' Bind to the OU.
Set objOU = GetObject("LDAP://" & strOU)
' Use FileSystemObject to delete folders.
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Filter on user objects.
objOU.Filter = Array("user")
' Enumerate users.
For Each objUser In objOU
' Skip computers (which have class user).
If (objUser.Class = "user") Then
' Take Ownership of profile then Delete user profile path.
If (objUser.profilePath <> "\\rcm-file\stuprofs$\") Then
If objFSO.FolderExists(objUser.profilePath) Then
wshShell.Run "CMD /K TAKEOWN /F """ & objUser.profilePath & """ /R /D Y", 1, True
objFSO.DeleteFolder objUser.profilePath, True
End If
End If
End If
' Delete userdata.
strUserData = "\\rcm-file\studuser$\" & objUser.sAMAccountName
If (objFSO.FolderExists(strUserData) = True) Then
objFSO.DeleteFolder(strUserData)
End If
' Delete userprofile if path not specified in the AD field.
strUserData = "\\rcm-file\stuprofs$\" & objUser.sAMAccountName
If (objFSO.FolderExists(strUserData) = True) Then
wshShell.Run "CMD /K TAKEOWN /F """ & strUserData & """ /R /D Y", 1, True
objFSO.DeleteFolder(strUserData)
End If
Next