如何使用 WMI 更改文件权限?
我想做的事情相当于 此处来自脚本。基本上,我想获得该文件的所有权,并将权限设置为所有者/完全控制。
在我看来,从 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
我仍然缺少的部分是设置权限,并让它在相对路径上工作。
I'm want to do the equivalent of what is described here from a script. Basically, I want to take ownership of the file, and set the permissions to OWNER/Full Control.
It seems to me that using WMI from a vbs script is the most portable way. That is, I'd like to avoid xcacls, icacls and other tools that either require a download, or are supported only on some versions of windows.
After googling around, I found this code for taking ownership:
'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
The pieces I'm still missing is setting the permissions, and having it work on relative paths.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您已经在
CIM_DataFile
类中使用TakeOwnership
,我假设您可以只使用 ChangeSecurityPermissions 更改权限,属于同一类。您也许可以使用 GetAbsolutePathName 来在使用之前将相对路径转换为绝对路径。
Since you're already using
TakeOwnership
in theCIM_DataFile
class, I'd assume you could just use ChangeSecurityPermissions to change the permissions, which is in the same class.And you might be able to use GetAbsolutePathName to convert your relative paths to absolute paths before you use them.
根据 ho1 的回答的提示,我又搜索了一些内容,最终得出了这个结论:
此脚本找到当前用户 SID,然后获取所有权并将 argv[0] 中给出的文件的权限更改为仅对当前用户完全控制用户。
Taking the hints from ho1's answer, I googled around some more, and eventually came up with this:
This script finds the current user SID, then takes ownership and changes the permissions on the file given in argv[0] to Full Control only to current user.