{"id":209,"date":"2007-01-12T22:10:38","date_gmt":"2007-01-13T08:10:38","guid":{"rendered":"https:\/\/anildesai.net\/?p=209"},"modified":"2007-10-26T22:27:56","modified_gmt":"2007-10-27T03:27:56","slug":"automating-virtual-server-using-vbscript","status":"publish","type":"post","link":"https:\/\/anildesai.net\/index.php\/2007\/01\/automating-virtual-server-using-vbscript\/","title":{"rendered":"Automating Virtual Server, Part 1: Automating Virtual Server Using VBScript"},"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>Series Introduction<\/h2>\n<p>Among the many benefits of deploying virtualization technology is the ability to better manage complex environments. When implemented correctly, it\u2019s often easier to manage virtual machines than it is to keep track of physical ones. But what happens when you end up with a large number of VMs. Organizations that start by dabbling in virtualization often find that the number of virtualization hosts and guest OS\u2019s can quickly get out-of-hand. Add to that the fact that virtualization management tools are still evolving, and you can end up with quite a challenge for systems administrators.  <\/p>\n<p>When dealing with physical servers, many IT pros and end-users have found ways to use automated methods of handling otherwise tedious tasks. For example, performing backups and deploying operating system updates are generally done manually in only the smallest of environments.  <\/p>\n<p>When it comes managing Microsoft Virtual Server VMs, there\u2019s a readily available method for automation. In this series of articles, <i>Automating Virtual Server<\/i>, I\u2019ll present details and examples of how you can programmatically manage your VMs. Code samples will be provided in VBScript, Visual Basic .NET, and C# syntax. Some experience with scripting and application development is assumed, but I\u2019ll provide links to relevant information if you need to get up to speed. Overall, I think you\u2019ll find that the process is fairly easy and straight-forward.  <\/p>\n<h2>Automating Virtualization<\/h2>\n<p>If you\u2019re a systems administrator or developer that\u2019s faced with managing a large number of virtual machines, there\u2019s a good chance that you find yourself wishing for a robot that could handle some of the more mundane tasks. While building such a device is more than could be covered in a single article, the focus here is to demonstrate how you can create scripts for performing routine tasks in Virtual Server. We\u2019ll begin by using the VBScript language in this article, since it\u2019s the easiest way to get up and running. In later articles, I\u2019ll provide code samples in .NET languages (C# and Visual Basic.NET).  <\/p>\n<h2>Virtual Server\u2019s COM API<\/h2>\n<p>One of the built-in features of Microsoft Virtual Server is a fully-documented Component Object Model (COM) API. By connecting to this API, you can perform pretty much any task that can be carried out manually using Virtual Server\u2019s built-in tools. In fact, the Virtual Server Administration Web Site itself is simply a web-based user interface that connects to the COM API behind the scenes.  <\/p>\n<p>The basic structure of the COM API is based on collections. Figure 1 provides an overview of the some of the most commonly-used ones. The figure is not a complete object model, and the full details can be found in the Virtual Server Programmer\u2019s Guide (a help file that\u2019s automatically installed with the product). The Programmer\u2019s Guide includes details about all of the objects that are available, along with a list of properties, methods and enumeration constants. If you\u2019re doing more than basic scripting, you\u2019ll probably find yourself referring to that file often.  <\/p>\n<p><a href=\"https:\/\/anildesai.net\/wp-content\/uploads\/2007\/10\/image43.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px\" height=\"282\" alt=\"image\" src=\"https:\/\/anildesai.net\/wp-content\/uploads\/2007\/10\/image-thumb43.png\" width=\"464\" border=\"0\"><\/a>  <\/p>\n<p><strong>Figure 1: An overview of the Virtual Server COM object model<\/strong>  <\/p>\n<h2>Getting Started with VBScript<\/h2>\n<p>It\u2019s beyond the scope of this article to cover the details of the VBScript language. If you\u2019re not familiar with it, some good starting points for more information are the Microsoft Developer Network (MSDN) Scripting site (<a href=\"http:\/\/msdn.microsoft.com\/scripting\">http:\/\/msdn.microsoft.com\/scripting<\/a>) and the Microsoft TechNet Script Center (<a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/default.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter<\/a>).  <\/p>\n<p>VBScript is based on the Visual Basic language syntax, and it\u2019s a very simple language for getting started with automation. The language is not case-sensitive and it provides for automatic data type conversions. These are two \u201cfeatures\u201d that can help get you up and running quickly (though they\u2019re not good practices when writing production applications).  <\/p>\n<p>Another advantage of writing VBScript code is that there\u2019s no requirement for a development environment: Just use your favorite text editor. In most cases, you can copy and paste the code in this article and use it directly on a Windows computer that\u2019s running Virtual Server. OK, let\u2019s get started.  <\/p>\n<h2>Connecting to Virtual Server<\/h2>\n<p>Generally, the first programmatic step you\u2019ll take when creating a new VBScript file for automating Virtual Server is to create a new object that attaches to the COM API. The following statement takes care of this:  <\/p>\n<blockquote>\n<p>Set objVirtualServer = CreateObject(\u201cVirtualServer.Application\u201d)<\/p>\n<\/blockquote>\n<p>As long as the Virtual Server product is installed on the local computer, this script should run as-is. By default, this will connect to the instance of Virtual Server that is running on the local machine. If you want to connect to an instance of Virtual Server that\u2019s running on a remote computer, you can use a link such as the following:  <\/p>\n<blockquote>\n<p>Set objVirtualServer = CreateObject(&#8220;VirtualServer.Application&#8221;,&#8221;RemoteServerName&#8221;)<\/p>\n<\/blockquote>\n<p>Note that you\u2019ll need to have the appropriate security settings and configuration to be able to remotely connect to a COM object (you can get details on these topics using the links provided earlier).  <\/p>\n<p>Let\u2019s prove that everything\u2019s working by adding the following two lines to get some information about the local Virtual Server instance:  <\/p>\n<blockquote>\n<p>WScript.Echo \u201cServer Name: \u201c &amp; objVirtualServer.Name  <\/p>\n<p>WScript.Echo \u201cServer Uptime (sec): \u201c &amp; objVirtualServer.Uptime<\/p>\n<\/blockquote>\n<p>Now, the script will output the name of the instance (which should be the same as the machine name), along with the number of seconds that the Virtual Server service has been running. We did this by accessing properties of our \u201cobjVirtualServer\u201d object. You can use other methods and properties of the Virtual Server object to perform tasks such as programmatically creating virtual hard disks and changing server settings.  <\/p>\n<h2>Working with Virtual Machines<\/h2>\n<p>Many of the most useful scripts you\u2019ll write will for performing tasks with Virtual Server VMs. You do this by creating a virtual machine object and then setting it equal to a particular VM. If you know the name of a particular VM, you can use the following syntax:  <\/p>\n<blockquote>\n<p>Set objVirtualServer = CreateObject(\u201cVirtualServer.Application\u201d)  <\/p>\n<p>objVirtualMachine = objVirtualServer.FindVirtualMachine(\u201cWindows XP SP2\u201d)<\/p>\n<\/blockquote>\n<p>First, you create a Virtual Server object (as we did in the previous section), and then you use its find method to create a virtual machine object. What if you don\u2019t know the name of the VM you want to access? You can easily loop through the VMs in the Virtual Server object\u2019s \u201cVirtualMachines\u201d collection. Here\u2019s a sample that outputs the name of each VM on the server:  <\/p>\n<blockquote>\n<p>Set objVirtualServer = CreateObject(\u201cVirtualServer.Application\u201d)  <\/p>\n<p>For Each objVirtualMachine in objVirtualServer.VirtualMachines  <\/p>\n<p>WScript.Echo(\u201cName of VM: \u201c) &amp; objVirtualMachine.Name  <\/p>\n<p>Next<\/p>\n<\/blockquote>\n<p>Once you have a \u201chandle\u201d to a virtual machine, you can access and modify its properties. One commonly-used function is to determine the amount of memory attached to a VM:  <\/p>\n<blockquote>\n<p>WScript.Echo \u201cVM Memory: \u201c &amp; objVirtualMachine.Memory<\/p>\n<\/blockquote>\n<p>Of course, you can perform useful tasks like starting or stopping the VM and managing the virtual hardware configuration (those are topics that we\u2019ll cover in later articles in this series).  <\/p>\n<h2>The End of the Beginning<\/h2>\n<p>In this article, I provided a few simple lines of VBScript code that allowed you to connect to Virtual Server and to work with VMs. Admittedly, we haven\u2019t done anything that\u2019s useful in a practical sense just yet. The goal of this article was to lay the foundation for getting started with automation. Next in the series: Automating Virtual Server using .NET.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article was first published on SearchServerVirtualization.TechTarget.com. Series Introduction Among the many benefits of deploying virtualization technology is the ability to better manage complex environments. When implemented correctly, it\u2019s often easier to manage virtual machines than it is to keep track of physical ones. But what happens when you end up with a large number [&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-209","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\/209","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=209"}],"version-history":[{"count":0,"href":"https:\/\/anildesai.net\/index.php\/wp-json\/wp\/v2\/posts\/209\/revisions"}],"wp:attachment":[{"href":"https:\/\/anildesai.net\/index.php\/wp-json\/wp\/v2\/media?parent=209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/anildesai.net\/index.php\/wp-json\/wp\/v2\/categories?post=209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/anildesai.net\/index.php\/wp-json\/wp\/v2\/tags?post=209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}