Cómo obtener información de la aplicación IIS a través de WMI
Pregunta
¿Cómo obtendría la ruta real del directorio de una aplicación IIS (carpeta virtual) usando WMI?
Solución
Use Scriptomatic V2 herramientas para ver más muestras así:
On Error Resume NextConst 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
Otros consejos
Claro, tiene 3 años, pero es una buena pregunta. Si la especificación que la solución debe usar .NET incluye PowerShell, entonces esto será suficiente. Alguien puede querer saber algún día:
$server = 'ServerName'
$query = "Select Path From IIsWebVirtualDirSetting WHERE Name = 'W3SVC/1/ROOT'"
Get-WmiObject -namespace "root/microsoftiisv2" -query $query -computername $server -authentication 6
El objeto resultante contendrá una propiedad llamada, " Ruta " ;.
Aquí hay una opción puramente .Net que debería devolver el mismo resultado que la respuesta de Isalamon.
De mi WmiMacros
Ejemplo de uso (reemplace 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)
Uso mucho más detallado:
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)
}
código de implementación detallado:
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