{"id":220,"date":"2007-02-28T22:24:48","date_gmt":"2007-03-01T08:24:48","guid":{"rendered":"https:\/\/anildesai.net\/?p=220"},"modified":"2007-02-28T22:24:48","modified_gmt":"2007-03-01T08:24:48","slug":"automating-virtual-server-part-4-creating-and-managing-vms","status":"publish","type":"post","link":"https:\/\/anildesai.net\/index.php\/2007\/02\/automating-virtual-server-part-4-creating-and-managing-vms\/","title":{"rendered":"Automating Virtual Server, Part 4: Creating and Managing VMs"},"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<p>If you\u2019ve been following along with my series on \u201cAutomating Virtual Server\u201d so far, you\u2019re familiar with the process of connecting to an instance of Virtual Server and accessing a virtual machine object (either by name or by looping through a collection of all VMs on the server). I provided examples of code in VBScript, Visual Basic .NET, and C#. In this article, I\u2019ll walk through the process of creating new VMs, and managing some basic VM properties.  <\/p>\n<h2>Creating a new VM<\/h2>\n<p>Since the point of virtualization is to run VMs, it should come as no surprise that the Virtual Server object contains a collection of virtual machines. Let\u2019s walk through the most common operations related to working with VMs. When writing code, you\u2019ll generally first create an instance of a Virtual Server host object by connecting to the host. To create a new VM, you can use the following method:  <\/p>\n<blockquote>\n<p>.CreateVirtualMachine(configurationName, configurationPath)<\/p>\n<\/blockquote>\n<p>You need only provide two arguments to this method:  <\/p>\n<ul>\n<li><b>configurationName<\/b>: A string value that specifies the logical name of the new virtual machine. The name must be unique on each instance of Virtual Server, so it\u2019s a good idea to check the .VirtualMachines collection for potential conflicts. Note that this name will also be used to create the default filenames of the VM. <\/li>\n<li><b>configurationPath<\/b>: A string that specifies the full path to the host folder into which the new VM files will be placed. It\u2019s a good idea to test that the Virtual Server service can access this path. If the specific folder does not exist, the Virtual Server COM API will create it automatically. <\/li>\n<\/ul>\n<p>It\u2019s important to keep in mind that different instances of Virtual Server might have different configuration settings. Remember that you can get information about default paths and search paths by using properties of the Virtual Server object.  <\/p>\n<h2>Deleting a Virtual Machine<\/h2>\n<p>From time to time, it will be necessary to delete a virtual machine and its associated files. The code requires a call to a method of the Virtual Server object:  <\/p>\n<blockquote>\n<p>.DeleteVirtualMachine(<i>VirtualMachineObject<\/i>)<\/p>\n<\/blockquote>\n<p>The argument here is actually a virtual machine object. So, your code will need to get an instance of a VM object by looping through the .VirtualMachines collection or by finding it by name. There\u2019s one important warning: Calling this method will actually result in the deletion of the relevant virtual machine configuration (.vmc) file, along with any undo disks or saved state files. Use with caution!  <\/p>\n<h2>Registering VMs<\/h2>\n<p>Adding and removing virtual machines from the Virtual Server instance is also a simple process. Registration involves pointing Virtual Server to an existing virtual machine configuration (.vmc) file. Virtual Server will try to enumerate the virtual hard disks and other properties stored in that file and add the VM with a given name. The method call is:  <\/p>\n<blockquote>\n<p>.RegisterVirtualMachine(<i>configurationName, configurationPath<\/i>)<\/p>\n<\/blockquote>\n<p>Note that the syntax is similar to the of creating a new VM \u2013 you need to provide the logical name that will be used for the VM, along with the full path and filename of the .vmc file.  <\/p>\n<h3>\n<h2>Unregistering VMs<\/h2>\n<\/h3>\n<p>If you want to remove a virtual machine from Virtual Server without deleting the associated files, you can use this method:  <\/p>\n<blockquote>\n<p>objVirtualServer.UnregisterVirtualMachine(<i>VirtualMachineObject<\/i>)<\/p>\n<\/blockquote>\n<p>As with deleting virtual machines, you need to provide an instance of a virtual machine object. This method is handy when you want to move or copy a virtual machine to another server.  <\/p>\n<h2>Code Examples: Managing Virtual Machines<\/h2>\n<p>So far, we\u2019ve looked at some pseudo-code that shows you how to perform common operations related to adding and removing VMs. Now, let\u2019s look at specific working examples. The following code will perform these steps:  <\/p>\n<ul>\n<li>1) Create a new VM called \u201cVSAutomation\u201d. The files will be stored in \u201cC:\\VM\\VSAutomation\u201d (note that you\u2019ll most likely want to change this path if you plan to run the code yourself). <\/li>\n<li>2) Unregister this VM from the Virtual Server installation. <\/li>\n<li>3) Copy all VM-related files to a new folder: VSAutomation2. <\/li>\n<li>4) Register the new VM in Virtual Server under the logical name \u201cVSAutomation2\u201d. <\/li>\n<\/ul>\n<p>Here are the code samples in VBScript, Visual Basic .NET and C# formats, respectively:  <\/p>\n<blockquote>\n<p>Set objVirtualServer = CreateObject(&#8220;VirtualServer.Application&#8221;)  <\/p>\n<p>&#8216;Create a new VM named &#8220;VSAutomation &#8221;  <\/p>\n<p>objVirtualServer.CreateVirtualMachine &#8220;VSAutomation&#8221;, &#8220;C:\\VM\\VSAutomation&#8221;  <\/p>\n<p>&#8216;Get a Virtual Machine object  <\/p>\n<p>Set objVirtualMachine = objVirtualServer.FindVirtualMachine(&#8220;VSAutomation&#8221;)  <\/p>\n<p>&#8216;Unregister the new VM  <\/p>\n<p>objVirtualServer.UnregisterVirtualMachine(objVirtualMachine)  <\/p>\n<p>&#8216;Copy the Virtual Machine files to the folder &#8220;VSAutomation2&#8221;  <\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)  <\/p>\n<p>objFSO.CopyFolder &#8220;C:\\VM\\VSAutomation&#8221;, &#8220;C:\\VM\\VSAutomation2&#8221;, OverWriteFiles  <\/p>\n<p>&#8216;Register the new copy of the virtual machine  <\/p>\n<p>objVirtualServer.RegisterVirtualMachine &#8220;VSAutomation2&#8221;, &#8220;C:\\VM\\VSAutomation2&#8221;  <\/p>\n<p>WScript.Echo &#8220;Operation complete.&#8221; <\/p>\n<\/blockquote>\n<p><b>Listing 1: Managing Virtual Machines using VBScript<\/b>  <\/p>\n<blockquote>\n<p>Imports Microsoft.VirtualServer.Interop  <\/p>\n<p>Namespace SearchServerVirtualization_VB  <\/p>\n<p>Public Class Listing_2  <\/p>\n<p>Public Shared Function ManageVirtualMachines() As String  <\/p>\n<p>Dim objVirtualServer As New VMVirtualServer  <\/p>\n<p>&#8216;Create a new VM named &#8220;VSAutomation&#8221;  <\/p>\n<p>objVirtualServer.CreateVirtualMachine(&#8220;VSAutomation&#8221;, &#8221; C:\\VM\\VSAutomation&#8221;)  <\/p>\n<p>&#8216;Get a Virtual Machine object  <\/p>\n<p>Dim objVirtualMachine As VMVirtualMachine  <\/p>\n<p>objVirtualMachine = objVirtualServer.FindVirtualMachine(&#8220;VSAutomation&#8221;)  <\/p>\n<p>&#8216;Unregister the new VM  <\/p>\n<p>objVirtualServer.UnregisterVirtualMachine(objVirtualMachine)  <\/p>\n<p>&#8216;Copy the Virtual Machine files to the folder &#8220;Test-Copy&#8221;  <\/p>\n<p>My.Computer.FileSystem.CopyDirectory(&#8220;C:\\VM\\VSAutomation&#8221;, &#8220;C:\\VM\\VSAutomation2&#8221;)  <\/p>\n<p>&#8216;Register the new copy of the virtual machine  <\/p>\n<p>objVirtualServer.RegisterVirtualMachine(&#8220;VSAutomation2&#8221;, &#8220;C:\\VM\\VSAutomation2&#8221;)  <\/p>\n<p>Return &#8220;Operation complete.&#8221;  <\/p>\n<p>End Function  <\/p>\n<p>End Class  <\/p>\n<p>End Namespace <\/p>\n<\/blockquote>\n<p><b>Listing 2: Managing Virtual Machines using VB.NET<\/b>  <\/p>\n<blockquote>\n<p>using Microsoft.VirtualServer.Interop;  <\/p>\n<p>namespace SearchServerVirtualization_CSharp  <\/p>\n<p>{  <\/p>\n<p>namespace ScriptingVirtualServer  <\/p>\n<p>{  <\/p>\n<p>public class Listing_3  <\/p>\n<p>{  <\/p>\n<p>public static string ManageVirtualMachines()  <\/p>\n<p>{  <\/p>\n<p>VMVirtualServer objVirtualServer = new VMVirtualServer();  <\/p>\n<p>\/\/Create a new VM named &#8220;VSAutomation&#8221;  <\/p>\n<p>objVirtualServer.CreateVirtualMachine(&#8220;VSAutomation&#8221;, @&#8221;C:\\VM\\VSAutomation&#8221;);  <\/p>\n<p>\/\/Get a Virtual Machine object  <\/p>\n<p>VMVirtualMachine objVirtualMachine;  <\/p>\n<p>objVirtualMachine = objVirtualServer.FindVirtualMachine(&#8220;VSAutomation&#8221;);  <\/p>\n<p>\/\/Unregister the new VM  <\/p>\n<p>objVirtualServer.UnregisterVirtualMachine(objVirtualMachine);  <\/p>\n<p>\/\/Create a new folder called &#8220;Test-Copy&#8221; and move the .vmc file  <\/p>\n<p>System.IO.Directory.CreateDirectory(@&#8221;C:\\VM\\VSAutomation2&#8243;);  <\/p>\n<p>System.IO.File.Move(@&#8221;C:\\VM\\VSAutomation\\VSAutomation.vmc&#8221;, @&#8221; C:\\VM\\VSAutomation2\\VSAutomation2.vmc &#8220;);  <\/p>\n<p>\/\/Register the new copy of the virtual machine  <\/p>\n<p>objVirtualServer.RegisterVirtualMachine(&#8220;VSAutomation&#8221;, @&#8221;C:\\VM\\VSAutomation2\\VSAutomation2.vmc &#8220;);  <\/p>\n<p>return &#8220;Operation complete.&#8221;;  <\/p>\n<p>}  <\/p>\n<p>}  <\/p>\n<p>}  <\/p>\n<p>} <\/p>\n<\/blockquote>\n<p><b>Listing 3: Managing Virtual Machines using C#<\/b>  <\/p>\n<h2>Summary<\/h2>\n<p>In this article, I walked through details related to creating new VMs and managing VM registration in Virtual Server. It only takes a little imagination to see how the code samples could be used to programmatically move and copy VMs between installations of Virtual Server. While I left out important aspects such as error-handling, logging, security-checking, and other details, this should serve as a great foundation for performing useful automation tasks.  <\/p>\n<p>In the next article in this series, I\u2019ll provide details about how you can work with VMs, including diving down into their virtual hardware configurations. Stay tuned!  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article was first published on SearchServerVirtualization.TechTarget.com. If you\u2019ve been following along with my series on \u201cAutomating Virtual Server\u201d so far, you\u2019re familiar with the process of connecting to an instance of Virtual Server and accessing a virtual machine object (either by name or by looping through a collection of all VMs on the server). [&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-220","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\/220","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=220"}],"version-history":[{"count":0,"href":"https:\/\/anildesai.net\/index.php\/wp-json\/wp\/v2\/posts\/220\/revisions"}],"wp:attachment":[{"href":"https:\/\/anildesai.net\/index.php\/wp-json\/wp\/v2\/media?parent=220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/anildesai.net\/index.php\/wp-json\/wp\/v2\/categories?post=220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/anildesai.net\/index.php\/wp-json\/wp\/v2\/tags?post=220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}