{"id":2922,"date":"2018-04-04T16:25:14","date_gmt":"2018-04-04T16:25:14","guid":{"rendered":"https:\/\/www.mjtnet.com\/blog\/?p=2922"},"modified":"2018-04-04T16:25:14","modified_gmt":"2018-04-04T16:25:14","slug":"running-powershell-code-from-inside-your-macros","status":"publish","type":"post","link":"https:\/\/www.mjtnet.com\/blog\/2018\/04\/04\/running-powershell-code-from-inside-your-macros\/","title":{"rendered":"Running Powershell Code From Inside Your Macros"},"content":{"rendered":"<p>A while back I wrote <a href=\"https:\/\/www.mjtnet.com\/blog\/2011\/06\/13\/calling-macro-scheduler-functions-from-powershell\/\">this blog post<\/a> which shows you how you can <a href=\"https:\/\/www.mjtnet.com\/blog\/2011\/06\/13\/calling-macro-scheduler-functions-from-powershell\/\">have a PowerShell script run Macro Scheduler commands<\/a> via the <a href=\"https:\/\/www.mjtnet.com\/macroscript_sdk.htm\">MacroScript SDK<\/a>.<\/p>\n<p>However, what if you want to do it the other way around and run PowerShell code from inside a Macro Scheduler macro instead?<\/p>\n<p>One way would simply be to run a PowerShell script on the command line, like this:<\/p>\n<pre>Run&gt;powershell.exe -file \"c:\\myfolder\\myscript.ps1\"<\/pre>\n<p>But what if you didn&#8217;t have a PowerShell script file?\u00a0 Well, while you could always have your macro create one using the WriteLn command (and delete it after), you don&#8217;t need to as you can pass a sequence of commands to PowerShell on the command line:<\/p>\n<pre>powershell.exe\u00a0-executionPolicy bypass -command \"Write-Host 'Hello World'; exit 123;\"<\/pre>\n<p>As you can see all we&#8217;re really doing is passing the content of the script instead of a file.<\/p>\n<p>We can also return the exit code of the PowerShell script to a variable in your macro.\u00a0 Here&#8217;s an example:<\/p>\n<pre class=\"brush:macroscript\">\r\n\/\/This line ensures the powershell script finishes before the macro continues\r\nLet>RP_WAIT=1\r\n\/\/Uncomment this line to HIDE the powershell window when ready to go live\r\n\/\/Let>RP_WINDOWMODE=0\r\n\r\nLet>localVar=Hello World\r\nLabelToVar>MyPowershellScript,theScript\r\nRun>powershell.exe -executionPolicy bypass -command \"%theScript%\"\r\n\r\n\/\/display the exit code\r\nMDL>RP_RESULT\r\n\r\n\/*\r\nMyPowershellScript:\r\n\r\n$var = '%localVar%';\r\nWrite-Host $var;\r\nStart-Sleep -s 10;\r\nexit 555;\r\n*\/\r\n<\/pre>\n<p><\/p>\n<p>What we&#8217;ve done here is store our PowerShell code in a label. We get that code into a variable and then pass it to PowerShell on the command line. <\/p>\n<p>Notice how we can embed Macro Scheduler variables in the code too.  We use PowerShell&#8217;s exit statement to set a return value which Macro Scheduler gets in the RP_RESULT variable.<\/p>\n<p>Setting RP_WAIT to 1 makes sure the macro waits for PowerShell to finish before proceeding with the rest of the script.  You may also want to hide the PowerShell window by setting RP_WINDOWMODE to 0, but for testing purposes I&#8217;ve left that commented out so that you can actually see it working. That&#8217;s also the reason for the 10 second delay at the end of the PowerShell script &#8211; apart from showing how we can add multiple lines of code it also gives us a chance to see things happen!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A while back I wrote this blog post which shows you how you can have a PowerShell script run Macro Scheduler commands via the MacroScript SDK. However, what if you want to do it the other way around and run PowerShell code from inside a Macro Scheduler macro instead? One way would simply be to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,6],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/2922"}],"collection":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/comments?post=2922"}],"version-history":[{"count":4,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/2922\/revisions"}],"predecessor-version":[{"id":2926,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/2922\/revisions\/2926"}],"wp:attachment":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/media?parent=2922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/categories?post=2922"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/tags?post=2922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}