VBScript to take ownership

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
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.
Locked
User avatar
fong21051
Posts: 2
Joined: Wed Jan 23, 2013 11:32 pm

VBScript to take ownership

Post by fong21051 » Wed Jan 23, 2013 11:32 pm

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

User avatar
fong21051
Posts: 2
Joined: Wed Jan 23, 2013 11:32 pm

VBScript to take ownership

Post by fong21051 » Fri Jan 25, 2013 2:12 am

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

jvierra
Posts: 13992
Joined: Tue May 22, 2007 9:57 am
Contact:

VBScript to take ownership

Post by jvierra » Mon Jan 28, 2013 4:58 am

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.

User avatar
Alexander Riedel
Posts: 7121
Joined: Tue May 29, 2007 4:43 pm

VBScript to take ownership

Post by Alexander Riedel » Thu Jan 31, 2013 9:50 pm

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
Alexander Riedel
SAPIEN Technologies, Inc.

Locked