{"id":15431,"date":"2018-07-19T06:00:40","date_gmt":"2018-07-19T13:00:40","guid":{"rendered":"https:\/\/www.sapien.com\/blog\/?p=15431"},"modified":"2018-07-31T10:30:13","modified_gmt":"2018-07-31T17:30:13","slug":"new-video-powershell-studio-working-with-azure-cmdlets-in-background-jobs","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2018\/07\/19\/new-video-powershell-studio-working-with-azure-cmdlets-in-background-jobs\/","title":{"rendered":"New Video &#8211; PowerShell Studio: Working with Azure cmdlets in Background Jobs"},"content":{"rendered":"<p>Our latest video shows two different ways\u00a0to execute a console AzureRM script as a job using PowerShell Studio 2018.<\/p>\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/07\/PS2018_AzureJob_01_2018-07-16_17-48-17.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-15436\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/07\/PS2018_AzureJob_01_2018-07-16_17-48-17-300x180.jpg\" alt=\"\" width=\"693\" height=\"416\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/07\/PS2018_AzureJob_01_2018-07-16_17-48-17-300x180.jpg 300w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/07\/PS2018_AzureJob_01_2018-07-16_17-48-17-768x462.jpg 768w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/07\/PS2018_AzureJob_01_2018-07-16_17-48-17-1024x615.jpg 1024w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/07\/PS2018_AzureJob_01_2018-07-16_17-48-17-848x510.jpg 848w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/07\/PS2018_AzureJob_01_2018-07-16_17-48-17.jpg 1208w\" sizes=\"auto, (max-width: 693px) 100vw, 693px\" \/><\/a><\/p>\n<p>In this short video we demonstrate how to use the Azure cmdlet <em>Stop-AzureRmVM<\/em> to stop an Azure VM by submitting the script code as a background job in two different ways:<\/p>\n<p>1. Create the scriptblock containing the Azure cmdlet to be submitted as a job using the <em>Start-Job<\/em> cmdlet.<br \/>\n2. Create a more elaborate scriptblock containing the Azure cmdlet to be submitted as a job using the <em>-AsJob<\/em> parameter.<\/p>\n<p>Both jobs can save the status and any output results from the <em>Get-Job<\/em> and <em>Receive-Job<\/em> cmdlets.<\/p>\n<p>What&#8217;s involved in this process:<\/p>\n<ol>\n<li>Connect to Azure and execute the command.<\/li>\n<li>Use the\u00a0<em>Start-Job<\/em>\u00a0cmdlet to submit the scriptblock as a background job.<\/li>\n<li>Use the\u00a0<em>Do\/While<\/em> loop to display the job progress.<\/li>\n<li>Use the <em>Receive-Job<\/em> cmdlet to save the job results.<\/li>\n<li>Use the <em>Get-Job<\/em> cmdlet to display the saved job results at the end of the process.<\/li>\n<\/ol>\n<p>We also show the use of adding an argument by passing a parameter to the scriptblock.<\/p>\n<p>View the video here:\u00a0<a href=\"https:\/\/youtu.be\/_gTQLuCYdKw\" target=\"_blank\" rel=\"noopener\">SAPIEN PowerShell Studio: Working with Azure Cmdlets In Background Jobs<\/a><\/p>\n<h3>Take Way<\/h3>\n<p>This example provides a scripting framework that is not limited to Azure, and that can also easily be enhanced. It is better to use background jobs when working with Azure commands in a GUI application.<\/p>\n<p>Most importantly, store the code in a scriptblock and submit the job using the\u00a0<em>Start-Job<\/em> cmdlet. The <em>Receive-Job<\/em> cmdlet will save information generated by the job, and the\u00a0<em>Get-Job<\/em>\u00a0cmdlet will provide the status information of the background job\u2014so if the job doesn&#8217;t generate any output, then it will not save any data.<\/p>\n<h3>Sample Code<\/h3>\n<p>Feel free to copy\/paste the code against your AzureRM VM&#8217;s.<\/p>\n<h4>Check the AzureRM VM Status:<\/h4>\n<pre>## - Check for VM status:\r\nGet-AzureRmVM -ResourceGroupName 'GlobalAzureBootCampResources' -status `\r\n| Select-Object Name, PowerState;<\/pre>\n<h4>Run Stop-AzureRMVM without using the -AsJob parameter:<\/h4>\n<pre>#region RunStop-AzureRMVM_NoAsJobParam\r\n\r\n## 1 - Run Stop-AzureRMVM cmdlet without the -AsJob parameter:\r\n$JobName = \"StopVmScriptJob\";\r\n$JobScript = {\r\n\r\n## - Sign-On to Azure:\r\nImport-AzureRmContext -Path 'C:\\Temp\\WinPS_AsubRMprofile.json' | Out-Null;\r\n\r\n## - Run Azure cmdlet with AsJob parameter:\r\nStop-AzureRMVM -ResourceGroupName \"GlobalAzureBootCampResources\" `\r\n-Name 'Win2K16VM1' `\r\n-Force -Verbose;\r\n};\r\n\r\n## - Step that submit the scriptblock as a background job - ##\r\n$job = Start-Job -Name $Name -ScriptBlock $JobScript;\r\n\r\n## - Loop to check for The Stop-AzureRM cmdlet job completion:\r\n\"Processing Started - $($JobName) - $((Get-Date).ToString(\"MM\/dd\/yyyy-MM:hh:ss\"))\"\r\ndo {\r\nStart-Sleep -Seconds 10;\r\n\"Processing! - $($JobName) - $((Get-Date).ToString(\"MM\/dd\/yyyy-MM:hh:ss\"))\"\r\n}\r\nwhile ((Get-Job).State -ne 'Completed');\r\n\"Process Completed - $($JobName) - $((Get-Date).ToString(\"MM\/dd\/yyyy-MM:hh:ss\"))\"\r\n\r\n## - Saving Job results:\r\n$ReceiveJobStatus1 = Receive-Job -Job $job -Keep;\r\n$GetJobStatus1 = Get-Job;\r\n\r\n## - Displaying Job results:\r\n$ReceiveJobStatus1\r\n$GetJobStatus1 | Format-List;\r\n\r\n#endregion RunStop-AzureRMVM_NoAsJobParam<\/pre>\n<h4>Run Stop-AzureRMVM using the -AsJob parameter:<\/h4>\n<pre>#region RunStop-AzureRMVM_WithAsJobParam\r\n\r\n## 2 - Run Stop-AzureRMVM cmdlet with the -AsJob parameter:\r\n$JobName = \"StopVmCmdletAsJob\";\r\n$JobScript = {\r\nparam($Name)\r\n\r\n## - Sign-On to Azure:\r\nImport-AzureRmContext -Path 'C:\\Temp\\WinPS_AsubRMprofile.json' | Out-Null;\r\n$TaskName = \"StopVM1_$($Name)\";\r\n\r\n## - Run Azure cmdlet with AsJob parameter:\r\nStop-AzureRMVM -ResourceGroupName \"GlobalAzureBootCampResources\" `\r\n-Name $Name `\r\n-AsJob `\r\n-Force -Verbose;\r\n\r\n## - Inside Job Loop to check for the cmdlet job completion:\r\n\"`r`nProcess Started - $TaskName - $((Get-Date).ToString(\"MM\/dd\/yyyy-MM:hh:ss\"))\"\r\ndo {\r\nStart-Sleep -Seconds 10;\r\n\"Processing! $TaskName - $((Get-Date).ToString(\"MM\/dd\/yyyy-MM:hh:ss\"))\"\r\n}\r\nwhile ((Get-Job).State -ne 'Completed');\r\n\"Process Ended - $TaskName - $((Get-Date).ToString(\"MM\/dd\/yyyy-MM:hh:ss\"))\"\r\n\r\n\"`r`nGetting Job General Results:\"\r\n$AzGetJobStatus1 = Get-Job\r\n$AzGetJobStatus1 | Format-List\r\n\r\n\"Getting Receive Results:\"\r\n$AzureReceiveJobStatus2 = Receive-Job * -Keep;\r\n$AzureReceiveJobStatus2 | Format-List\r\n}\r\n\r\n## - Step that submit the scriptblock as a background job - ##\r\n$job = Start-Job -Name $Name -ScriptBlock $JobScript -ArgumentList 'Win2K16VM1';\r\n\r\n## - Loop to check for The Stop-AzureRM cmdlet job completion:\r\n\"`r`nBackground Process Started - $($JobName) - $((Get-Date).ToString(\"MM\/dd\/yyyy-MM:hh:ss\"))\"\r\ndo {\r\nStart-Sleep -Seconds 10;\r\n\"Processing Job - $($JobName) - $((Get-Date).ToString(\"MM\/dd\/yyyy-MM:hh:ss\"))\"\r\n}\r\nwhile ((Get-Job).State -ne 'Completed');\r\n\"Background Process Completed - $($JobName) - $((Get-Date).ToString(\"MM\/dd\/yyyy-MM:hh:ss\"))\"\r\n\r\n## - Saving Job results:\r\n$ReceiveJobStatus2 = Receive-Job -Job $job -Keep;\r\n$GetJobStatus2 = Get-Job\r\n\r\n## - Displaying Job results:\r\n$ReceiveJobStatus2\r\n$GetJobStatus2 | Format-List\r\n\r\n#endregion RunStop-AzureRMVM_WithAsJobParam<\/pre>\n<h3>Summary<\/h3>\n<p>Create and test the script using the PowerShell Console before implementing in a GUI application such as PowerShell Studio. This sample script will require some minor changes as you build the GUI application.\u00a0 Refer to the related articles below for more details.<\/p>\n<h3>Related Articles<\/h3>\n<ul>\n<li><a href=\"https:\/\/www.sapien.com\/blog\/2018\/07\/12\/powershell-studio-how-to-prevent-powershell-cmdlets-from-hanging-in-gui-apps\/\">PowerShell Studio: How to prevent Powershell cmdlets from hanging in GUI apps<\/a><\/li>\n<li><a href=\"https:\/\/info.sapien.com\/index.php\/guis\/gui-advanced-tips\/powershell-studio-creating-responsive-forms\">PowerShell Studio: Creating Responsive Forms<\/a><\/li>\n<li><a href=\"https:\/\/www.sapien.com\/blog\/2018\/07\/31\/new-video-powershell-studio-working-with-azure-cmdlets-in-background-jobs-in-a-gui-app\/\">PowerShell Studio: Working with Azure cmdlets in Background Jobs in a GUI app<\/a><\/li>\n<\/ul>\n<h3>Instructional Videos<\/h3>\n<p>Learn about other SAPIEN Technologies product features by checking out the videos on our\u00a0<a href=\"http:\/\/youtube.com\/SAPIENTech\" target=\"_blank\" rel=\"noopener\">YouTube channel<\/a>.<\/p>\n<h3>Feedback<\/h3>\n<p>As always, if you have any ideas, comments, or feedback, please\u00a0<a href=\"https:\/\/www.sapien.com\/forums\/viewforum.php?f=23\" target=\"_blank\" rel=\"noopener\">visit our feedback forum<\/a>\u00a0and reference this post.<\/p>\n<p>&nbsp;<\/p>\n<p><em>Max Trinidad is a Technology Evangelist at SAPIEN Technologies Inc., and a Microsoft PowerShell MVP. You can reach him at\u00a0<a href=\"mailto:maxt@sapien.com\">maxt@sapien.com<\/a><\/em><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Our latest video shows two different ways\u00a0to execute a console AzureRM script as a job using PowerShell Studio 2018. In this short video we demonstrate how to use the Azure cmdlet Stop-AzureRmVM to stop an Azure VM by submitting the script code as a background job in two different ways: 1. Create the scriptblock containing [&hellip;]<\/p>\n","protected":false},"author":47,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1143,1238,703,410,1239,25],"tags":[28,1215,1016,37,997],"class_list":["post-15431","post","type-post","status-publish","format-standard","hentry","category-powershell","category-powershell-core","category-powershell-studio","category-primalscript-software-news","category-pscore6","category-windows-powershell","tag-powershell","tag-powershell-core","tag-powershell-studio","tag-primalscript","tag-windows-powershell"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/15431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/47"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=15431"}],"version-history":[{"count":17,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/15431\/revisions"}],"predecessor-version":[{"id":15666,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/15431\/revisions\/15666"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=15431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=15431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=15431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}