mercredi 2 avril 2014

How to run a WMI sub query inside a foreach loop?


Vote count:

0




I am new to WMI. I have this WMI query that gets all the Virtual machine instances running on a remote windows server 2008 r2. Once I get the instance, how do I run a query on it and find the location where its vhd file is stored?



using System;
using System.Management;

public class RemoteConnect
{
public static readonly string VirtualMachineQuery = "SELECT * FROM Msvm_ComputerSystem WHERE __SERVER != Name";

public static void Main()
{

var connectionOptions = new ConnectionOptions
{
Username = @"domain\username",
Password = "password",
Impersonation = ImpersonationLevel.Impersonate,
Authentication = AuthenticationLevel.Packet
};

var path = new ManagementPath
{
ClassName = "",
NamespacePath = @"root\virtualization",
Path = @"\\servername\root\virtualization",
RelativePath = "",
Server = "servername"
};


var mgmtScope =
new ManagementScope(
path, connectionOptions);

mgmtScope.Connect();
Console.WriteLine(mgmtScope.IsConnected);

var search = new ManagementObjectSearcher(
mgmtScope,
new ObjectQuery(VirtualMachineQuery));

ManagementObjectCollection collection = search.Get();

foreach (var instance in collection)
{
//I want to run a query on the VM instance and get its vhd file location
//How do I do that?

foreach (var property in instance.Properties)
{

Console.WriteLine(property.Name);
Console.WriteLine(property.Value);
Console.WriteLine("********************************");

}

}

}

}


asked 1 min ago






Aucun commentaire:

Enregistrer un commentaire