{"id":214,"date":"2007-01-13T22:14:01","date_gmt":"2007-01-14T08:14:01","guid":{"rendered":"https:\/\/anildesai.net\/?p=214"},"modified":"2010-03-11T07:20:49","modified_gmt":"2010-03-11T13:20:49","slug":"automating-virtual-server-using-net","status":"publish","type":"post","link":"https:\/\/anildesai.net\/index.php\/2007\/01\/automating-virtual-server-using-net\/","title":{"rendered":"Automating Virtual Server, Part 2: Automating Virtual Server Using .NET"},"content":{"rendered":"<p><em>This article was first published on <\/em><a href=\"http:\/\/searchservervirtualization.techtarget.com\/\"><em>SearchServerVirtualization.TechTarget.com<\/em><\/a><em>.<\/em><\/p>\n<h2>The .NET Advantage<\/h2>\n<p>In the first article in this series, <em>Automating Virtual Server Using VBScript<\/em>, I provided an overview of Virtual Server\u2019s COM API. If you\u2019re unfamiliar with the object model, I recommend you read that article before continuing with this one. To get started quickly, I presented examples in VBScript format. While this was a simple way to get started, many developers and systems administrators will want to use the features of a type-safe, compiled language for their production automation needs. In this article, I\u2019ll walk through the process of getting started with Virtual Server\u2019s COM API using Visual Studio .NET. And, I\u2019ll provide code samples in both C# and Visual Basic.NET.<\/p>\n<h2>Creating a .NET Application<\/h2>\n<p>In order to make the most of the topics in this article, you\u2019ll need to be familiar with developing simple .NET applications using Visual Studio. If you don\u2019t consider yourself at least a closet developer, I recommend you stick with using VBScript (at least to being with). If you\u2019d like more information about Microsoft\u2019s .NET development technologies, a great starting place is the Microsoft Developer Network (MSDN) .NET site at <a href=\"http:\/\/msdn.microsoft.com\/net\">http:\/\/msdn.microsoft.com\/net<\/a>.<\/p>\n<p>There\u2019s one important requirement that you\u2019ll need to keep in mind when automating Virtual Server using .NET: You must have your COM security settings configured so that your applications will be able to access either the local instance of Virtual Server, or an instance running on another computer. Details related to doing this programmatically are covered in the Virtual Server Programmer\u2019s Guide (a help file that\u2019s automatically installed with Virtual Server). While you\u2019re there, be sure to read details related to threading recommendations.<\/p>\n<p>OK, now that we have the preliminary information out of the way, let\u2019s get started by looking at code that can be using in a C# or VB.NET application. These examples can be used in any type of .NET application, including Windows Forms applications, ASP.NET web applications, command-line applications, and Windows Services.<\/p>\n<h2>Checking Your References<\/h2>\n<p>Once you\u2019ve created a new .NET project, you\u2019ll need to add a reference to the Virtual Server 2005 R2 Type Library. If you\u2019re using Visual Studio, you can easily to this by clicking on Project \u00e0 Add Reference, and then selecting the COM tab (see Figure 1). As long as Virtual Server is installed and registered on the local machine, you should see the type library listed.<\/p>\n<p><a href=\"https:\/\/anildesai.net\/wp-content\/uploads\/2007\/10\/image44.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border-width: 0px;\" src=\"https:\/\/anildesai.net\/wp-content\/uploads\/2007\/10\/image-thumb44.png\" border=\"0\" alt=\"image\" width=\"446\" height=\"337\" \/><\/a><\/p>\n<p><strong>Figure 1: Adding a reference to the \u201cVirtual Server 2005 R2 Type Library\u201d COM object.<\/strong><\/p>\n<p>Since the type library is a COM object (as opposed to a .NET-based \u201cmanaged code\u201d object), Visual Studio will automatically create a COM interoperability layer. The bottom line is that you\u2019ll now be able to access the API by adding one of the following lines to your source code (I\u2019ll use the convention of presenting Visual Basic.NET code samples first, followed by C#):<\/p>\n<blockquote><p>Imports Microsoft.VirtualServer.Interop \u2018Visual Basic Example<\/p><\/blockquote>\n<blockquote><p>using Microsoft.VirtualServer.Interop; \u2018 C# Example<\/p><\/blockquote>\n<p>Connecting to Virtual Server<\/p>\n<p>Most commonly, you\u2019ll start writing your code by creating a Virtual Server object. Here are some code samples for connecting to the local instance of the Virtual Server service:<\/p>\n<blockquote><p>\u2018 Visual Basic Example<\/p>\n<p>Dim objVirtualServer As VMVirtualServer<\/p>\n<p>objVirtualServer = New VMVirtualServer<\/p><\/blockquote>\n<blockquote><p>\u2018C# Example<\/p>\n<p>VMVirtualServer objVirtualServer;<\/p>\n<p>objVirtualServer = new VMVirtualServer();<\/p><\/blockquote>\n<p>You\u2019re now free to start working with the properties and methods of the Virtual Server object. To get more details (as well as details about connecting to remote instances of Virtual Server), see the Virtual Server Programmer\u2019s Guide.<\/p>\n<h2>Working with Virtual Machines<\/h2>\n<p>The most common next step in typical Virtual Server automation code will be to create an object that references a specific virtual machine. This process is easy enough and can be done in a couple of different ways. First, if you know the name of the VM for which you want to create an object, you can simply use the .FindVirtualMachine() method of the Virtual Server object that you\u2019ve created (just provide the name of the VM as a string argument).<\/p>\n<p>If you want to loop through all of the VMs on a particular instance of Virtual Server, you can simply iterate through the .VirtualMachines collection (a property of the Virtual Server object itself). One item will be returned for each VM that is attached to the host. Once you have a reference to a particular VM, you can access properties of the VM object to get and set properties such as the amount of physical memory that\u2019s allocated. And, you can use methods to perform operations such as starting and stopping VMs, and adding and removing virtual hardware. We\u2019ll focus on those topics in later articles in this series.<\/p>\n<h2>Looking to the Future<\/h2>\n<p>So far, we\u2019ve focused on the first steps that are required to get started with building a basic Virtual Server automation application using .NET. Just as a quick example, the process for building a Windows forms application that performs some of the same operations as Virtual Server\u2019s built-in admin tools can be quick and easy. While it\u2019s not exactly a state-of-the-art example of coding finesse, I created a simple but functional demonstration application (shown in Figure 2) in under an hour. And, I chose not to use my favorite .NET language (I\u2019ll let you guess which one that is).<\/p>\n<p><a href=\"https:\/\/anildesai.net\/wp-content\/uploads\/2007\/10\/image45.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border-width: 0px;\" src=\"https:\/\/anildesai.net\/wp-content\/uploads\/2007\/10\/image-thumb45.png\" border=\"0\" alt=\"image\" width=\"455\" height=\"484\" \/><\/a><\/p>\n<p><strong>Figure 2: A simple Virtual Server automation application.<\/strong><\/p>\n<p>The total functional portion of the source code is somewhere around 200 lines (you can download the Visual Studio project from my web site at <a href=\"http:\/\/AnilDesai.net\">http:\/\/AnilDesai.net<\/a>). Note that the source code is provided as-is, and it\u2019s only provided as an example &#8211; it most certainly isn\u2019t production-quality code.<\/p>\n<p><em><strong>Update: <\/strong><\/em>You can now download the source code directly using the following link: <a href=\"https:\/\/anildesai.net\/wp-content\/uploads\/2007\/01\/VirtualServerTestApp-Anil-Desai.zip\">VirtualServerTestApp (Anil Desai)<\/a>.\u00a0 The file includes a zip file with a Visual Studio 2008 project.\u00a0 Be sure to see the earlier posts in this series to determine the pre-requisites for running the application.<\/p>\n<h2>Building on the Foundation<\/h2>\n<p>In this and the previous article, I\u2019ve set the stage for performing more useful tasks. You should now have the ability to connect to an instance of Virtual Server and to create an object for a virtual machine. In the next several articles, I\u2019ll provide details for managing virtual machines, working with virtual hard disks, and configuring virtual networks. We\u2019re just getting warmed up, so stay tuned!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article was first published on SearchServerVirtualization.TechTarget.com. The .NET Advantage In the first article in this series, Automating Virtual Server Using VBScript, I provided an overview of Virtual Server\u2019s COM API. If you\u2019re unfamiliar with the object model, I recommend you read that article before continuing with this one. To get started quickly, I presented [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,23],"tags":[],"class_list":["post-214","post","type-post","status-publish","format-standard","hentry","category-vm-automating-scripting","category-microsoft-virtual-server"],"_links":{"self":[{"href":"https:\/\/anildesai.net\/index.php\/wp-json\/wp\/v2\/posts\/214","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/anildesai.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/anildesai.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/anildesai.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/anildesai.net\/index.php\/wp-json\/wp\/v2\/comments?post=214"}],"version-history":[{"count":2,"href":"https:\/\/anildesai.net\/index.php\/wp-json\/wp\/v2\/posts\/214\/revisions"}],"predecessor-version":[{"id":685,"href":"https:\/\/anildesai.net\/index.php\/wp-json\/wp\/v2\/posts\/214\/revisions\/685"}],"wp:attachment":[{"href":"https:\/\/anildesai.net\/index.php\/wp-json\/wp\/v2\/media?parent=214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/anildesai.net\/index.php\/wp-json\/wp\/v2\/categories?post=214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/anildesai.net\/index.php\/wp-json\/wp\/v2\/tags?post=214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}