Edit

Share via


New-AzBatchTask

Creates a Batch task under a job.

Syntax

JobId_Single (Default)

New-AzBatchTask
    -JobId <String>
    -Id <String>
    -CommandLine <String>
    -BatchContext <BatchAccountContext>
    [-DisplayName <String>]
    [-ResourceFiles <PSResourceFile[]>]
    [-EnvironmentSettings <IDictionary>]
    [-AuthenticationTokenSettings <PSAuthenticationTokenSettings>]
    [-UserIdentity <PSUserIdentity>]
    [-AffinityInformation <PSAffinityInformation>]
    [-Constraints <PSTaskConstraints>]
    [-MultiInstanceSettings <PSMultiInstanceSettings>]
    [-DependsOn <TaskDependencies>]
    [-ApplicationPackageReferences <PSApplicationPackageReference[]>]
    [-OutputFile <PSOutputFile[]>]
    [-ExitConditions <PSExitConditions>]
    [-ContainerSettings <PSTaskContainerSettings>]
    [-DefaultProfile <IAzureContextContainer>]
    [<CommonParameters>]

JobId_Bulk

New-AzBatchTask
    -JobId <String>
    -BatchContext <BatchAccountContext>
    [-Tasks <PSCloudTask[]>]
    [-DefaultProfile <IAzureContextContainer>]
    [<CommonParameters>]

JobObject_Bulk

New-AzBatchTask
    -BatchContext <BatchAccountContext>
    [-Job <PSCloudJob>]
    [-Tasks <PSCloudTask[]>]
    [-DefaultProfile <IAzureContextContainer>]
    [<CommonParameters>]

JobObject_Single

New-AzBatchTask
    -Id <String>
    -CommandLine <String>
    -BatchContext <BatchAccountContext>
    [-Job <PSCloudJob>]
    [-DisplayName <String>]
    [-ResourceFiles <PSResourceFile[]>]
    [-EnvironmentSettings <IDictionary>]
    [-AuthenticationTokenSettings <PSAuthenticationTokenSettings>]
    [-UserIdentity <PSUserIdentity>]
    [-AffinityInformation <PSAffinityInformation>]
    [-Constraints <PSTaskConstraints>]
    [-MultiInstanceSettings <PSMultiInstanceSettings>]
    [-DependsOn <TaskDependencies>]
    [-ApplicationPackageReferences <PSApplicationPackageReference[]>]
    [-OutputFile <PSOutputFile[]>]
    [-ExitConditions <PSExitConditions>]
    [-ContainerSettings <PSTaskContainerSettings>]
    [-DefaultProfile <IAzureContextContainer>]
    [<CommonParameters>]

Description

The New-AzBatchTask cmdlet creates an Azure Batch task under the job specified by the JobId parameter or the Job parameter.

Examples

Example 1: Create a Batch task

New-AzBatchTask -JobId "Job-000001" -Id "Task23" -CommandLine "cmd /c dir /s" -BatchContext $Context

This command creates a task that has the ID Task23 under the job that has the ID Job-000001. The task runs the specified command. Use the Get-AzBatchAccountKey cmdlet to assign a context to the $Context variable.

Example 2: Create a Batch task

$autoUser = New-Object Microsoft.Azure.Commands.Batch.Models.PSAutoUserSpecification -ArgumentList @("Task", "Admin")
$userIdentity = New-Object Microsoft.Azure.Commands.Batch.Models.PSUserIdentity $autoUser
Get-AzBatchJob -Id "Job-000001" -BatchContext $Context | New-AzBatchTask -Id "Task26" -CommandLine "cmd /c echo hello > newFile.txt" -UserIdentity $userIdentity -BatchContext $Context

This command gets the Batch job that has the ID Job-000001 by using the Get-AzBatchJob cmdlet. The command passes that job to the current cmdlet by using the pipeline operator. The command creates a task that has the ID Task26 under that job. The task runs the specified command by using elevated permissions.

Example 3: Add a collection of tasks to the specified job by using the pipeline

$Context = Get-AzBatchAccountKey -AccountName "ContosoBatchAccount"
$Task01 = New-Object Microsoft.Azure.Commands.Batch.Models.PSCloudTask("Task23", "cmd /c dir /s")
$Task02 = New-Object Microsoft.Azure.Commands.Batch.Models.PSCloudTask("Task24", "cmd /c dir /s")
Get-AzBatchJob -Id "Job-000001" -BatchContext $Context | New-AzBatchTask -Tasks @($Task01, $Task02) -BatchContext $Context

