如何通过WMI获取IIS应用程序信息

时间:2009-04-29 10:43:13

标签: .net windows iis wmi

如何使用WMI获取IIS应用程序(虚拟文件夹)的实际目录路径?

3 个答案:

答案 0 :(得分:4)

当然,它已经3岁了,但这是个不错的小问题。如果规范解决方案必须使用.NET包括PowerShell,那么这将起到作用。有人可能想知道某一天:

$server = 'ServerName'
$query = "Select Path From IIsWebVirtualDirSetting WHERE Name = 'W3SVC/1/ROOT'"
Get-WmiObject -namespace "root/microsoftiisv2" -query $query -computername $server -authentication 6

生成的对象将包含一个名为“Path”的属性。

答案 1 :(得分:3)

使用Scriptomatic V2工具查看更多样本:

On Error Resume Next

Const wbemFlagReturnImmediately = &h10 Const wbemFlagForwardOnly = &h20

arrComputers = Array("*") For Each strComputer In arrComputers WScript.Echo WScript.Echo "==========================================" WScript.Echo "Computer: " & strComputer WScript.Echo "=========================================="

Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\MicrosoftIISv2") Set colItems = objWMIService.ExecQuery("SELECT * FROM IIsWebVirtualDir_IIsWebVirtualDir", "WQL", _ wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each objItem In colItems WScript.Echo "GroupComponent: " & objItem.GroupComponent WScript.Echo "PartComponent: " & objItem.PartComponent WScript.Echo Next Next

答案 2 :(得分:2)

这是一个纯粹的.Net选项,应该返回与Isalamon的答案相同的结果。

来自我的WmiMacros

示例用法(替换strComputer

Macros.WmiMacros.QueryWmiAdvanced  (Macros.WmiMacros.ScopeItem.Creation("\\\\strComputer\\root\\MicrosoftIISv2", true,true)) "SELECT * FROM IIsWebVirtualDir_IIsWebVirtualDir"
|> Seq.map (fun e-> e.Properties.["GroupComponent"].Value, e.Properties.["PartComponent"].Value)

更详细的用法:

let webServerSettings = 
    Macros.WmiMacros.QueryWmiAdvanced  (Macros.WmiMacros.ScopeItem.Creation("\\\\strComputer\\root\\MicrosoftIISv2", true,true)) "SELECT Name,ServerComment FROM IIsWebServerSetting" 
    |> Seq.map (fun e -> e.Properties.["Name"].Value,e.Properties.["ServerComment"].Value)
let webVirtualDirs = 
    Macros.WmiMacros.QueryWmiAdvanced  (Macros.WmiMacros.ScopeItem.Creation("\\\\strComputer\\root\\MicrosoftIISv2", true,true)) "SELECT AppRoot,Name FROM IIsWebVirtualDir"
    |> Seq.map (fun e -> e.Properties.["Name"].Value,e.Properties.["AppRoot"].Value)
let webVirtualDirSettings = 
    Macros.WmiMacros.QueryWmiAdvanced  (Macros.WmiMacros.ScopeItem.Creation("\\\\strComputer\\root\\MicrosoftIISv2", true,true)) "SELECT Name,Path,AppPoolId FROM IIsWebVirtualDirSetting"
    |> Seq.map (fun e -> e.Properties.["Name"].Value,e.Properties.["Path"].Value,e.Properties.["AppPoolId"].Value)
// webServerSettings.Dump("ss");
// webVirtualDirs.Dump("vd");
query { 
    for name,sc in webServerSettings do 
    join (vname,appRoot) in webVirtualDirs on ((name.ToString() + "/ROOT") = vname.ToString())
    join (sname,path,appPoolId) in webVirtualDirSettings on (name.ToString()+ "/ROOT" = sname.ToString() )
    select (appRoot,name,sc,path,appPoolId)
    }

详细的实施代码:

type ScopeItem = 
    | Scope of ManagementScope
    | Creation of string*bool*bool

let private createAdvancedScope (path:string) requiresDomainSecurity requiresPacketSecurity = 
    let scope = 
        if requiresDomainSecurity then  
            let conn = ConnectionOptions(Authority=sprintf "ntlmdomain:%s" Environment.UserDomainName)
            ManagementScope(path, conn)
        else 
        ManagementScope(path, null)
    if requiresPacketSecurity then scope.Options.Authentication <- AuthenticationLevel.PacketPrivacy
    scope.Connect()
    scope

let QueryWmiAdvanced (scopeInput: ScopeItem) query = 
        let scope = 
            match scopeInput with
            | Scope s -> s
            | Creation (path, requiresDomainSecurity, requiresPacketSecurity) -> createAdvancedScope path requiresDomainSecurity requiresPacketSecurity
            // createAdvancedScope path requiresDomainSecurity requiresPacketSecurity
        let query = new ObjectQuery(query)
        use searcher = new ManagementObjectSearcher(scope, query)
        use results = searcher.Get()
        results |> Seq.cast<ManagementObject>  |> Array.ofSeq