{"id":417,"date":"2014-11-03T00:01:00","date_gmt":"2014-11-03T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/11\/03\/use-powershell-to-start-or-stop-virtual-machines-in-order\/"},"modified":"2014-11-03T00:01:00","modified_gmt":"2014-11-03T00:01:00","slug":"use-powershell-to-start-or-stop-virtual-machines-in-order","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-start-or-stop-virtual-machines-in-order\/","title":{"rendered":"Use PowerShell to Start or Stop Virtual Machines in Order"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about using a Windows PowerShell workflow to start or stop virtual machines in a specific order.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This morning I am sitting outside enjoying a beautiful fall day. I found a little coffee and tea shop nearby, so I am sitting outside sipping a nice cup of English Breakfast tea and munching an apple cinnamon scone. It is not too bad actually. Of course, the little coffee and tea shop has WiFi, so I have my Surface&nbsp;3 Pro with me, and I am checking email sent to <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>. I ran across an email from a person who is trying to start various virtual machines in a particular order on the server running Hyper-V. Hmmm&hellip;That sounds like a job for a Windows PowerShell workflow.<\/p>\n<p><b>Note&nbsp;<\/b> For a good review of Windows PowerShell workflow, refer to <a href=\"\/b\/heyscriptingguy\/archive\/tags\/workflow\/\" target=\"_blank\">this collection of Hey, Scripting Guy! Blog posts<\/a>. I have nearly twenty posts about workflow, so I suggest that you begin with the &ldquo;Workflow for Mere Mortals&rdquo; series.<\/p>\n<h2>Windows PowerShell workflow sequence<\/h2>\n<p>Using a Windows PowerShell workflow sequence is really an easy thing to do. Creating a Windows PowerShell workflow is actually no more difficult than creating a Windows PowerShell function. In fact, instead of using the word <b>function<\/b><i>, <\/i>I use the word <b>workflow<\/b><i>. <\/i>That is about it.<\/p>\n<p>Oh, one more thing&#8230;<\/p>\n<p>I need to know how to create curly braces (or brackets, or tuborgs, or curly fries&#8230;whatever you like to call the characters that are above the left and right square brackets on an EN-US keyboard). The left brace is ASCII 123 and the right brace is ASCII 125. OK. That is the most difficult portion.<\/p>\n<p>I begin my workflow, provide a name for the workflow, and open the script block by typing the left brace. Here is what this looks like:<\/p>\n<p style=\"margin-left:30px\">workflow start-vms<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p>Now, I use the <b>Sequence<\/b> keyword to indicate that I want to perform the tasks in a specific order. The <b>Sequence<\/b> keyword also uses a script block, so it opens with another left curly brace. Here is what this looks like:<\/p>\n<p style=\"margin-left:30px\">Sequence<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/p>\n<p>Now all I need to do is include the <b>Start-VM<\/b> commands in the order in which I want my virtual machines to start. This is shown here:<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Start-vm -Name dc1_nwt<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Start-VM -Name S1_nwt<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Start-VM -Name S2_nwt<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Start-VM -Name C1_nwt&nbsp;<\/p>\n<p>Now, I need to close the script blocks and call the workflow. Here is that portion of the script:<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;}<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">start-vms<\/p>\n<p>The complete script is shown here:<\/p>\n<p style=\"margin-left:30px\">workflow start-vms<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Sequence<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Start-vm -Name dc1_nwt<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Start-VM -Name S1_nwt<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Start-VM -Name S2_nwt<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Start-VM -Name C1_nwt<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">start-vms<\/p>\n<p>I run the script, and a progress bar appears for each virtual machine, as shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-11-03-14-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-11-03-14-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>Stopping the virtual machines<\/h2>\n<p>To stop the virtual machines in a specific order, all I need to do is find the word <b>Start<\/b> and replace it with <b>Stop<\/b>, then reorder the machines. The completed script is shown here:<\/p>\n<p style=\"margin-left:30px\">workflow Stop-VMS<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Sequence<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Stop-VM -Name C1_nwt<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Stop-VM -Name S2_nwt<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Stop-VM -Name S1_nwt<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Stop-VM -Name DC1_nwt<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">Stop-VMS<\/p>\n<p>When I run this, once again, the progress bar appears to let me know how the script is going:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-11-03-14-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-11-03-14-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>That is all there is to using Windows PowerShell to start and to stop virtual machines in a specific order. Workflow Week will continue tomorrow when I will talk about more cool stuff.<\/p>\n<p>I 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=\"mailto: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><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using a Windows PowerShell workflow to start or stop virtual machines in a specific order. Microsoft Scripting Guy, Ed Wilson, is here. This morning I am sitting outside enjoying a beautiful fall day. I found a little coffee and tea shop nearby, so I am sitting outside [&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":[3,45,382],"class_list":["post-417","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-windows-powershell","tag-workflow"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using a Windows PowerShell workflow to start or stop virtual machines in a specific order. Microsoft Scripting Guy, Ed Wilson, is here. This morning I am sitting outside enjoying a beautiful fall day. I found a little coffee and tea shop nearby, so I am sitting outside [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/417","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=417"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/417\/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=417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}