2

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

我正在使用 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 操作 ScriptMaps 的工作代码?

4

2 回答 2

2

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();
    }
于 2009-04-30T15:14:26.477 回答
1

我发现从头开始创建 WMI 对象非常困难。更容易 Clone() 您从系统中查询的现有对象,然后对其进行修改。这是我最近编写的用于处理 ScriptMaps 的函数。它在 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
    }
}
于 2009-04-22T23:52:10.100 回答