I am trying to add a user to the security tab for a folder. I can do it
manually, but my code fails.
I use code I found in a news group a bit modified:
Dim Sec 'Dim sec As New ADsSecurity
Dim sd 'Dim sd As IADsSecurityDescriptor
Dim Dacl 'Dim Dacl As IADsAccessControlList
Dim newAce 'Dim newAce As New AccessControlEntry
Dim WSHNetwork
Dim sFolderPath
Dim sTrustee
' Prolog
Set WSHNetwork = WScript.CreateObject("WScript.Network")
Set Sec = CreateObject("ADsSecurity")
sTrustee = WSHNetwork.UserDomain & "\" & WSHNetwork.UserName
sFolderPath = "S:\CLIENTS\FUNDS\000 test"
Set sd = sec.GetSecurityDescriptor("FILE://" & sFolderPath)
Set Dacl = sd.DiscretionaryAcl
On Error Resume Next
' Body
Set newAce = CreateObject("AccessControlEntry")
wscript.echo "New trustee: " & sTrustee
newAce.Trustee = sTrustee
newAce.AccessMask = ADS_RIGHT_GENERIC_ALL Or ADS_RIGHT_GENERIC_READ
Or ADS_RIGHT_GENERIC_EXECUTE _
Or ADS_RIGHT_GENERIC_WRITE Or
ADS_RIGHT_DELETE
newAce.AceFlags = ADS_ACEFLAG_UNKNOWN Or ADS_ACEFLAG_INHERIT_ACE
newAce.AceType = ADS_ACETYPE_ACCESS_ALLOWED
sd.Owner = newAce.Trustee
Dacl.AddAce newAce
'Removes everyone else from the permission
For Each newAce In Dacl
wscript.echo "Trustees already there: " & newAce.Trustee
If newAce.Trustee <> Trim(sTrustee) Then
Dacl.RemoveAce newAce
wscript.echo "Removing trustee: " & newAce.Trustee
End If
sd.DiscretionaryAcl = Dacl
Next
sd.DiscretionaryAcl = Dacl
sec.SetSecurityDescriptor sd
' trustees after the change
Set sd = sec.GetSecurityDescriptor("FILE://" & sFolderPath)
Set Dacl = sd.DiscretionaryAcl
For Each newAce In Dacl
wscript.echo "Trustees afterwards: " & newAce.Trustee
Next
' Epilog
If Err.Number <> 0 then
MsgBox Err.Description
End if
' Destroy all objects
Set sec = Nothing
Set sd = Nothing
Set Dacl = Nothing
Set ace = Nothing
Set newAce = Nothing
---
No user is added and current users are not deleted when I check the
folder's security tab after the code has run.
What am I doing wrong?
I use Windows 2000.
Regards,
Jan Nordgreen