The first command creates an object reference to the account keys for the batch account named ContosoBatchAccount by using Get-AzBatchAccountKey. The command stores this object reference in the $Context variable. The next two commands create PSCloudTask objects by using the New-Object cmdlet. The commands store the tasks in the $Task01 and $Task02 variables. The final command gets the Batch job that has the ID Job-000001 by using Get-AzBatchJob. Then the command passes that job to the current cmdlet by using the pipeline operator. The command adds a collection of tasks under that job. The command uses the context stored in $Context.

Example 4: Add a collection of tasks to the specified job

$Context = Get-AzBatchAccountKey -AccountName "ContosoBatchAccount"
$Task01 = New-Object Microsoft.Azure.Commands.Batch.Models.PSCloudTask("Task23", "cmd /c dir /s")
$Task02 = New-Object Microsoft.Azure.Commands.Batch.Models.PSCloudTask("Task24", "cmd /c dir /s")
New-AzBatchTask -JobId "Job-000001" -Tasks @($Task01, $Task02) -BatchContext $Context

The first command creates an object reference to the account keys for the batch account named ContosoBatchAccount by using Get-AzBatchAccountKey. The command stores this object reference in the $Context variable. The next two commands create PSCloudTask objects by using the New-Object cmdlet. The commands store the tasks in the $Task01 and $Task02 variables. The final command adds the tasks stored in $Task01 and $Task02 under the job that has the ID Job-000001.

Example 5: Add a task with output files

New-AzBatchTask -JobId "Job-000001" -Id "Task23" -CommandLine "cmd /c dir /s" -BatchContext $Context
$blobContainerDestination = New-Object Microsoft.Azure.Commands.Batch.Models.PSOutputFileBlobContainerDestination "https://myaccount.blob.core.windows.net/sascontainer?sv=2015-04-05&st=2015-04-29T22%3A18%3A26Z&se=2015-04-30T02%3A23%3A26Z&sr=b&sp=rw&spr=https&sig=Z%2FRHIX5Xcg0Mq2rqI3OlWTjEg2tYkboXr1P9ZUXDtkk%3D"
$destination = New-Object Microsoft.Azure.Commands.Batch.Models.PSOutputFileDestination $blobContainerDestination
$uploadOptions = New-Object Microsoft.Azure.Commands.Batch.Models.PSOutputFileUploadOptions "TaskSuccess"
$outputFile = New-Object Microsoft.Azure.Commands.Batch.Models.PSOutputFile "*.txt", $blobContainerDestination, $uploadOptions

New-AzBatchTask -JobId "Job-000001" -Id "Task23" -CommandLine "cmd /c dir /s" -OutputFile $outputFile -BatchContext $Context

Example 6: Add a task with authentication token settings

$authSettings = New-Object Microsoft.Azure.Commands.Batch.Models.PSAuthenticationTokenSettings
$authSettings.Access = "Job"
New-AzBatchTask -JobId "Job-000001" -Id "Task23" -CommandLine "cmd /c dir /s" -AuthenticationTokenSettings $authSettings -BatchContext $Context

Example 7: Add a task which runs in a container

$Context = Get-AzBatchAccountKey -AccountName "ContosoBatchAccount"
New-AzBatchTask -JobId "Job-000001" -Id "Task23" -CommandLine "cmd /c dir /s" -ContainerSettings (New-Object Microsoft.Azure.Commands.Batch.Models.PSTaskContainerSettings "containerImageName") -BatchContext $Context

Parameters

-AffinityInformation

Specifies a locality hint that the Batch service uses to select a node on which to run the task.

Parameter properties

Type:PSAffinityInformation
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

JobId_Single
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False
JobObject_Single
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

-ApplicationPackageReferences

The New-AzBatchTask cmdlet creates an Azure Batch task under the job specified by the JobId parameter or the Job parameter.

Parameter properties

Type:

PSApplicationPackageReference[]

Default value:None
Supports wildcards:False
DontShow:False
Aliases:ApplicationPackageReference

Parameter sets

JobId_Single
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False
JobObject_Single
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

-AuthenticationTokenSettings

The settings for an authentication token that the task can use to perform Batch service operations. If this is set, the Batch service provides the task with an authentication token which can be used to authenticate Batch service operations without requiring an account access key. The token is provided via the AZ_BATCH_AUTHENTICATION_TOKEN environment variable. The operations that the task can carry out using the token depend on the settings. For example, a task can request job permissions in order to add other tasks to the job, or check the status of the job or of other tasks.

Parameter properties

Type:PSAuthenticationTokenSettings
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

JobId_Single
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False
JobObject_Single
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

-BatchContext

Specifies the BatchAccountContext instance that this cmdlet uses to interact with the Batch service. If you use the Get-AzBatchAccount cmdlet to get your BatchAccountContext, then Microsoft Entra authentication will be used when interacting with the Batch service. To use shared key authentication instead, use the Get-AzBatchAccountKey cmdlet to get a BatchAccountContext object with its access keys populated. When using shared key authentication, the primary access key is used by default. To change the key to use, set the BatchAccountContext.KeyInUse property.

