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-filestuprofs$") 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-filestuduser$" & objUser.sAMAccountName
If (objFSO.FolderExists(strUserData) = True) Then
objFSO.DeleteFolder(strUserData)
End If
' Delete userprofile if path not specified.
strUserData = "rcm-filestuprofs$" & objUser.sAMAccountName
If (objFSO.FolderExists(strUserData) = True) Then
objFSO.DeleteFolder(strUserData)
End If
Next
VBScript to take ownership
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
VBScript to take ownership
Hi Guys,
I have amended my script below to try and take ownership of the profiles, but now I receive the following error:
Line: 26
Char: 5
Error: Permission denied.
Line 26 is the following line: objFSO.DeleteFolder objUser.profilePath, True
It looks like the "TAKEOWN" function did not work?
I am so close, please may you help me resolve this, I have included the changes to the script, below.
Many thanks,
Fong
-------------------------------------------------------------------
Option Explicit
Dim strOU, objOU, objFSO, objUser, strUserData, wshShell
Dim wsh: Set wshShell = CreateObject("WScript.Shell")
' 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-filestuprofs$") Then
If objFSO.FolderExists(objUser.profilePath) Then
wshShell.Run "TAKEOWN /F """ & objUser.profilePath & """ /R /D O", 0, True
objFSO.DeleteFolder objUser.profilePath, True
End If
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-filestuduser$" & objUser.sAMAccountName
If (objFSO.FolderExists(strUserData) = True) Then
objFSO.DeleteFolder(strUserData)
End If
' Delete userprofile if path not specified.
strUserData = "rcm-filestuprofs$" & objUser.sAMAccountName
If (objFSO.FolderExists(strUserData) = True) Then
objFSO.DeleteFolder(strUserData)
End If
Next
I have amended my script below to try and take ownership of the profiles, but now I receive the following error:
Line: 26
Char: 5
Error: Permission denied.
Line 26 is the following line: objFSO.DeleteFolder objUser.profilePath, True
It looks like the "TAKEOWN" function did not work?
I am so close, please may you help me resolve this, I have included the changes to the script, below.
Many thanks,
Fong
-------------------------------------------------------------------
Option Explicit
Dim strOU, objOU, objFSO, objUser, strUserData, wshShell
Dim wsh: Set wshShell = CreateObject("WScript.Shell")
' 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-filestuprofs$") Then
If objFSO.FolderExists(objUser.profilePath) Then
wshShell.Run "TAKEOWN /F """ & objUser.profilePath & """ /R /D O", 0, True
objFSO.DeleteFolder objUser.profilePath, True
End If
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-filestuduser$" & objUser.sAMAccountName
If (objFSO.FolderExists(strUserData) = True) Then
objFSO.DeleteFolder(strUserData)
End If
' Delete userprofile if path not specified.
strUserData = "rcm-filestuprofs$" & objUser.sAMAccountName
If (objFSO.FolderExists(strUserData) = True) Then
objFSO.DeleteFolder(strUserData)
End If
Next
VBScript to take ownership
Do you have sufficient rights to be able to take ownership?
Even after you take ownership you may have to add fullcontrol and propagate.
You need to test this at a commandline to be sure you have correct rights and that the folders are set up correctly.
Even after you take ownership you may have to add fullcontrol and propagate.
You need to test this at a commandline to be sure you have correct rights and that the folders are set up correctly.
- Alexander Riedel
- Posts: 8479
- Last visit: Thu Mar 28, 2024 9:29 am
- Been upvoted: 37 times
VBScript to take ownership
Maybe this would help:
http://www.sapien.com/blog/2008/09/29/w ... -the-file/
The component is here: http://www.sapien.com/downloads
in the "Free components" folder
http://www.sapien.com/blog/2008/09/29/w ... -the-file/
The component is here: http://www.sapien.com/downloads
in the "Free components" folder
Alexander Riedel
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.