使用 ManagementClasses 添加 ScriptMap 对象
我已经问过一个相关的问题,但遗憾的是答案虽然正确,但并没有真正解决我的问题。
我正在使用 ManagementClass/ManagementObject WMI API(因为它比 DirectoryEntry API 更能处理远程管理)。 我想从
使用通用字符串格式解决方案中完全删除现有的脚本映射,该解决方案似乎适用于 VBS,但不适用于 ManagementClass API。 因此,我一直在尝试编写一些可以创建正确的脚本映射对象数组的东西,例如
foreach (var extension in extensions) {
var scriptMap = scriptMapClass.CreateInstance();
SetWmiProperty(scriptMap, "ScriptMap.Extensions", "." + extension);
不幸的是,似乎不可能实现函数 SetWmiProperty。 如果我尝试以下操作,
wmiObject.Properties.Add(propertyName, CimType.SInt32);
我会收到“由于对象的当前状态,操作无效。”。 另一方面,如果我只是尝试设置该属性,我会被告知该属性不存在。 scriptMap 类具有路径“ScriptMap”,这是现有对象显示的路径。
有人有使用 ManagementClass API 操作 ScriptMap 的工作代码吗?
I've already asked a related question, but sadly the answers, whilst correct, didn't actually solve my problem.
I'm using the ManagementClass/ManagementObject WMI API (because it's better at handling remote administration than the DirectoryEntry API). I'd like to completely remove the existing script maps from a
Using the common string format solution seems to work for VBS, but not for the ManagementClass API. So, I've been trying to write something that would create the correct array of script map objects e.g.
foreach (var extension in extensions) {
var scriptMap = scriptMapClass.CreateInstance();
SetWmiProperty(scriptMap, "ScriptMap.Extensions", "." + extension);
Unfortunately, it doesn't seem possible to implement the function SetWmiProperty. If I attempt the following
wmiObject.Properties.Add(propertyName, CimType.SInt32);
I get "Operation is not valid due to the current state of the object.". On the other hand, if I just try to set the property, I get told that the property doesn't exist. The scriptMap class has the path "ScriptMap", which is what the existing objects display.
Does anyone have any working code that manipulates ScriptMaps using the ManagementClass API?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现从头开始创建 WMI 对象非常困难。 更容易 Clone() 从系统查询的现有对象,然后对其进行修改。 这是我最近编写的一个处理 ScriptMap 的函数。 它是在 Powershell 中,而不是 C# 中,但想法是相同的:
I find it's very difficult to create WMI objects from scratch. Easier to Clone() an existing object you've queried from the system and then modify it. Here's a function I wrote recently to deal with ScriptMaps. It's in Powershell, not C#, but the idea is the same:
Richard Berg 概述的技术的 AC# 示例。
A C# example of the technique outlined by Richard Berg.