如何使用 WMI 更改文件权限? [英] How to change file permissions with WMI?

查看:30
本文介绍了如何使用 WMI 更改文件权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做与所描述的相同的事情 此处 来自脚本.基本上,我想取得文件的所有权,并将权限设置为所有者/完全控制.

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.

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

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.

推荐答案

由于您已经在 CIM_DataFile 类中使用了 TakeOwnership,我假设您可以使用 ChangeSecurityPermissions 更改权限,这是在同一个班级.

Since you're already using TakeOwnership in the CIM_DataFile class, I'd assume you could just use ChangeSecurityPermissions to change the permissions, which is in the same class.

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

And you might be able to use GetAbsolutePathName to convert your relative paths to absolute paths before you use them.

这篇关于如何使用 WMI 更改文件权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