{"id":3321,"date":"2013-06-28T00:01:00","date_gmt":"2013-06-28T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/06\/28\/create-a-new-virtual-machine-with-powershell-part-3\/"},"modified":"2013-06-28T00:01:00","modified_gmt":"2013-06-28T00:01:00","slug":"create-a-new-virtual-machine-with-powershell-part-3","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/create-a-new-virtual-machine-with-powershell-part-3\/","title":{"rendered":"Create a New Virtual Machine with PowerShell: Part 3"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary<\/strong><span style=\"font-size: 12px\">: Windows PowerShell MVP and honorary Scripting Guy, Sean Kearney, completes his series about using Windows PowerShell to&nbsp;create a virtual machine.<\/span>\nMicrosoft Scripting Guy, Ed Wilson, is here. If you are a seasoned Hey, Scripting Guy! Blog reader, you know that the most frequent guest blogger is Sean Kearney. If you are new to the blog, I welcome you, and I encourage you to catch up with&nbsp;<a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/sean+kearney\/\" target=\"_blank\">Sean&rsquo;s previous blogs<\/a>.\nSean is a Windows PowerShell MVP and&nbsp;<a href=\"http:\/\/blogs.technet.comhttps:\/\/devblogs.microsoft.com\/scripting\/honorary-scripting-guy-award-recipients-announced\/\" target=\"_blank\">an Honorary Scripting Guy.<\/a>&nbsp;Sean&rsquo;s session at TechEd NA and TechEd Europe this year is&nbsp;<a href=\"https:\/\/channel9.msdn.com\/Events\/TechEd\/NorthAmerica\/2013\/MDC-B326#fbid=rHDRO4Syj3v\" target=\"_blank\">Integrating with Microsoft System Center 2012 and Windows PowerShell<\/a>. In his free time, Sean has written several blog posts about Hyper-V and some other cool stuff, and for the next few weeks, Sean will be the designated guest blogger on Fridays.\nTo read the previous posts in this series, please see:\n<a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/06\/14\/create-a-new-virtual-machine-with-windows-powershell-part-1.aspx\">Create a New Virtual Machine with Windows PowerShell: Part 1<\/a>\n<a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/06\/21\/create-a-new-virtual-machine-with-powershell-part-2.aspx\">Create a New Virtual Machine with Windows PowerShell: Part&nbsp;2<\/a>\nTake it away Sean&hellip;\nMy good friend, Brad, had been playing with making a new virtual machine in Windows Server 2012 with Hyper-V. He wanted to create a machine with the following parameters:<\/p>\n<p style=\"padding-left: 30px\">Name of &ldquo;Brad VM SCSM&rdquo;<\/p>\n<p style=\"padding-left: 30px\">2 gigabytes of RAM<\/p>\n<p style=\"padding-left: 30px\">60 gigabyte hard drive<\/p>\n<p style=\"padding-left: 30px\">2 CPU cores assigned<\/p>\n<p style=\"padding-left: 30px\">Connected to my virtual network called &ldquo;Hyper-V-Prod1&rdquo;\nTo do so, he ran the following commands in Windows PowerShell:<\/p>\n<p style=\"padding-left: 30px\">NEW-VM &ndash;Name &ldquo;Brad VM SCSM&rdquo; &ndash;MemoryStartupBytes 2048 &ndash;NewVHDPath E:VHDBradVMSCSM-C.VHDX &ndash;NewVHDSizeBytes 60000000000<\/p>\n<p style=\"padding-left: 30px\">ADD-VMNetworkAdapter &ndash;VMName &ldquo;Brad VM SCSM&rdquo; &ndash;Switchname &ldquo;Hyper-V-Prod1&rdquo;<\/p>\n<p style=\"padding-left: 30px\">SET-VMProcessor &ndash;VMName &ldquo;Brad VM SCSM&rdquo;\nNow Brad&rsquo;s question became how to make this repeatable. &ldquo;I&rsquo;ve figured out online that if I save this as a .PS1 file with Notepad or the Windows PowerShell ISE, this is technically a Windows PowerShell script. I would like to do two things to make this truly useful:<\/p>\n<ul>\n<li>Avoid editing the text file to change virtual machine names and parameters<\/li>\n<li>Be able to create the virtual machines on any Windows Server 2012 with Hyper-V<\/li>\n<\/ul>\n<p>First, I wanted to show Brad how to change the names to variables for the same script. &ldquo;Wherever you see a value in the script, you can swap it with a variable starting with a dollar sign (<strong>$<\/strong>). Here&rsquo;s your original script changed with appropriately named variables in Windows PowerShell.&rdquo;<\/p>\n<p style=\"padding-left: 30px\">$VMName=&rdquo;Brad VM SCSM&rdquo;<\/p>\n<p style=\"padding-left: 30px\">$Memory=2048<\/p>\n<p style=\"padding-left: 30px\">$NewVHD=&rdquo;E:VHDBradVMSCSM-C.vhdx&rdquo;<\/p>\n<p style=\"padding-left: 30px\">$VHDSize=60000000000<\/p>\n<p style=\"padding-left: 30px\">$Switch=&rdquo;Hyper-V-Prod1&rdquo;<\/p>\n<p style=\"padding-left: 30px\">$Cpu=2<\/p>\n<p style=\"padding-left: 30px\">NEW-VM &ndash;Name $Vmname &ndash;MemoryStartupBytes $Memory &ndash;NewVHDPath $NewVHD &ndash;NewVHDSizeBytes $VHDSize<\/p>\n<p style=\"padding-left: 30px\">ADD-VMNetworkAdapter &ndash;VMName $VMName &ndash;Switchname $Switch<\/p>\n<p style=\"padding-left: 30px\">SET-VMProcessor &ndash;VMName $VMName &ndash;count $Cpu\nBrad&rsquo;s eyes lit up. &ldquo;Oh, that&rsquo;s actually pretty easy. So is there any way we can write this script to accept these as values?&rdquo;\nI looked over. &ldquo;Brad, I&rsquo;d like you to meet parameters. We can simply add <strong>Param<\/strong> before the variables that we want to change, and separate the list with commas like this:&rdquo;<\/p>\n<p style=\"padding-left: 30px\">Param(<\/p>\n<p style=\"padding-left: 30px\">$VMName,<\/p>\n<p style=\"padding-left: 30px\">$Memory,<\/p>\n<p style=\"padding-left: 30px\">$NewVHD,<\/p>\n<p style=\"padding-left: 30px\">$VHDSize,<\/p>\n<p style=\"padding-left: 30px\">$Switch,<\/p>\n<p style=\"padding-left: 30px\">$Cpu=2<\/p>\n<p style=\"padding-left: 30px\">)\n&ldquo;So with these names as parameters, we can launch our NEW-BRADVM.PS1 Script like this:&rdquo;<\/p>\n<p style=\"padding-left: 30px\">.\/NEW-BRADVM.PS1 &ndash;VMName &lsquo;test&rsquo; &ndash;Memory 2048MB -$NewVHD &lsquo;C:VHDMyVHD.VHDX&rsquo; &ndash;VHDSize 10000000000 $Switch &lsquo;Hyper-V-Prod1&rsquo; &ndash;Cpu 4\n&ldquo;Fine. Now how about my bigger request? I&rsquo;d like to run this from one computer to make these kinds of changes to multiple virtual machines running Hyper-V.&rdquo;\n&ldquo;Ah. Such a simple answer. You can supply in the parameter called <strong>ComputerName<\/strong> to most cmdlets. In the case of the Hyper-V cmdlets, this parameter specifies the name of the computer running Hyper-V. So your completed script with the ability to create a virtual machine on any remote computer running Hyper-V would look like this:&rdquo;<\/p>\n<p style=\"padding-left: 30px\">Param(<\/p>\n<p style=\"padding-left: 30px\">$Computername,<\/p>\n<p style=\"padding-left: 30px\">$VMName,<\/p>\n<p style=\"padding-left: 30px\">$Memory,<\/p>\n<p style=\"padding-left: 30px\">$NewVHD,<\/p>\n<p style=\"padding-left: 30px\">$VHDSize,<\/p>\n<p style=\"padding-left: 30px\">$Switch,<\/p>\n<p style=\"padding-left: 30px\">$Cpu=2<\/p>\n<p style=\"padding-left: 30px\">)<\/p>\n<p style=\"padding-left: 30px\">NEW-VM &ndash;Name $Vmname &ndash;MemoryStartupBytes $Memory &ndash;NewVHDPath $NewVHD &ndash;NewVHDSizeBytes $VHDSize &ndash;computername $computername<\/p>\n<p style=\"padding-left: 30px\">ADD-VMNetworkAdapter &ndash;VMName $VMName &ndash;Switchname $Switch &ndash;computername $computername<\/p>\n<p style=\"padding-left: 30px\">SET-VMProcessor &ndash;VMName $VMName &ndash;count $Cpu &ndash;computername $computername\n&ldquo;Here&rsquo;s the cool part, Brad. If I had a list of this information in a .csv file, I can do all this in one line:&rdquo;<\/p>\n<p style=\"padding-left: 30px\">IMPORT-CSV | FOREACH { .\/NEW-BRADVM.PS1 &ndash;VMName $_.Vmname &ndash;Memory $_.Ram -$NewVHD $_.VHDFile &ndash;VHDSize $_.Vhdsize $Switch $_.Switch &ndash;Cpu $_.CpuCount }\n&ldquo;All of that in one line? Why didn&rsquo;t you tell me about Windows PowerShell before?!?!&rdquo;\nSo, it appears that we have yet another person saved by the Power of Shell.\n~Sean\nThanks, Sean. This is awesome stuff. Join me tomorrow for 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: Windows PowerShell MVP and honorary Scripting Guy, Sean Kearney, completes his series about using Windows PowerShell to&nbsp;create a virtual machine. Microsoft Scripting Guy, Ed Wilson, is here. If you are a seasoned Hey, Scripting Guy! Blog reader, you know that the most frequent guest blogger is Sean Kearney. If you are new to 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":[56,271,3,154,45],"class_list":["post-3321","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-hyper-v","tag-scripting-guy","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Windows PowerShell MVP and honorary Scripting Guy, Sean Kearney, completes his series about using Windows PowerShell to&nbsp;create a virtual machine. Microsoft Scripting Guy, Ed Wilson, is here. If you are a seasoned Hey, Scripting Guy! Blog reader, you know that the most frequent guest blogger is Sean Kearney. If you are new to the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3321","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=3321"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3321\/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=3321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}