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 SkipVought 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
0
0
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
 
Did you try this ?
objFSO.DeleteFolder objUser.profilePath, True

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi PH,

Thanks for your reply, but I already got that line in my script. Deleting the folder (profile) isn't the issue. The problem is I just dont have the permission to delete it!
Is there a line I can put before it so I can take ownership of the folder first, then delete the folder?
Many thanks,
ICT/Fong
 
What about this (provided you've created a wshShell object before the loop)?
Code:
...
If objFSO.FolderExists(objUser.profilePath) Then
  wshShell.Run "TAKEOWN /F """ & objUser.profilePath & """ /R /D O", 0, True
  objFSO.DeleteFolder objUser.profilePath, True
End If
...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi PH,

Thanks for your quick response. But now I am receiving the error "variable is undefined: 'wshShell'". Sorry but I'm a junior programmer so not sure what you mean by creating a wshShell object before the loop.
 
Dim wsh: Set wshShell = CreateObject("WScript.Shell")

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I really appreciate your help on this PH. I have put your lines of code from your posts but I now I receive 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-file\stuprofs$\") 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-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
 
In order to debug the takeown command, use this:
wshShell.Run "CMD /K TAKEOWN /F """ & objUser.profilePath & """ /R /D O", 1, True

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks for your speedy response.
I have replaced the line: wshShell.Run "TAKEOWN /F """ & objUser.profilePath & """ /R /D O", 0, True
With: wshShell.Run "CMD /K TAKEOWN /F """ & objUser.profilePath & """ /R /D O", 1, True

I know receive the following error in command line:

'\\rcmutility\software\VB SCRIPT\Script to delete STUDENT AD accounts, profiles
and userdata'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported. Defaulting to Windows directory.
ERROR: Invalid syntax. 'O' value is not allowed for '/D' option.
Type "TAKEOWN /?" for usage.

C:\WINDOWS>

------------------------------------------------------------
Do I need to put in the path where the profiles resides? They reside in the following location: \\rcm-file\stuprofs$
Please may you further assist. Many thanks, Fong
 
Sorry, I gave you a french syntax ...
Replace /D O with /D [!]Y[/!]

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PH, the command prompt started running and I can see I have taken ownership of one of the profile folders! But now it does not run the next line of script to delete this profile. It does not loop around. Many thanks for this step, but can you help fix this. Fong
 
After the command prompt, I am still then receiving the error:

Line: 26
Char: 5
Error: Permission denied.

 
So, now you may use this:
wshShell.Run "TAKEOWN /F """ & objUser.profilePath & """ /R /D Y", 0, True

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi PH, thanks but I am still receiving the error permission is denied. I am not sure why this is because I have checked the properties of the folder and can see I have taken ownership permission of the folder.
Im not sure shy I am still receiving this permission denied error
 
Hi again PH, When I run the script and the command prompt, it only takes ownership permission of "one" of the profile folders. I want it to take permission of "all" the profile folders that have their AD account in the "student leavers" OU. please may you assist
 
Just to confirm, after executing the takeown command
Code:
wshShell.Run "TAKEOWN /F """ & objUser.profilePath & """ /R /D Y", 0, True

Is your current user ID listed as the owner?
[ol 1]
[li]Check the Properties of the folder[/li]
[li]Go to the Security tab[/li]
[li]Click Advanced[/li]
[li]Click the Owner tab[/li]
[li]Are you listed?[/li]
[/ol]

Drill down a level or two, check the Owner status of a few subfolders & files - are you still listed as the owner?

To help figure out where it may be breaking, maybe you need to bracket your commands with
Code:
...
on error resume next
<execute your code like delete whatever>
if (err.number <> 0) then
    wscript.echo "error " & err.number & " occurred: " & err.description
    err.clear
else
    'wscript.echo "completed successfully: " & err.number
end if
on error goto 0
...


That's usually what I do when I run into problems like this.
Code:
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-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 objUser.profilePath"
				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(objUser.profilePath)"
				On Error GoTo 0
			End If
		End If
		' Delete the user object from AD.
		On Error Resume Next
		objUser.DeleteObject (0)
		ErrCheck Err.Number,Err.Description,"objUser.DeleteObject (0)"
		On Error GoTo 0
	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(strUserData)"
		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(strUserData)"
		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
 
Many thanks for your most helpful Phylum. I will test this and see how I go. Fong
 
Hi Phylum,

I would just like to say from what I test so far, it works like a gem! Thank you so much for all your time on assisting me, I really appreciate this. Happy Chinese New year to you too! Fong
 
Hi Phylum,

Ahhh, my apologies but I think I spoke too soon.. I have amended your script so it "Does not delete the user object from AD." I simply just remove that section from the script so it keeps the object in AD.
But now I am getting continuous error loop that doesn't end saying:

Error 70/6 occured while trying to "objFSO.DeleteFolder(strUserData)": Permissions denied

Please may you take a look at this when you have a moment. Many thanks for your help and time.



--------------------------------------------

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 objUser.profilePath"
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(objUser.profilePath)"
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(strUserData)"
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(strUserData)"
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top