This video shows two different ways to execute a console AzureRM script as a job using PowerShell Studio. This method also works in PrimalScript.
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 the Azure cmdlet to be submitted as a job using the Start-Job cmdlet.
2. Create a more elaborate scriptblock containing the Azure cmdlet to be submitted as a job using the -AsJob parameter.
Both jobs can save the status and any output results from the Get-Job and Receive-Job cmdlets.
What’s involved in this process:
We also show the use of adding an argument by passing a parameter to the scriptblock.
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.
Most importantly, store the code in a scriptblock and submit the job using the Start-Job cmdlet. The Receive-Job cmdlet will save information generated by the job, and the Get-Job cmdlet will provide the status information of the background job—so if the job doesn’t generate any output, then it will not save any data.
Feel free to copy/paste the code against your AzureRM VM’s.
## - Check for VM status: Get-AzureRmVM -ResourceGroupName 'GlobalAzureBootCampResources' -status ` | Select-Object Name, PowerState;
#region RunStop-AzureRMVM_NoAsJobParam ## 1 - Run Stop-AzureRMVM cmdlet without the -AsJob parameter: $JobName = "StopVmScriptJob"; $JobScript = { ## - Sign-On to Azure: Import-AzureRmContext -Path 'C:\Temp\WinPS_AsubRMprofile.json' | Out-Null; ## - Run Azure cmdlet with AsJob parameter: Stop-AzureRMVM -ResourceGroupName "GlobalAzureBootCampResources" ` -Name 'Win2K16VM1' ` -Force -Verbose; }; ## - Step that submit the scriptblock as a background job - ## $job = Start-Job -Name $Name -ScriptBlock $JobScript; ## - Loop to check for The Stop-AzureRM cmdlet job completion: "Processing Started - $($JobName) - $((Get-Date).ToString("MM/dd/yyyy-MM:hh:ss"))" do { Start-Sleep -Seconds 10; "Processing! - $($JobName) - $((Get-Date).ToString("MM/dd/yyyy-MM:hh:ss"))" } while ((Get-Job).State -ne 'Completed'); "Process Completed - $($JobName) - $((Get-Date).ToString("MM/dd/yyyy-MM:hh:ss"))" ## - Saving Job results: $ReceiveJobStatus1 = Receive-Job -Job $job -Keep; $GetJobStatus1 = Get-Job; ## - Displaying Job results: $ReceiveJobStatus1 $GetJobStatus1 | Format-List; #endregion RunStop-AzureRMVM_NoAsJobParam
#region RunStop-AzureRMVM_WithAsJobParam ## 2 - Run Stop-AzureRMVM cmdlet with the -AsJob parameter: $JobName = "StopVmCmdletAsJob"; $JobScript = { param($Name) ## - Sign-On to Azure: Import-AzureRmContext -Path 'C:\Temp\WinPS_AsubRMprofile.json' | Out-Null; $TaskName = "StopVM1_$($Name)"; ## - Run Azure cmdlet with AsJob parameter: Stop-AzureRMVM -ResourceGroupName "GlobalAzureBootCampResources" ` -Name $Name ` -AsJob ` -Force -Verbose; ## - Inside Job Loop to check for the cmdlet job completion: "`r`nProcess Started - $TaskName - $((Get-Date).ToString("MM/dd/yyyy-MM:hh:ss"))" do { Start-Sleep -Seconds 10; "Processing! $TaskName - $((Get-Date).ToString("MM/dd/yyyy-MM:hh:ss"))" } while ((Get-Job).State -ne 'Completed'); "Process Ended - $TaskName - $((Get-Date).ToString("MM/dd/yyyy-MM:hh:ss"))" "`r`nGetting Job General Results:" $AzGetJobStatus1 = Get-Job $AzGetJobStatus1 | Format-List "Getting Receive Results:" $AzureReceiveJobStatus2 = Receive-Job * -Keep; $AzureReceiveJobStatus2 | Format-List } ## - Step that submit the scriptblock as a background job - ## $job = Start-Job -Name $Name -ScriptBlock $JobScript -ArgumentList 'Win2K16VM1'; ## - Loop to check for The Stop-AzureRM cmdlet job completion: "`r`nBackground Process Started - $($JobName) - $((Get-Date).ToString("MM/dd/yyyy-MM:hh:ss"))" do { Start-Sleep -Seconds 10; "Processing Job - $($JobName) - $((Get-Date).ToString("MM/dd/yyyy-MM:hh:ss"))" } while ((Get-Job).State -ne 'Completed'); "Background Process Completed - $($JobName) - $((Get-Date).ToString("MM/dd/yyyy-MM:hh:ss"))" ## - Saving Job results: $ReceiveJobStatus2 = Receive-Job -Job $job -Keep; $GetJobStatus2 = Get-Job ## - Displaying Job results: $ReceiveJobStatus2 $GetJobStatus2 | Format-List #endregion RunStop-AzureRMVM_WithAsJobParam
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. Refer to the related articles below for more details.
Learn about other SAPIEN Technologies product features by checking out the videos on our YouTube channel.