VBS获取文件的SDDL字符串

标签: , , , , ,

VBS调用WMI中Win32_SecurityDescriptorHelper类的Win32SDToSDDL方法将security descriptor转成SDDL字符串。

代码如下:

' Obtain argument from command line

If WScript.Arguments.Count = 0 Then
    WScript.Echo "Usage: GetFileSD <file_name>"
    WScript.Quit 1
End If

' Get the filename with path, for example C:\Users\user1\test.txt
Set objFileSystem = CreateObject( "Scripting.FileSystemObject" )
Filename = WScript.Arguments( 0 )
Set objFile = objFileSystem.GetFile( Filename )
Filename = objFile.Path
WScript.Echo Filename

' Get an instance of Win32_SecurityDescriptorHelper
Set objHelper = GetObject( _
    "winmgmts:root\cimv2:Win32_SecurityDescriptorHelper" )

' Connect to WMI on local computer and root\cimv2 namespace
Set objWMIService = GetObject( "winmgmts:root\cimv2" )

' Get the instance of Win32_LogicalFileSecuritySetting
'    associated with the file
' Replace single "\" with "\\" as escape character
Set objFile = objWMIService.Get( _
    & "Win32_LogicalFileSecuritySetting=""" _
    & Replace( Filename,"\","\\") & """" )

' Get the existing security descriptor for the file
Return = objFile.GetSecurityDescriptor( objSD )
If ( return <> 0 ) Then
    WScript.Echo "Could not get security descriptor: " & Return
    wscript.Quit Return
End If

' Convert file security descriptor from 
'     Win32_SecurityDescriptor format to SDDL format
Return = objHelper.Win32SDToSDDL( objSD,SDDLstring )
If ( Return <> 0 )  Then
    WScript.Echo "Could not convert to SDDL: " & Return
    WScript.Quit Return
End If

WScript.Echo SDDLstring

代码出处:http://msdn.microsoft.com/ZH-CN/library/windows/desktop/aa394056%28v=vs.85%29.aspx

赞赏

微信赞赏支付宝赞赏

随机文章:

  1. VBS基础教程第六篇
  2. 未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPackage”包
  3. 伪造电子邮件发送时间
  4. 用JavaScript实现PHP的urldecode函数
  5. OpenWrt实现WOL(Wake-on-LAN)网络唤醒

留下回复