{"id":50833,"date":"2010-03-29T00:01:00","date_gmt":"2010-03-29T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/03\/29\/hey-scripting-guy-virtual-machine-manager-cmdlets-and-windows-powershell\/"},"modified":"2010-03-29T00:01:00","modified_gmt":"2010-03-29T00:01:00","slug":"hey-scripting-guy-virtual-machine-manager-cmdlets-and-windows-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-virtual-machine-manager-cmdlets-and-windows-powershell\/","title":{"rendered":"Hey, Scripting Guy! Virtual Machine Manager Cmdlets and Windows PowerShell"},"content":{"rendered":"<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\"><b><a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/a><\/b><\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\"><b><\/b><\/font><\/font>&nbsp;<\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\"><b>About the author<\/b>: Susan Hill writes content for Virtual Machine Manager. While her early influences include Kernighan and Ritchie, more recently she has broadened her skills under the Windows PowerShell movement. She aspires to be the Gauguin of Windows PowerShell, and in preparation for this, she spends her spare time travelling to warm climates, discovering new wines, and painting.<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\">\n<p><font size=\"3\" face=\"Calibri\">&nbsp;<\/font><\/p>\n<\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">After having a chat with the Microsoft Scripting Guy Ed Wilson about writing a guest blog entry, I was definitely considering my life a little boring compared to the adventures he recounts in his posts. I therefore decided to get myself a virtual life, and promptly looked up pictures of Fiji and other assorted tropical locales. Living in the Seattle area, I can&rsquo;t help but dream of sunny places and palm trees when the dreary gray skies start to get on my nerves. Although the other Saturday it was a balmy 58 degrees, making me dig out my shorts and flip-flops, the weather promptly turned and we had snow flurries on the following Monday. Go figure. <\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">Speaking of a virtual life, I spend my days working in the virtual world of System Center Virtual Machine Manager (VMM) and would like to take a few minutes to introduce you to some concepts you&rsquo;ll need in order to write Windows PowerShell scripts for VMM. This is part one of a four-part series of VMM posts. This post is written for beginners working with VMM cmdlets, and starts with the basics. Over the next few days, we&#8217;ll move virtual machines between hosts and the library, create new virtual machines, and expire virtual machines created for self-service users.<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">VMM is built on Windows PowerShell. This means that all of the administrative tasks you can do through the Administrator Console you can also do using the VMM cmdlets provided with the Windows PowerShell &#8211; Virtual Machine Manager command shell.<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\" face=\"Calibri\">When you start the VMM command shell, before using any of the cmdlets to access your managed environment, you first need to connect to your VMM server. If you start the VMM command shell from the UI, you are automatically connected to the VMM server. However, if you start the VMM command shell from the Start menu, you will need to connect to the server manually. In fact, all of the scripts contained in our <\/font><a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/cc764259.aspx\"><font size=\"3\" face=\"Calibri\">VMM Scripting Guide<\/font><\/a><font size=\"3\"><font face=\"Calibri\"> begin with connecting to the VMM server:<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"CodeBlock\"><span>Get-VMMServer &ndash;ComputerName &ldquo;VMMServer01.Contoso.com&rdquo;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">If you chose to run the VMM cmdlets from the Windows PowerShell command window instead of the VMM command shell, you will need to add the VMM snap-in:<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"CodeBlock\"><span>Add-PSSnapin Microsoft.SystemCenter.VirtualMachineManager<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">You can now see all of the VMM commands available to you by typing the following command:<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"CodeBlock\"><span>Get-Command &ndash;CommandType cmdlet &ndash;PSSnapin Microsoft.SystemCenter.VirtualMachineManager<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\"><font size=\"3\">However, as a colleague pointed out to me the other day, he knows how to see the available commands and how to use the <b>Get-Help<\/b> cmdlet to view the help for the commands, but he wants to know <i>where to start.<\/i> So, here are a few commands that will help you get oriented in your VMM environment. <\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\"><font size=\"3\">We&#8217;ll start by taking a look at the hosts you are currently managing with VMM. After you connect to the VMM server, type the following:<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"CodeBlock\"><span>Get-VMHost<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\"><font size=\"3\">This returns all hosts managed by VMM, whether they are Hyper-V hosts, VMWare ESX Server hosts, or Virtual Server hosts. However, if you&rsquo;re managing several hosts, you will see a lot of information scrolling through your Command Prompt window. If you&rsquo;d like to see just the names of the servers managed by VMM, use the following command:<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"CodeBlock\"><span>Get-VMHost | Format-Table name<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\"><font size=\"3\">Now let&#8217;s take a look at just one host, and change its properties:<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"CodeBlock\"><span># Get the host and store it in the variable $VMHost<\/p>\n<p><\/span><\/p>\n<p class=\"CodeBlock\"><span>$VMHost = Get-VMHost -ComputerName &#8220;VMHost01.Contoso.com&#8221;<\/p>\n<p><\/span><\/p>\n<p class=\"CodeBlock\"><span># Set the host status as unavailable for placement<\/p>\n<p><\/span><\/p>\n<p class=\"CodeBlock\"><span>Set-VMHost -VMHost $VMHost -AvailableForPlacement $FALSE<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\"><font size=\"3\">You can return the host status to A<b>vailable for placement<\/b> by using the following command:<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"CodeBlock\"><span>Set-VMHost -VMHost $VMHost -AvailableForPlacement $TRUE<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\"><font size=\"3\">Now, let&rsquo;s take a look at the virtual machines on those hosts. You can get all of the virtual machines in your VMM environment by using the Get-VM cmdlet: <\/p>\n<p><\/font><\/font><\/p>\n<p class=\"CodeBlock\"><span>Get-VM | Format-Table name, status<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\"><font size=\"3\">Of course, now that you know the names of your hosts, you can get just the virtual machines for a particular host:<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"CodeBlock\"><span>Get-VM &ndash;VMHost &ldquo;VMMHost01.Contoso.com&rdquo; | Format-Table name, status<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\"><font size=\"3\">Building on that concept, we can now manipulate individual virtual machines. For example, we can stop and start a virtual machine:<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"CodeBlock\"><span>Stop-VM -VM &#8220;VM01&#8221;<\/p>\n<p><\/span><\/p>\n<p class=\"CodeBlock\"><span>Start-VM -VM &#8220;VM01&#8221;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\"><font size=\"3\">You can also change the properties of a virtual machine:<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"CodeBlock\"><span># Get the virtual machine<\/p>\n<p><\/span><\/p>\n<p class=\"CodeBlock\"><span>$VM = Get-VM -Name VM01 -VMHost &ldquo;VMMHost01.Contoso.com&rdquo;<\/p>\n<p><\/span><\/p>\n<p class=\"CodeBlock\"><span># Stop the virtual machine<\/p>\n<p><\/span><\/p>\n<p class=\"CodeBlock\"><span>Stop-VM -VM $VM<\/p>\n<p><\/span><\/p>\n<p class=\"CodeBlock\"><span># Change the memory to 1024 MB<\/p>\n<p><\/span><\/p>\n<p class=\"CodeBlock\"><span>Set-VM -VM $VM -MemoryMB 1024<\/p>\n<p><\/span><\/p>\n<p class=\"CodeBlock\"><span># Start the virtual machine<\/p>\n<p><\/span><\/p>\n<p class=\"CodeBlock\"><span>Start-VM -VM $VM<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">I hope this spurs your interest in the world of VMM and what you can do using the VMM command shell. Tomorrow, we&#8217;ll start building scripts that move around your virtual machines.<\/p>\n<p><\/font><\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; About the author: Susan Hill writes content for Virtual Machine Manager. While her early influences include Kernighan and Ritchie, more recently she has broadened her skills under the Windows PowerShell movement. She aspires to be the Gauguin of Windows PowerShell, and in preparation for this, she spends her spare time travelling to warm climates, [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[56,3,91,92,45],"class_list":["post-50833","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-scripting-guy","tag-susan-hill","tag-vmm","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; About the author: Susan Hill writes content for Virtual Machine Manager. While her early influences include Kernighan and Ritchie, more recently she has broadened her skills under the Windows PowerShell movement. She aspires to be the Gauguin of Windows PowerShell, and in preparation for this, she spends her spare time travelling to warm climates, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/50833","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=50833"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/50833\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=50833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=50833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=50833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}