Page 1 of 1

VBScript to take ownership

Posted: Wed Jan 23, 2013 11:32 pm
by fong21051
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

Posted: Fri Jan 25, 2013 2:12 am
by fong21051
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

VBScript to take ownership

Posted: Mon Jan 28, 2013 4:58 am
by jvierra
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.

VBScript to take ownership

Posted: Thu Jan 31, 2013 9:50 pm
by Alexander Riedel
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