【问题标题】:How to change file permissions with WMI?如何使用 WMI 更改文件权限?
【发布时间】:2010-11-03 09:44:34
【问题描述】:

我想做与脚本中描述的here 相同的操作。基本上,我想获得文件的所有权,并将权限设置为 OWNER/Full Control。

在我看来,从 vbs 脚本中使用 WMI 是最便携的方式。也就是说,我想避免使用 xcacls、icacls 和其他需要下载或仅在某些版本的 windows 上受支持的工具。

谷歌搜索后,我找到了获取所有权的代码:

'connect to WMI namespace on local machine 
Set objServices = 
GetObject("winmgmts:{impersonationLevel=impersonate}") 
'get a reference to data file 
strFile = Wscript.Arguments(0) 
Set objFile = objServices.Get("CIM_DataFile.Name='" & strFile & "'") 
If  objFile.TakeOwnership = 0 Then 
    Wscript.Echo "File ownership successfully changed" 
Else 
    Wscript.Echo "File ownership transfer operation" 
End If 

我仍然缺少的部分是设置权限,并让它在相对路径上工作。

【问题讨论】:

    标签: windows vbscript wmi


    【解决方案1】:

    由于您已经在 CIM_DataFile 类中使用了 TakeOwnership,我假设您可以使用 ChangeSecurityPermissions 来更改权限,它在同一个类中。

    您也许可以在使用之前使用GetAbsolutePathName 将相对路径转换为绝对路径。

    【讨论】:

      【解决方案2】:

      从 ho1 的回答中得到提示,我在 Google 上搜索了更多内容,最终得出了这个结论:

      此脚本查找当前用户 SID,然后获取所有权并将 argv[0] 中给出的文件的权限更改为仅对当前用户完全控制。

      Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}") 
      
      Function GetCurrentUserSID
          ' Get user name '
          Set colComputer = objWMI.ExecQuery("Select * from Win32_ComputerSystem")
      
          ' Looping over one item '
          For Each objComputer in colComputer
            currentUserName = objComputer.UserName
          Next
      
          Set AccountSIDs = GetObject("Winmgmts:").InstancesOf("Win32_AccountSID") 
          For Each AccountSID In AccountSIDs
              AccountKey = AccountSID.Element 
              Set objAccount = GetObject("Winmgmts:"+AccountKey) 
              strName = objAccount.Domain & "\" & objAccount.Name
              If strName = currentUserName Then ' that's it 
                  SIDKey = AccountSID.Setting
                  Set SID = GetObject("Winmgmts:" + SIDKey)
                  GetCurrentUserSID = SID.BinaryRepresentation
                  Exit For 
              End If   
          Next 
      End Function
      
      Function LimitPermissions(path, SID)
          Set objFile = objWMI.Get("CIM_DataFile.Name='" & path & "'") 
      
          Set Trustee = GetObject("Winmgmts:Win32_Trustee").SpawnInstance_ 
          Trustee.SID = SID
      
          Set ACE = getObject("Winmgmts:Win32_Ace").Spawninstance_ 
          ACE.AccessMask = 2032127 ' Full Control
          ACE.AceFlags = 3 
          ACE.AceType = 0
          ACE.Trustee = Trustee 
      
          Set objSecDescriptor = GetObject("Winmgmts:Win32_SecurityDescriptor").SpawnInstance_ 
          objSecDescriptor.DACL = Array(ACE) 
      
          objFile.ChangeSecurityPermissions objSecDescriptor, 4 
      End Function
      
      Function TakeOwnership(path)
          Set objFile = objWMI.Get("CIM_DataFile.Name='" & path & "'") 
          TakeOwnership = objFile.TakeOwnership
      End Function
      
      ' Main '
      
      strFilename = Wscript.Arguments(0) 
      Set fso = CreateObject("Scripting.FileSystemObject")
      path = fso.GetAbsolutePathName(strFilename)
      
      SID = GetCurrentUserSID
      
      TakeOwnership path
      LimitPermissions path, SID
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-12-30
        • 1970-01-01
        • 1970-01-01
        • 2011-02-27
        • 2017-04-06
        • 2014-01-25
        • 2010-12-09
        相关资源
        最近更新 更多