Parameter properties

Type:BatchAccountContext
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

(All)
Position:Named
Mandatory:True
Value from pipeline:True
Value from pipeline by property name:False
Value from remaining arguments:False

-CommandLine

Specifies the command line for the task.

Parameter properties

Type:String
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

JobId_Single
Position:Named
Mandatory:True
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False
JobObject_Single
Position:Named
Mandatory:True
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

-Constraints

Specifies the execution constraints that apply to this task.

Parameter properties

Type:PSTaskConstraints
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

JobId_Single
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False
JobObject_Single
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

-ContainerSettings

The settings for the container under which the task runs. If the pool that will run this task has containerConfiguration set, this must be set as well. If the pool that will run this task doesn't have containerConfiguration set, this must not be set. When this is specified, all directories recursively below the AZ_BATCH_NODE_ROOT_DIR (the root of Azure Batch directories on the node) are mapped into the container, all task environment variables are mapped into the container, and the task command line is executed in the container.

Parameter properties

Type:PSTaskContainerSettings
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

JobId_Single
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False
JobObject_Single
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

-DefaultProfile

The credentials, account, tenant, and subscription used for communication with azure.

Parameter properties

Type:IAzureContextContainer
Default value:None
Supports wildcards:False
DontShow:False
Aliases:AzContext, AzureRmContext, AzureCredential

Parameter sets

(All)
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

-DependsOn

Specifies that the task depends on other tasks. The task will not be scheduled until all depended-on tasks have completed successfully.

Parameter properties

Type:TaskDependencies
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

JobId_Single
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False
JobObject_Single
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

-DisplayName

Specifies the display name of the task.

Parameter properties

Type:String
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

JobId_Single
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False
JobObject_Single
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

-EnvironmentSettings

Specifies the environment settings, as key/value pairs, that this cmdlet adds to the task. The key is the environment setting name. The value is the environment setting.

Parameter properties

Type:IDictionary
Default value:None
Supports wildcards:False
DontShow:False
Aliases:EnvironmentSetting

Parameter sets

JobId_Single
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False
JobObject_Single
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

-ExitConditions

The New-AzBatchTask cmdlet creates an Azure Batch task under the job specified by the JobId parameter or the Job parameter.

Parameter properties

Type:PSExitConditions
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

JobId_Single
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False
JobObject_Single
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

-Id

Specifies the ID of the task.

Parameter properties

Type:String
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

JobId_Single
Position:Named
Mandatory:True
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False
JobObject_Single
Position:Named
Mandatory:True
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

-Job

Specifies the job under which this cmdlet creates the task. To obtain a PSCloudJob object, use the Get-AzBatchJob cmdlet.

Parameter properties

Type:PSCloudJob
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

JobObject_Bulk
Position:Named
Mandatory:False
Value from pipeline:True
Value from pipeline by property name:False
Value from remaining arguments:False
JobObject_Single
Position:Named
Mandatory:False
Value from pipeline:True
Value from pipeline by property name:False
Value from remaining arguments:False

-JobId

Specifies the ID of the job under which this cmdlet creates the task.

Parameter properties

Type:String
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

JobId_Single
Position:Named
Mandatory:True
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False
JobId_Bulk
Position:Named
Mandatory:True
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

-MultiInstanceSettings

Specifies information about how to run a multi-instance task.

Parameter properties

Type:PSMultiInstanceSettings
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

JobId_Single
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False
JobObject_Single
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

-OutputFile

Gets or sets a list of files that the Batch service will upload from the compute node after running the command line. For multi-instance tasks, the files will only be uploaded from the compute node on which the primary task is executed.

Parameter properties

Type:

PSOutputFile[]

Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

JobId_Single
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False
JobObject_Single
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

-ResourceFiles

Specifies resource files, as key/value pairs, that the task requires. The key is the resource file path. The value is the resource file blob source.

Parameter properties

Type:

PSResourceFile[]

Default value:None
Supports wildcards:False
DontShow:False
Aliases:ResourceFile

Parameter sets

JobId_Single
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False
JobObject_Single
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

-Tasks

Specifies the collection of tasks to be added. Each task must have a unique ID.

Parameter properties

Type:

PSCloudTask[]

Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

JobId_Bulk
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False
JobObject_Bulk
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

-UserIdentity

The user identity under which the task runs.

Parameter properties

Type:PSUserIdentity
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

JobId_Single
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False
JobObject_Single
Position:Named
Mandatory:False
Value from pipeline:False
Value from pipeline by property name:False
Value from remaining arguments:False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

Inputs

PSCloudJob

BatchAccountContext

Outputs

Void