萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 編程語言綜合 >> VBSCript之GenerateSDDL函數(權限設置)

VBSCript之GenerateSDDL函數(權限設置)

自己寫的一個函數和應用實例,用於設置對象的相關權限,需要的朋友可以參考下   復制代碼 代碼如下:
Function GenerateSDDL(AccountName, AccessFlag, AccessType, AccessMask)
 Dim Accounts, ObjWMI, ObjSID, ObjTru, ObjACE
 Const SET_DACL_PRESENT = &H8004

 Set ObjWMI = GetObject("winmgmts:" & "{impersonationLevel=impersonate, (Security)}!.rootcimv2")

 Set Accounts = ObjWMI.ExecQuery("SELECT * FROM Win32_Account WHERE Name='" & AccountName & "'")
 For Each Account In Accounts
  StrSID = Account.SID
 Next
 Set ObjSID = ObjWMI.Get("Win32_SID.SID='"& StrSID &"'")

 Set ObjTru = ObjWMI.Get("Win32_Trustee").SpawnInstance_()
 ObjTru.Domain  = ObjSID.ReferencedDomainName
 ObjTru.Name   = ObjSID.AccountName
 ObjTru.SID   = ObjSID.BinaryRepresentation
 ObjTru.SidLength = ObjSID.SidLength
 ObjTru.SIDString = ObjSID.Sid

 Set ObjACE = ObjWMI.Get("Win32_ACE").SpawnInstance_()
 ObjACE.Trustee   = ObjTru
 ObjACE.AceType   = AccessType
 ObjACE.AccessMask  = AccessMask
 ObjACE.AceFlags  = AccessFlag

 Set GenerateSDDL = ObjWMI.Get("Win32_SecurityDescriptor").SpawnInstance_()
 GenerateSDDL.Owner   = ObjTru
 GenerateSDDL.DACL   = Array(ObjACE)
 GenerateSDDL.ControlFlags = SET_DACL_PRESENT
End Function

例子:

復制代碼 代碼如下:
strPath = "d:1.txt"
Set ObjWMI = GetObject("winmgmts:" & "{impersonationLevel=impersonate, (Security)}!.rootcimv2")
Set ObjSec = ObjWMI.Get("Win32_LogicalFileSecuritySetting.Path='" & strPath & "'")
ObjSec.SetSecurityDescriptor(GenerateSDDL("everyone", &H0, &H1, &H100E0))

文章來源: http://www.enun.net/?p=1255

copyright © 萬盛學電腦網 all rights reserved