2009-04-29 103 views

回答

2

使用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

+0

謝謝@lsalamon,這是一個有用的提示,(+1),但我忘記標記我的要求,它是.NET代碼。 – ProfK 2009-09-20 15:53:14

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 

生成的對象將包含一個名爲「路徑」一個屬性。

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 
+0

我很驚訝沒有.net解決方案,直到現在。野生。 – Maslow 2014-09-30 13:11:56