{"id":3578,"date":"2013-05-26T00:01:00","date_gmt":"2013-05-26T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/05\/26\/weekend-scripter-use-powershell-to-build-multiple-virtual-machines\/"},"modified":"2013-05-26T00:01:00","modified_gmt":"2013-05-26T00:01:00","slug":"weekend-scripter-use-powershell-to-build-multiple-virtual-machines","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-use-powershell-to-build-multiple-virtual-machines\/","title":{"rendered":"Weekend Scripter: Use PowerShell to Build Multiple Virtual Machines"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary<\/strong><span style=\"font-size: 12px\">: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to build multiple virtual machines with Hyper-V.<\/span>\nMicrosoft Scripting Guy, Ed Wilson, is here. One of the really cool things that I love about Windows&nbsp;8 is having real-live, honest-to-goodness, Hyper-V built in to the operating system. And of course, that means also having the Windows PowerShell cmdlets to manage it. With the power of modern laptops (the Scripting Wife has 32&nbsp;GB of RAM, a solid-state drive, and a SATA 3 drive on her new laptop), running multiple virtual machines is a real option&mdash;and a real treat.\nThe other day, I needed to create three virtual machines to test my instructor-led lab scenario that I am creating for TechEd in <a href=\"http:\/\/northamerica.msteched.com\/#fbid=fii1JDbUFpU\" target=\"_blank\">New Orleans<\/a> and <a href=\"http:\/\/europe.msteched.com\/#fbid=Y6s0qEgGIo3\" target=\"_blank\">Madrid<\/a>. Because I am in the process of redoing my network, I do not have access to any advanced deployment tools. It&rsquo;s just me, my laptop, and Windows PowerShell. Because my objective is to build three virtual machines (not to write the perfect script), I decided to knock out a quick script. The result is shown here:<\/p>\n<p style=\"padding-left: 30px\">$name = &#8220;Client1&#8243;,&#8221;Server1&#8243;,&#8221;Server2&#8221;<\/p>\n<p style=\"padding-left: 30px\">$name |<\/p>\n<p style=\"padding-left: 30px\">Foreach {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; new-vm $_ -MemoryStartupBytes 512MB -SwitchName InternalSwitch `<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; -BootDevice cd -NewVHDPath &#8220;F:VM$_Virtual Hard Disks$_.vhdx&#8221; `<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; -NewVHDSizeBytes 127GB }<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Foreach($n in $name)<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;if($n -match &#8216;client&#8217;)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; Set-VMMemory -VMName $n -DynamicMemoryEnabled $true -MinimumBytes 512MB -MaximumBytes 4096MB -StartupBytes 2048MB -Buffer 20<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; Set-VMDvdDrive -VMName $n -Path $((Get-Item F:winsource*client*).fullname)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; }<\/p>\n<p style=\"padding-left: 30px\">&nbsp;if($n -match &#8216;server&#8217;)<\/p>\n<p style=\"padding-left: 30px\">&nbsp; {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; Set-VMMemory -VMName $n -DynamicMemoryEnabled $true -MinimumBytes 512MB -MaximumBytes 2048MB -StartupBytes 1024MB -Buffer 20<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; Set-VMDvdDrive -VMName $n -Path $((Get-Item F:winsource*server*).fullname)<\/p>\n<p style=\"padding-left: 30px\">&nbsp; }<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Get-VM $name | Start-VM<\/p>\n<h2>Three important cmdlets<\/h2>\n<p>Because I only needed to create three virtual machines, I hard coded the machine names into a variable. I also thought it was a good idea to use easy-to-remember names. This part of the code is no surprise:<\/p>\n<p style=\"padding-left: 30px\">$name = &#8220;Client1&#8243;,&#8221;Server1&#8243;,&#8221;Server2&#8221;<\/p>\n<h3>New-VM<\/h3>\n<p>Now I come to the first important cmdlet: <strong>New-VM<\/strong>. The <strong>New-VM<\/strong> cmdlet creates a new virtual machine. The basic configuration for all three virtual machines is the same. I specify the startup memory, the name of the switch, the boot device, the path to the VHD, and the size of the VHD. I pipe the three computer names to the <strong>New-VM<\/strong> cmdlet, and I end up with three new virtual machines. This portion of the code is shown here:<\/p>\n<p style=\"padding-left: 30px\">$name |<\/p>\n<p style=\"padding-left: 30px\">Foreach {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; new-vm $_ -MemoryStartupBytes 512MB -SwitchName InternalSwitch `<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; -BootDevice cd -NewVHDPath &#8220;F:VM$_Virtual Hard Disks$_.vhdx&#8221; `<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; -NewVHDSizeBytes 127GB }<\/p>\n<h3>Set-VMMemory and Set-VMDvdDrive<\/h3>\n<p>Now I need to walk through the computer names by using the <strong>ForEach<\/strong> command. For each of the computer names, I will set the virtual machine memory and the DVD drive information. But because I will have different memory and different DVD info depending on if the operating system is a client or a server, I use the <strong>If <\/strong>statement.\nTo set the memory, I use the <strong>Set-VMMemory<\/strong> cmdlet, and I specify that I want to use dynamic memory in addition to the minimum and maximum bytes. I also set the startup memory. Next, I set the DVD drive to point to the ISO of the operating system I want to use.\nThis portion of the code is shown here:<\/p>\n<p style=\"padding-left: 30px\">Foreach($n in $name)<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;if($n -match &#8216;client&#8217;)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; Set-VMMemory -VMName $n -DynamicMemoryEnabled $true -MinimumBytes 512MB -MaximumBytes 4096MB -StartupBytes 2048MB -Buffer 20<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; Set-VMDvdDrive -VMName $n -Path $((Get-Item F:winblue*client*).fullname)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; }<\/p>\n<p style=\"padding-left: 30px\">&nbsp;if($n -match &#8216;server&#8217;)<\/p>\n<p style=\"padding-left: 30px\">&nbsp; {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; Set-VMMemory -VMName $n -DynamicMemoryEnabled $true -MinimumBytes 512MB -MaximumBytes 2048MB -StartupBytes 1024MB -Buffer 20<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; Set-VMDvdDrive -VMName $n -Path $((Get-Item F:winblue*server*).fullname)<\/p>\n<p style=\"padding-left: 30px\">&nbsp; }<\/p>\n<h2>Start me up<\/h2>\n<p>The last thing to do is to start the newly created virtual machines. To do this, I use the <strong>Get-VM<\/strong> cmdlet and supply the three computer names that are stored in the <strong>$Name<\/strong> variable. I pipe the results to the <strong>Start-VM<\/strong> cmdlet. Now, I still have to walk through the wizard, but that actually goes pretty quickly on modern operating systems.\nJoin me tomorrow because I have an awesome guest blog about WSUS by Honorary Scripting Guy, Boe Prox. You don&rsquo;t want to miss it&mdash;the post absolutely rocks!\nI invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><span style=\"font-size: 12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to build multiple virtual machines with Hyper-V. Microsoft Scripting Guy, Ed Wilson, is here. One of the really cool things that I love about Windows&nbsp;8 is having real-live, honest-to-goodness, Hyper-V built in to the operating system. And of course, that means also having the [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[271,3,61,45,368],"class_list":["post-3578","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-hyper-v","tag-scripting-guy","tag-weekend-scripter","tag-windows-powershell","tag-windows-server-2012"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to build multiple virtual machines with Hyper-V. Microsoft Scripting Guy, Ed Wilson, is here. One of the really cool things that I love about Windows&nbsp;8 is having real-live, honest-to-goodness, Hyper-V built in to the operating system. And of course, that means also having the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3578","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=3578"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3578\/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=3578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}