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 を扱うために最近書いた関数を次に示します。これは C# ではなく Powershell にありますが、考え方は同じです。

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 に答える