• 2 min read

Basic Monitoring with Batch PowerShell

In the 0.8.15 release of the Azure PowerShell SDK, there are 4 new BatchPowerShell cmdlets shipping.

Last October we released the new Azure Batch service and along with it, PowerShell cmdlets to help you perform most of the tasks. In the 0.8.15 release of the Azure PowerShell SDK, there are 4 new BatchPowerShell cmdlets shipping: Get-AzureBatchPool, Get-AzureBatchWorkItem, Get-AzureBatchJob, and Get-AzureBatchTask. These cmdlets can be used to monitor the state of your Batch workloads.

 

Getting Started

Check out this earlier post for information on installing the Azure PowerShell SDK and creating a Batch account. Once you have an account, invoke the Get-AzureBatchAccountKeys cmdlet and store the output BatchAccountContext in a variable.

$context = Get-AzureBatchAccountKeys ""

 

This BatchAccountContext object stores your account name and keys. All cmdlets that interact with the Batch service have a –BatchContext parameter that you’ll need to pass this BatchAccountContext object into. By default, the Primary Key will be used for authentication, but you can explicitly select which key to use by changing your BatchAccountContext’s KeyInUse property.

$context.KeyInUse = "Secondary"

 

Querying for Data

Use the Get-AzureBatchWorkItem cmdlet to see your WorkItems. If you just pass in your BatchAccountContext object without any other parameters, you’ll query for all WorkItems under your account

Get-AzureBatchWorkItem -BatchContext $context

 

You can also supply an OData filter using the –Filter argument to only bring back the WorkItems you’re interested in.

$filter = "startswith(name,'myWork') and state eq 'active'"

Get-AzureBatchWorkItem -Filter $filter -BatchContext $context 

 

You can also query for a specific WorkItem by name using the –Name parameter.

Get-AzureBatchWorkItem -Name "myWorkItem" -BatchContext $context

 

Once you have your WorkItem, you can use the Get-AzureBatchJob cmdlet to query for its Jobs. If you pipe the output of Get-AzureBatchWorkItem into Get-AzureBatchJob, you’ll query for the Jobs under your WorkItem(s), and you can also supply an OData filter.

Get-AzureBatchWorkItem -Name "myWorkItem" -BatchContext $context | Get-AzureBatchJob -Filter "state eq 'active'" -BatchContext $context

 

You can also get a specific Job by directly specifying a WorkItem and Job name.

Get-AzureBatchJob -WorkItemName "myWorkItem" -Name "job-0000000001" -BatchContext $context

 

You can similarly pipe the Get-AzureBatchJob output into Get-AzureBatchTask to query for Tasks under your Job(s).

Get-AzureBatchJob -WorkItemName "myWorkItem" -Name "job-0000000001" -BatchContext $context | Get-AzureBatchTask -BatchContext $context

 

You can also get a specific Task by specifying the WorkItem, Job, and Task names.

Get-AzureBatchTask "myWorkItem" "job-0000000001" "task1" -BatchContext $context

Max Count

By default, each cmdlet will return a maximum of 1000 objects. If you encounter this limit, you can either refine your filter to bring back fewer objects, or you can explicitly set the maximum count using the –MaxCount parameter. To remove the upper bound, set your MaxCount to 0 or less.

Get-AzureBatchWorkItem -MaxCount 2500 -BatchContext $context