{"id":15559,"date":"2018-07-31T10:22:58","date_gmt":"2018-07-31T17:22:58","guid":{"rendered":"https:\/\/www.sapien.com\/blog\/?p=15559"},"modified":"2018-08-06T15:12:34","modified_gmt":"2018-08-06T22:12:34","slug":"new-video-powershell-studio-working-with-azure-cmdlets-in-background-jobs-in-a-gui-app","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2018\/07\/31\/new-video-powershell-studio-working-with-azure-cmdlets-in-background-jobs-in-a-gui-app\/","title":{"rendered":"New Video &#8211; PowerShell Studio: Working with Azure cmdlets in Background Jobs in a GUI app"},"content":{"rendered":"<p>This is the latest installment in our video series on working with Azure cmdlets in background jobs. In this example we are executing the PowerShell script in a Windows GUI Application.<\/p>\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/07\/GUI_Video_03_2018-07-26_16-23-38.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-15567\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/07\/GUI_Video_03_2018-07-26_16-23-38-300x180.jpg\" alt=\"\" width=\"813\" height=\"488\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/07\/GUI_Video_03_2018-07-26_16-23-38-300x180.jpg 300w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/07\/GUI_Video_03_2018-07-26_16-23-38-768x462.jpg 768w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/07\/GUI_Video_03_2018-07-26_16-23-38-1024x616.jpg 1024w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/07\/GUI_Video_03_2018-07-26_16-23-38-848x510.jpg 848w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/07\/GUI_Video_03_2018-07-26_16-23-38.jpg 1920w\" sizes=\"auto, (max-width: 813px) 100vw, 813px\" \/><\/a><\/p>\n<p>View the video here:\u00a0<a href=\"https:\/\/youtu.be\/rSnjtaTqPEs\" target=\"_blank\" rel=\"noopener\">SAPIEN PowerShell Studio: GUI Working with Azure Cmdlets In Background Jobs<\/a><\/p>\n<h3>Minor Sample Script Changes<\/h3>\n<p>As the sample script evolves to execute\u00a0the job from a Windows GUI application instead of the Console, depending on the script complexity more changes might be needed to interact with the GUI components.<\/p>\n<p>The sample script executes the Stop-AzureRMVM cmdlet using the -AsJob parameter which will capture more information about the cmdlet being sent as a job:<\/p>\n<pre>:\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:<\/pre>\n<p>The date\/time format will display the processing information in the RichTextBox &#8220;<em>$DisplayText<\/em>&#8221; object:<\/p>\n<pre>:\r\n$t = Get-Date;\r\n$DisplayText.AppendText(\"`r`nBackground Process Started - $($JobName) - $(($t).ToString(\"MM\/dd\/yyyy - HH:mm:ss\"))\")\r\n:<\/pre>\n<h3>Sample GUI Application<\/h3>\n<p>The sample GUI Application has two controls: Button, and RichTextBox.<\/p>\n<p>The Button executes the code that will send the jobs to the background, and the RichTextBox is the area that will display the result of the task submitted by the button.<\/p>\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/07\/GUI_Video_01_2018-07-26_16-23-38.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-15565\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/07\/GUI_Video_01_2018-07-26_16-23-38-300x132.jpg\" alt=\"\" width=\"834\" height=\"367\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/07\/GUI_Video_01_2018-07-26_16-23-38-300x132.jpg 300w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/07\/GUI_Video_01_2018-07-26_16-23-38-768x338.jpg 768w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/07\/GUI_Video_01_2018-07-26_16-23-38-848x373.jpg 848w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/07\/GUI_Video_01_2018-07-26_16-23-38.jpg 966w\" sizes=\"auto, (max-width: 834px) 100vw, 834px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/07\/GUI_Video_02_2018-07-26_16-23-38.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-15566\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/07\/GUI_Video_02_2018-07-26_16-23-38-300x178.jpg\" alt=\"\" width=\"833\" height=\"494\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/07\/GUI_Video_02_2018-07-26_16-23-38-300x178.jpg 300w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2018\/07\/GUI_Video_02_2018-07-26_16-23-38.jpg 645w\" sizes=\"auto, (max-width: 833px) 100vw, 833px\" \/><\/a><\/p>\n<p>You can download the updated sample Windows GUI here: <a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2018\/07\/AzStopVMJob_GUI_sample_asJob.zip\" target=\"_blank\" rel=\"noopener\">AzStopVMJob_GUI_sample_asJob<\/a>.<\/p>\n<h3>Sample Code<\/h3>\n<p>This is the modified code used in the button control:<\/p>\n<pre>$buttonAzStopVMJob_Click = {\r\n#TODO: Place custom script here\r\n$buttonAzStopVMJob.Enabled = $false;\r\n\r\n#region RunStop-AzureRMVM_WithAsJobParam\r\n## 2 - Run Stop-AzureRMVM cmdlet with the -AsJob parameter:\r\n\r\n## - Create Scriptblock for the job:\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$t = Get-Date;\r\n\"`r`nCmdlet Job Process Started - $TaskName - $(($t).ToString(\"MM\/dd\/yyyy - HH:mm:ss\"))\"\r\ndo {\r\nStart-Sleep -Seconds 20;\r\n$t = Get-Date;\r\n\"Cmdlet Job Processing! $TaskName - $(($t).ToString(\"MM\/dd\/yyyy - HH:mm:ss\"))\"\r\n}\r\nwhile ((Get-Job).State -ne 'Completed');\r\n$t = Get-Date;\r\n\"Cmdlet Job Process Ended - $TaskName - $(($t).ToString(\"MM\/dd\/yyyy - HH:mm:ss\"))\"\r\n\r\n\"`r`nScriptblock - Getting Get-Job Results:\"\r\nGet-Job | Format-List\r\n\r\n\"Scriptblock - Getting Receive-Job Results:\"\r\nReceive-Job * -Keep | Format-List\r\n}\r\n\r\n## - Send\/Start job in background with a jobname:\r\n$DisplayText.AppendText(\"`r`nStart Job to Stop VM!`r`n\");\r\n\r\n## - Step that submit the scriptblock as a background job - ##\r\n$job = Start-Job -Name $JobName -ScriptBlock $JobScript -ArgumentList 'Win2K16VM1';\r\n\r\n## - Loop to check for The Stop-AzureRM cmdlet job completion:\r\n$t = Get-Date;\r\n$DisplayText.AppendText(\"`r`nBackground Process Started - $($JobName) - $(($t).ToString(\"MM\/dd\/yyyy - HH:mm:ss\"))\")\r\ndo {\r\nStart-Sleep -Seconds 20;\r\n$t = Get-Date;\r\n$DisplayText.AppendText(\"`r`nProcessing Job - $($JobName) - $(($t).ToString(\"MM\/dd\/yyyy - HH:mm:ss\"))\")\r\n}\r\nwhile ((Get-Job).State -ne 'Completed');\r\n$t = Get-Date;\r\n$DisplayText.AppendText(\"`r`nBackground Process Completed - $($JobName) - $(($t).ToString(\"MM\/dd\/yyyy - HH:mm:ss\"))\");\r\n\r\n## - Enable button when background job is done:\r\n$buttonAzStopVMJob.Enabled = $true;\r\n$DisplayText.AppendText(\"`r`nProcess Completed - VM Stopped!`r`n\");\r\n\r\n## - Display the background job results:\r\n$DisplayText.AppendText(\"`r`nDisplaying Receive-Job from Stop-AzureRM AsJob Results:`r`n\");\r\n$DisplayText.AppendText((Receive-Job -Job $job -Keep | Out-String -Width 1000));\r\n\r\n## - Display Azure Stop-AzureRM cmdlet job results:\r\n$DisplayText.AppendText(\"`r`nDisplaying Get-Job Main Task Results:`r`n\");\r\n$DisplayText.AppendText((Get-Job | Format-List | Out-String -Width 1000));\r\n\r\n#endregion RunStop-AzureRMVM_WithAsJobParam\r\n\r\n}<\/pre>\n<p>Feel free to copy\/paste this code and modify as necessary.<\/p>\n<h3>Summary<\/h3>\n<p>The sample script underwent some minor changes to interact with the GUI application. Please reference the related links below for more details.<\/p>\n<p>Stay tuned for a future blog article where we will discuss SAPIEN\u2019s JobTracker helper functions in the wizard template \u201cGrid Job\u201d form.<\/p>\n<h3>Related Links<\/h3>\n<ul>\n<li><a href=\"https:\/\/www.sapien.com\/blog\/2018\/07\/19\/new-video-powershell-studio-working-with-azure-cmdlets-in-background-jobs\/\" target=\"_blank\" rel=\"noopener\">PowerShell Studio: Working with Azure cmdlets in background jobs<\/a><\/li>\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-scripting\/powershell-gui-apps-why-think-in-events\">PowerShell GUI Apps: Why think in events?<\/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<\/ul>\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<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the latest installment in our video series on working with Azure cmdlets in background jobs. In this example we are executing the PowerShell script in a Windows GUI Application. View the video here:\u00a0SAPIEN PowerShell Studio: GUI Working with Azure Cmdlets In Background Jobs Minor Sample Script Changes As the sample script evolves to [&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":[946,1143,941,703,25],"tags":[28,961,1016,997],"class_list":["post-15559","post","type-post","status-publish","format-standard","hentry","category-guiprogramming","category-powershell","category-powershell-5-0","category-powershell-studio","category-windows-powershell","tag-powershell","tag-powershell-5-0","tag-powershell-studio","tag-windows-powershell"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/15559","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=15559"}],"version-history":[{"count":14,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/15559\/revisions"}],"predecessor-version":[{"id":15748,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/15559\/revisions\/15748"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=15559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=15559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=15559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}