使用 ManagementClasses 添加 ScriptMap 对象

发布于 2024-07-11 13:04:11 字数 756 浏览 3 评论 0原文

我已经问过一个相关的问题,但遗憾的是答案虽然正确,但并没有真正解决我的问题。

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羅雙樹 2024-07-18 13:04:15

我发现从头开始创建 WMI 对象非常困难。 更容易 Clone() 从系统查询的现有对象,然后对其进行修改。 这是我最近编写的一个处理 ScriptMap 的函数。 它是在 Powershell 中,而不是 C# 中,但想法是相同的:

function Add-AspNetExtension
{
    [CmdletBinding()]
    param (
        [Parameter(Position=0, Mandatory=$true)]
        [psobject] $site  # IIsWebServer custom object created with Get-IIsWeb
        [Parameter(ValueFromPipeline=$true, Mandatory=$true)]
        [string] $extension
    )

    begin 
    {
        # fetch current mappings
        # without the explicit type, PS will convert it to an Object[] when you use the += operator
        [system.management.managementbaseobject[]] $maps = $site.Settings.ScriptMaps

        # whatever the mapping is for .aspx will be our template for mapping other things to ASP.NET
        $template = $maps | ? { $_.Extensions -eq ".aspx" }
    }

    process
    {
        $newMapping = $template.Clone()
        $newMapping.Extensions = $extension
        $maps += newMapping
    }

    end
    {
        $site.Settings.ScriptMaps = $maps
    }
}

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:

function Add-AspNetExtension
{
    [CmdletBinding()]
    param (
        [Parameter(Position=0, Mandatory=$true)]
        [psobject] $site  # IIsWebServer custom object created with Get-IIsWeb
        [Parameter(ValueFromPipeline=$true, Mandatory=$true)]
        [string] $extension
    )

    begin 
    {
        # fetch current mappings
        # without the explicit type, PS will convert it to an Object[] when you use the += operator
        [system.management.managementbaseobject[]] $maps = $site.Settings.ScriptMaps

        # whatever the mapping is for .aspx will be our template for mapping other things to ASP.NET
        $template = $maps | ? { $_.Extensions -eq ".aspx" }
    }

    process
    {
        $newMapping = $template.Clone()
        $newMapping.Extensions = $extension
        $maps += newMapping
    }

    end
    {
        $site.Settings.ScriptMaps = $maps
    }
}
寄风 2024-07-18 13:04:13

Richard Berg 概述的技术的 AC# 示例。

static void ConfigureAspNet(ManagementObject virtualDirectory, string version, string windowsLocation, IEnumerable<string> extensions)
    {
        var scriptMaps = virtualDirectory.GetPropertyValue("ScriptMaps");
        var templateObject = ((ManagementBaseObject[])scriptMaps)[0];
        List<ManagementBaseObject> result = new List<ManagementBaseObject>();
        foreach (var extension in extensions) {
            var scriptMap = (ManagementBaseObject) templateObject.Clone();
            result.Add(scriptMap);
            if (extension == "*")
            {
                scriptMap.SetPropertyValue("Flags", 0);
                scriptMap.SetPropertyValue("Extensions", "*");
            } else
            {
                scriptMap.SetPropertyValue("Flags", 5);
                scriptMap.SetPropertyValue("Extensions", "." + extension);
            }
            scriptMap.SetPropertyValue("IncludedVerbs", "GET,HEAD,POST,DEBUG");
            scriptMap.SetPropertyValue("ScriptProcessor",
                string.Format(@"{0}\microsoft.net\framework\{1}\aspnet_isapi.dll", windowsLocation, version));
        }
        virtualDirectory.SetPropertyValue("ScriptMaps", result.ToArray());
        virtualDirectory.Put();
    }

A C# example of the technique outlined by Richard Berg.

static void ConfigureAspNet(ManagementObject virtualDirectory, string version, string windowsLocation, IEnumerable<string> extensions)
    {
        var scriptMaps = virtualDirectory.GetPropertyValue("ScriptMaps");
        var templateObject = ((ManagementBaseObject[])scriptMaps)[0];
        List<ManagementBaseObject> result = new List<ManagementBaseObject>();
        foreach (var extension in extensions) {
            var scriptMap = (ManagementBaseObject) templateObject.Clone();
            result.Add(scriptMap);
            if (extension == "*")
            {
                scriptMap.SetPropertyValue("Flags", 0);
                scriptMap.SetPropertyValue("Extensions", "*");
            } else
            {
                scriptMap.SetPropertyValue("Flags", 5);
                scriptMap.SetPropertyValue("Extensions", "." + extension);
            }
            scriptMap.SetPropertyValue("IncludedVerbs", "GET,HEAD,POST,DEBUG");
            scriptMap.SetPropertyValue("ScriptProcessor",
                string.Format(@"{0}\microsoft.net\framework\{1}\aspnet_isapi.dll", windowsLocation, version));
        }
        virtualDirectory.SetPropertyValue("ScriptMaps", result.ToArray());
        virtualDirectory.Put();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文