How to get IIS application info via WMI

2019-05-08 03:49发布

How would I get the actual directory path of an IIS application (virtual folder) using WMI?

3条回答
叛逆
2楼-- · 2019-05-08 04:11

Here's a purely .Net option that should be returning the same result as Isalamon's answer.

From my WmiMacros

Sample usage (replace 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)

Far far more detailed usage:

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)
    }

detailed implementation code:

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
查看更多
Deceive 欺骗
3楼-- · 2019-05-08 04:28

Use Scriptomatic V2 tools to view more samples like that :

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

查看更多
小情绪 Triste *
4楼-- · 2019-05-08 04:30

Sure, it's 3 years old, but it's a nice little question. If the specification the solution must use .NET includes PowerShell, then this will do the trick. Someone may want to know some day:

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

The resulting object will contain one property named, "Path".

查看更多
登录 后发表回答