{"id":3558,"date":"2013-05-28T00:01:00","date_gmt":"2013-05-28T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/05\/28\/use-powershell-to-add-vhds-and-configure-new-virtual-machines\/"},"modified":"2013-05-28T00:01:00","modified_gmt":"2013-05-28T00:01:00","slug":"use-powershell-to-add-vhds-and-configure-new-virtual-machines","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-add-vhds-and-configure-new-virtual-machines\/","title":{"rendered":"Use PowerShell to Add VHDs and Configure New 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 create and to add new virtual hard disks while configuring new virtual machines on Hyper-V.<\/span>\nMicrosoft Scripting Guy, Ed Wilson, is here. Yesterday was a holiday in the U.S. People in Charlotte, North Carolina were outside cooking hot dogs on their barbeque qrills, and on the north side of the city, it was a NASCAR race weekend. Personally, I thought it was a great time to spend at home and play around with Windows PowerShell. <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/05\/23\/scripting-wife-comments-on-beginner-event-5.aspx\" target=\"_blank\">The Scripting Wife<\/a> was also working with Windows PowerShell for her entry for the <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/2013+scripting+games\/\" target=\"_blank\">Scripting Games<\/a>. So, at least around our household, it was a Windows PowerShell holiday.<\/p>\n<h2>Modifying virtual machine storage<\/h2>\n<p>One of the things that I like to do is to create a small virtual disk and attach it to a newly created virtual machine on Hyper-V. (Luckily, I can do all this on my laptop running Windows&nbsp;8). In my previous blog post, <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/05\/26\/weekend-scripter-use-powershell-to-build-multiple-virtual-machines.aspx\" target=\"_blank\">Use PowerShell to Build Multiple Virtual Machines<\/a>,<em> <\/em>I talked about using <strong>New-VM<\/strong>, <strong>Set-VMMemory<\/strong>, and <strong>Set-VMDvdDrive<\/strong> to perform the initial build of a series of virtual machines.\nToday I am going to use three more cmdlets: <strong>New-VHD<\/strong>, <strong>Add-VMHardDiskDrive<\/strong>, and <strong>Set-VM<\/strong>. I will create a new VHD for my virtual machine to use for storage, add it to the virtual machine, and then while I am playing around, I am going to change the default snapshot storage location and change the smart paging file location. In the end, I will provide a link to my modified <strong>Create-VM<\/strong> script in the <a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\" target=\"_blank\">Scripting Guys Script Repository<\/a>.<\/p>\n<h3>Create a new VHD<\/h3>\n<p>To create a new VHD, I use the <strong>New-VHD<\/strong> cmdlet. I need to specify the path to the VHD (including the name for the newly created VHD, in addition to the size of the disk and if I want it to be a dynamic disk. In my command (because I contain it inside a loop that creates the initial virtual machines), I do not have a specific name for the newly created VHD. I will use the name of the virtual machine, and append <strong>disk2.vhdx<\/strong><em> <\/em>to the end of the file name.\nThe thing that is perhaps a bit cool is the use of the escape ( <strong>` <\/strong>) character to separate the variable name <strong>$n<\/strong> from the rest of the text used to create the file name. Here is the command I use:<\/p>\n<p style=\"padding-left: 30px\">New-VHD -Path &#8220;F:VM$nVirtual Hard Disks$n`Disk2.vhdx&#8221; -Dynamic -SizeBytes 50MB<\/p>\n<h3>Add the newly created VHD to the virtual machine<\/h3>\n<p>To add the newly created VHD to the previously created virtual machine, I use the <strong>Add-VMHardDiskDrive<\/strong> cmdlet. I need to provide it with the name of the virtual machine and the path to the VHD. In reality, it is pretty simple. Here is the command:<\/p>\n<p style=\"padding-left: 30px\">ADD-VMHardDiskDrive -VMName $n -Path &#8220;F:VM$nVirtual Hard Disks$n`Disk2.vhdx&#8221;<\/p>\n<h3>Configure snapshot and smart paging locations<\/h3>\n<p>To configure the locations for the snap shot storage and for smart paging, I need to use the <strong>Set-VM<\/strong> cmdlet. When I do this, I simply provide the locations for the appropriate parameters as shown here:<\/p>\n<p style=\"padding-left: 30px\">Set-VM -SnapshotFileLocation &#8220;F:VM$nSnapshots&#8221; &ndash;SmartPagingFilePath &#8220;F:VM$npaging&#8221;<\/p>\n<h2>Finishing up<\/h2>\n<p>So that is it for the modifications to my Create-VM.ps1 script. Not bad for 30 lines of code, including blank lines for spacing. The completed script 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:winblue*client*).fullname)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; New-VHD -Path &#8220;F:VM$nVirtual Hard Disks$n`Disk2.vhdx&#8221; -Dynamic -SizeBytes 50MB<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; ADD-VMHardDiskDrive -VMName $n -Path &#8220;F:VM$nVirtual Hard Disks$n`Disk2.vhdx&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; Get-VM -Name $n |<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set-VM -SnapshotFileLocation &#8220;F:VM$nSnapshots&#8221; -SmartPagingFilePath &#8220;F:VM$npaging&#8221;<\/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;&nbsp; New-VHD -Path &#8220;F:VM$nVirtual Hard Disks$n`Disk2.vhdx&#8221; -Dynamic -SizeBytes 50MB<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; ADD-VMHardDiskDrive -VMName $n -Path &#8220;F:VM$nVirtual Hard Disks$n`Disk2.vhdx&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; Get-VM -Name $n |<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Set-VM -SnapshotFileLocation &#8220;F:VM$nSnapshots&#8221; -SmartPagingFilePath &#8220;F:VM$npaging&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp; }<\/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\nThe complete script is uploaded to the Scripting Guys Script Repository: <a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/Create-and-Configure-a-new-19d2745d\" target=\"_blank\">Create and Configure a New Virtual Machine<\/a>.\nJoin me tomorrow when I will talk about more cool Windows PowerShell stuff.\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.\n<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 create and to add new virtual hard disks while configuring new virtual machines on Hyper-V. Microsoft Scripting Guy, Ed Wilson, is here. Yesterday was a holiday in the U.S. People in Charlotte, North Carolina were outside cooking hot dogs on their barbeque qrills, [&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,130,45],"class_list":["post-3558","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-hyper-v","tag-scripting-guy","tag-servers","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to create and to add new virtual hard disks while configuring new virtual machines on Hyper-V. Microsoft Scripting Guy, Ed Wilson, is here. Yesterday was a holiday in the U.S. People in Charlotte, North Carolina were outside cooking hot dogs on their barbeque qrills, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3558","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=3558"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3558\/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=3558"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3558"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3558"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}