Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive
 

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:

  1. Connect to Azure and execute the command.
  2. Use the Start-Job cmdlet to submit the scriptblock as a background job.
  3. Use the Do/While loop to display the job progress.
  4. Use the Receive-Job cmdlet to save the job results.
  5. Use the Get-Job cmdlet to display the saved job results at the end of the process.

We also show the use of adding an argument by passing a parameter to the scriptblock.

Take Away

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.

Sample Code

Feel free to copy/paste the code against your AzureRM VM’s.

Check the AzureRM VM Status:

## - Check for VM status:
Get-AzureRmVM -ResourceGroupName 'GlobalAzureBootCampResources' -status `
| Select-Object Name, PowerState;

Run Stop-AzureRMVM without using the -AsJob parameter:

#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

Run Stop-AzureRMVM using the -AsJob parameter:

#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

Summary

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.

Related Articles

Instructional Videos

Learn about other SAPIEN Technologies product features by checking out the videos on our YouTube channel.

If you have questions about our products, please post in our support forum.
For licensed customers, use the forum associated with your product in our Product Support Forums for Registered Customers.
For users of trial versions, please post in our Former and Future Customers - Questions forum.
Copyright © 2024 SAPIEN Technologies, Inc.