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

VBScript to take ownership

Status
Not open for further replies.

fong21051

Technical User
Jun 1, 2012
38
GB
Hi Everyone,

I have written a VBScript to delete userfiles/data. But the issue is when I run my script I receive the following error:

line: 24
Char:18
Error: permission denied.

I'm pretty sure the problem is that I do not have "Ownership" permissions of some of the profiles thus receiving the error access is denied. May you help amend my script so I can take ownership of these profiles?
Can I use wshShell.run with setacl.exe?
Many thanks for your help. Fong

-------------------------------------------------
Option Explicit

Dim strOU, objOU, objFSO, objUser, strUserData

' Specify the OU.
strOU = "ou=StudentsToDelete,ou=Default,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
' Delete user profile path.
If (objUser.profilePath <> "\\rcm-file\stuprofs$\") Then
If (objFSO.FolderExists(objUser.profilePath) = True) Then
objFSO.DeleteFolder(objUser.profilePath)
End If
End If
' Delete the user object from AD.
objUser.DeleteObject (0)
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.
strUserData = "\\rcm-file\stuprofs$\" & objUser.sAMAccountName
If (objFSO.FolderExists(strUserData) = True) Then
objFSO.DeleteFolder(strUserData)
End If

Next
 
Dear Sir/Madam,

unfortunately no one has responded to me yet but I am still having issues :(
When I run the script that Phylum has kindly written for me, I receive continuous error loops that does end that say the following:

Error 70 occurred while trying to 'objFSO.DeleteFolder objUser.profilePath': Permission denied

AND Error 70 occurred while trying to 'objFSO.DeleteFolder(strUserData): Permission denied

It doesn't seem to take ownership of the Profile folder or Userdata folder. The 2 error messages above repeat forever in a loop. Please can someone help me with this.

Many thanks, Fong
 
Change your error reporting as indicated below. That will show you exactly which folders cannot be deleted. Then, check the acl permissions in those folders to confirm that in fact, the user that you are executing the script as has ownership.

Other than that, maybe files are open and in use, preventing the folders from being deleted? Can you delete those folders manually?


Code:
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
' Delete user profile path.
If (objUser.profilePath <> "\\rcm-file\stuprofs$\") Then
If objFSO.FolderExists(objUser.profilePath) Then
wshShell.Run "TAKEOWN /F """ & objUser.profilePath & """ /R /D Y", 0, True
On Error Resume next
objFSO.DeleteFolder objUser.profilePath, True
ErrCheck Err.Number,Err.Description,"objFSO.DeleteFolder [highlight #FCE94F]" & objUser.profilePath[/highlight]
On Error GoTo 0
End If
If (objFSO.FolderExists(objUser.profilePath) = True) Then
On Error Resume next
objFSO.DeleteFolder(objUser.profilePath)
ErrCheck Err.Number,Err.Description,"objFSO.DeleteFolder([highlight #FCE94F]" & objUser.profilePath & ")"[/highlight]
On Error GoTo 0
End If
End If
End If

' Delete userdata.
strUserData = "\\rcm-file\studuser$\" & objUser.sAMAccountName
If (objFSO.FolderExists(strUserData) = True) Then
On Error Resume Next
objFSO.DeleteFolder(strUserData)
ErrCheck Err.Number,Err.Description,"objFSO.DeleteFolder([highlight #FCE94F]" & strUserData & ")"[/highlight]
On Error GoTo 0
End If

' Delete userprofile if path not specified.
strUserData = "\\rcm-file\stuprofs$\" & objUser.sAMAccountName
If (objFSO.FolderExists(strUserData) = True) Then
On Error Resume Next
objFSO.DeleteFolder(strUserData)
ErrCheck Err.Number,Err.Description,"objFSO.DeleteFolder([highlight #FCE94F]" & strUserData & ")"[/highlight]
On Error GoTo 0
End If
Next

Sub ErrCheck(ErrCode, ErrDescription, Action)
If (ErrCode<>0) Then
MsgBox "Error " & ErrCode & " occurred while trying to '" & Action & "': " & ErrDescription
Err.Clear
End If
End Sub
 
Hi guitarzan,

Thank you for your extremely helpful advice.
I have done what you said.
Please see below answers to your questions
1. I login to the server that contains all the user profiles and userdata.

2. The script says "permission denied" and tells me which userdata my permission is denied. However, I am manually able to do a "shift delete" and this works. I don't understand why I receive this error.

3. The take ownership doesn't seem to work. It tells me my "permission denied" again on the user profiles. I check the profiles in question and I can see I haven't taken ownership hence cannot access the folder. However, I am able to manually take ownership and delete these profiles.

4. The user profiles and userdata I am deleting cannot be in use because they are of those users who have left and their accounts disabled.

Please may you further assist me. Many thanks for your help.
 
Sorry, I forgot to add I login to the server to run the script as a Domain Admin so I should have full permissions
 
Hi Everyone,
Just to let you know that this is now finally sort (not the first time I said this, but lets hope this is final time)
I found out the reason why it isn't taking ownership is because the "profilepath" field in AD is not always specified in AD or is specified incorrectly with the wrong path.

I simply put in the following code at the end so that it will pickup the "username" field in AD using this code to take ownership:


' Delete userprofile if path not specified.
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

Hopefully that will be the end of this chapter (but u never know). Many thanks for all your help guys.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top