ArtinSoft's Blogs

Software Migration Experts
Welcome to ArtinSoft's Blogs Sign in | Join | Help
in Search

Jose Aguilar's Blog

All things migrations, software and technology

Virtual Server, WMI and C#

This code snippet allows you to connect to a remote instance of Virtual Server 2005 R2, via WMI, and retrieve all the Virtual Machine WMI objects.

   1:   
   2:  System.Management.ConnectionOptions co = 
   3:      new System.Management.ConnectionOptions();
   4:  co.Username = user;
   5:  co.Password = password;
   6:   
   7:  ManagementScope scope = new ManagementScope(
   8:      new ManagementPath(@"\\"+computer+
            @"\root\vm\virtualserver"),co);
   9:   
  10:  scope.Connect();
  11:  SelectQuery oq = 
  12:      new SelectQuery("SELECT * FROM VirtualMachine");
  13:   
  14:  ManagementObjectSearcher searcher =
  15:      new ManagementObjectSearcher(scope, oq);
  16:   
  17:  foreach (ManagementObject queryObj in searcher.Get())
  18:  {
  19:       //Do something
  20:  }
 

You need to provide a user and password with enough credentials to query the Virtual Server instance.
BTW, if you ever need to format your code for display on the web, check out this website.

Published Aug 11 2006, 01:18 PM by Jaguilar
Filed under:

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server (Non-Commercial Edition), by Telligent Systems