{"id":2987,"date":"2013-08-23T00:01:00","date_gmt":"2013-08-23T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/08\/23\/powershell-workflow-for-mere-mortals-part-5\/"},"modified":"2013-08-23T00:01:00","modified_gmt":"2013-08-23T00:01:00","slug":"powershell-workflow-for-mere-mortals-part-5","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powershell-workflow-for-mere-mortals-part-5\/","title":{"rendered":"PowerShell Workflow for Mere Mortals: Part 5"},"content":{"rendered":"<p><strong>Summary<\/strong>: Microsoft Scripting Guy, Ed Wilson, concludes his five-part series about Windows PowerShell Workflow.<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy! I have a number of commands that I want to run against several remote servers. The commands include stuff that must happen prior to something else happening. But then, there are also some things that I would like to happen as fast as possible. Is this permissible? If so, do I have to write two different workflows?<\/p>\n<p>&mdash;TB<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello TB,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This afternoon I am sipping an awesome cup of Oolong tea with a cinnamon stick, jasmine flower, and lemon grass. The flavor is just about perfect. In the background, I am listening to <a href=\"http:\/\/en.wikipedia.org\/wiki\/Maurice_Ravel\" target=\"_blank\">Ravel<\/a>. Outside, the sky is dark and it is raining. The thunder seems to punctuate the music.<\/p>\n<p><strong>Note<\/strong>&nbsp;&nbsp;This is the last post in a five-part series about Windows PowerShell Workflow for &ldquo;mere mortals.&rdquo; Before you read this post, please read:&nbsp;<\/p>\n<ul>\n<li><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/08\/19\/powershell-workflow-for-mere-mortals-part-1.aspx\" target=\"_blank\">PowerShell Workflow for Mere Mortals: Part 1<\/a><\/li>\n<li><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/08\/20\/powershell-workflow-for-mere-mortals-part-2.aspx\" target=\"_blank\">PowerShell Workflow for Mere Mortals: Part 2<\/a><\/li>\n<li><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/08\/21\/powershell-workflow-for-mere-mortals-part-3.aspx\" target=\"_blank\">PowerShell Workflow for Mere Mortals: Part 3<\/a><\/li>\n<li><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/08\/21\/powershell-workflow-for-mere-mortals-part-4.aspx\" target=\"_blank\">PowerShell Workflow for Mere Mortals: Part 4<\/a><\/li>\n<\/ul>\n<p>For more information about workflow, see these Hey, Scripting Guy! Blog posts:&nbsp;<a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/workflow\/\" target=\"_blank\">Windows PowerShell Workflow<\/a>.&nbsp;<\/p>\n<p>Well TB, the good news is that you do not need to write two different workflows to enable parallel processing and sequential processing. Windows PowerShell Workflows are flexible enough to handle both in the same workflow.<\/p>\n<h2>Adding a sequence activity to a workflow<\/h2>\n<p>To add a sequence activity to a Windows PowerShell Workflow, all I need to do is use the <strong>Sequence<\/strong> keyword and specify a script block. When I do this, it causes the commands in the sequence script block to run sequentially and in the specified order.<\/p>\n<p>The key concept here is that a <strong>Sequence<\/strong> activity occurs within a <strong>Parallel<\/strong> activity. The <strong>Sequence<\/strong> activity is required when I want commands to run in a particular order. This is because commands running inside a <strong>Parallel<\/strong> activity run in an undetermined order.<\/p>\n<p>The commands in the <strong>Sequence<\/strong> script block run in parallel with all of the commands in the <strong>Parallel<\/strong> activity. But the commands within the <strong>Sequence<\/strong> script block run in the order in which they appear in the script block. The following workflow illustrates this technique:<\/p>\n<p style=\"padding-left: 30px\">workflow get-winfeatures<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;Parallel {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Get-WindowsFeature -Name PowerShell*<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; InlineScript {$env:COMPUTERNAME}<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Sequence {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Get-date<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $PSVersionTable.PSVersion } }<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p>In the previous workflow, the order for <strong>Get-WindowsFeature<\/strong>, the inline script, and the <strong>Sequence<\/strong> activity is not determined. The only thing I know for sure is that the <strong>Get-Date<\/strong> command runs before I obtain the <strong>PSVersion<\/strong> because this is the order that I specified in the <strong>Sequence<\/strong> activity script block.<\/p>\n<p>To run my workflow, I first run the PS1 script that contains the workflow. Next, I call the workflow and I pass two computer names to it via the <strong>PSComputerName<\/strong> automatic parameter. Here is my command:<\/p>\n<p style=\"padding-left: 30px\">get-winfeatures -PSComputerName server1, server2<\/p>\n<p>The image that follows shows the Windows PowerShell ISE where I call the workflow. It also illustrates the order in which the commands ran this time. Note that the commands in the <strong>Sequence<\/strong> script block ran in the specified order&mdash;that is, <strong>Get-Date<\/strong> executed before <strong>$PsVersionTable.PSVersion<\/strong>. Also notice that they were in the same <strong>Parallel<\/strong> stream of execution.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4885.HSG-8-23-13-01.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4885.HSG-8-23-13-01.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<h2>Some workflow coolness<\/h2>\n<p>One of the cool things about this workflow, is that I ran it from my laptop running Windows&nbsp;8. What is so cool about that? Well, the <strong>Get-WindowsFeature<\/strong> cmdlet does not work on desktop operating systems. Therefore, I ran a command from my laptop&mdash;a command which does not exist on my laptop, but it does exist on the target computers, Server1 and Server2.<\/p>\n<p>Another cool workflow feature is the <strong>InlineScript<\/strong> activity. I am able to access an environmental variable from the remote servers. The <strong>InlineScript<\/strong> activity allows me to do things that otherwise would not be permitted in a Windows PowerShell Workflow. It adds a lot of flexibility.<\/p>\n<p>TB, that is all there is to using Windows PowerShell Workflow and specifying <strong>Sequence<\/strong> information. This concludes Windows PowerShell Workflow week. Join me tomorrow when I will talk about Active Directory with Windows PowerShell.<\/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><strong>Ed Wilson, Microsoft Scripting Guy<\/strong>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, concludes his five-part series about Windows PowerShell Workflow. &nbsp; &nbsp;Hey, Scripting Guy! I have a number of commands that I want to run against several remote servers. The commands include stuff that must happen prior to something else happening. But then, there are also some things that I would [&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,4,45,382],"class_list":["post-2987","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell","tag-workflow"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, concludes his five-part series about Windows PowerShell Workflow. &nbsp; &nbsp;Hey, Scripting Guy! I have a number of commands that I want to run against several remote servers. The commands include stuff that must happen prior to something else happening. But then, there are also some things that I would [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2987","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=2987"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2987\/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=2987"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2987"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2987"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}