【问题标题】:How to get IIS application info via WMI如何通过 WMI 获取 IIS 应用程序信息
【发布时间】:2009-04-29 10:43:13
【问题描述】:

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

【问题讨论】:

    标签: .net windows iis wmi


    【解决方案1】:

    使用Scriptomatic V2 工具查看更多类似示例:

    出错时继续下一步

    常量 wbemFlagReturnImmediately = &h10 常量 wbemFlagForwardOnly = &h20

    arrComputers = Array("*") 对于 arrComputers 中的每个 strComputer WScript.Echo WScript.Echo "========================================== WScript.Echo "计算机:" & strComputer WScript.Echo "==========================================="

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

    对于 colItems 中的每个 objItem WScript.Echo "GroupComponent:" & objItem.GroupComponent WScript.Echo "PartComponent:" & objItem.PartComponent WScript.Echo 下一个 下一个

    【讨论】:

    • 感谢@lsalamon,这是一个有用的提示,(+1),但我忘了标记我的要求,即它是 .NET 代码。
    • Scriptomatic V2 也创建了 C# 示例。
    【解决方案2】:

    当然,它已经 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”的属性。

    【讨论】:

      【解决方案3】:

      这是一个纯粹的 .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
      

      【讨论】:

      • 我很惊讶直到现在还没有提出 .net 解决方案。野生的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-07
      • 1970-01-01
      • 2012-08-28
      相关资源
      最近更新 更多