Dela via


Hantera Batch-resurser med PowerShell-cmdlets

Med Azure Batch PowerShell-cmdlet kan du utföra och script många vanliga Batch-uppgifter. Detta är en kort introduktion till de cmdlets du kan använda för att hantera dina Batch-konton och arbeta med dina Batch-resurser som pooler, jobb och uppgifter.

För en komplett lista över Batch cmdlets och detaljerad cmdlet-syntax, se Azure Batch cmdlet-referensen.

We recommend that you update your Azure PowerShell modules frequently to take advantage of service updates and enhancements.

Förutsättningar

  • Install and configure the Azure PowerShell module. För att installera en specifik Azure Batch-modul, såsom en förhandsversion, se PowerShell Gallery.

  • Kör cmdlet Connect-AzAccount för att ansluta till din prenumeration (Azure Batch-cmdlets ingår i Azure Resource Manager-modulen).

    Connect-AzAccount
    
  • Register with the Batch provider namespace. Du behöver bara utföra denna åtgärd en gång per prenumeration.

    Register-AzResourceProvider -ProviderNamespace Microsoft.Batch
    

Manage Batch accounts and keys

Skapa ett Batch-konto

New-AzBatchAccount creates a Batch account in a specified resource group. Om du inte redan har en resursgrupp, skapa en genom att köra cmdlet:en New-AzResourceGroup. Specify one of the Azure regions in the Location parameter, such as "Central US". Till exempel:

New-AzResourceGroup –Name MyBatchResourceGroup –Location "Central US"

Sedan, skapa ett Batch-konto i resursgruppen. Ange ett namn för kontot i <account_name>, samt plats och namn för din resursgrupp. Att skapa Batch-kontot kan ta lite tid att slutföra. Till exempel:

New-AzBatchAccount –AccountName <account_name> –Location "Central US" –ResourceGroupName <res_group_name>

Anmärkning

Batchkontots namn måste vara unikt inom Azure-regionen för resursgruppen, innehålla mellan 3 och 24 tecken och endast använda gemener och siffror.

Skaffa kontots åtkomstnycklar

Get-AzBatchAccountKeys shows the access keys associated with an Azure Batch account. Till exempel, kör följande för att få de primära och sekundära nycklarna för det konto du skapade.

$Account = Get-AzBatchAccountKeys –AccountName <account_name>

$Account.PrimaryAccountKey

$Account.SecondaryAccountKey

Skapa en ny åtkomstnyckel

New-AzBatchAccountKey genererar en ny primär eller sekundär kontonyckel för ett Azure Batch-konto. Till exempel, för att generera en ny primärnyckel för ditt Batch-konto, skriv:

New-AzBatchAccountKey -AccountName <account_name> -KeyType Primary

Anmärkning

För att generera en ny sekundärnyckel, ange "Secondary" för KeyType-parametern. You have to regenerate the primary and secondary keys separately.

Delete a Batch account

Remove-AzBatchAccount tar bort ett Batch-konto. Till exempel:

Remove-AzBatchAccount -AccountName <account_name>

When prompted, confirm you want to remove the account. Account removal can take some time to complete.

Skapa ett BatchAccountContext-objekt

Du kan autentisera för att hantera Batch-resurser genom att använda antingen delad nyckelautentisering eller Microsoft Entra-autentisering. To authenticate using the Batch PowerShell cmdlets, first create a BatchAccountContext object to store your account credentials or identity. Du skickar BatchAccountContext-objektet till cmdletar som använder BatchContext-parametern.

Shared key authentication

$context = Get-AzBatchAccountKeys -AccountName <account_name>

Anmärkning

By default, the account's primary key is used for authentication, but you can explicitly select the key to use by changing your BatchAccountContext object’s KeyInUse property: $context.KeyInUse = "Secondary".

Microsoft Entra-autentisering

$context = Get-AzBatchAccount -AccountName <account_name>

Skapa och modifiera Batch-resurser

Use cmdlets such as New-AzBatchPool, New-AzBatchJob, and New-AzBatchTask to create resources under a Batch account. Det finns motsvarande Get- och Set- cmdlets för att uppdatera egenskaperna hos befintliga resurser, och Remove- cmdlets för att ta bort resurser under ett Batch-konto.

When using many of these cmdlets, in addition to passing a BatchContext object, you need to create or pass objects that contain detailed resource settings, as shown in the following example. See the detailed help for each cmdlet for additional examples.

Skapa en Batch pool

När du skapar eller uppdaterar en Batch-pool anger du en konfiguration. Pools should generally be configured with Virtual Machine Configuration, which lets you either specify one of the supported Linux or Windows VM images listed in the Azure Virtual Machines Marketplace, or provide a custom image that you have prepared. Molntjänsters konfigurationspooler tillhandahåller endast Windows-beräkningsnoder och stöder inte alla funktioner i Batch.

När du kör New-AzBatchPool, skicka operativsysteminställningarna i ett PSVirtualMachineConfiguration- eller PSCloudServiceConfiguration-objekt. Till exempel skapar följande kodsnutt en Batch-pool med storlek Standard_A1 beräkningsnoder i den virtuella maskinkonfigurationen, avbildad med Ubuntu Server 20.04-LTS. Här anger parametern VirtualMachineConfiguration variabeln $configuration som objektet PSVirtualMachineConfiguration. The BatchContext parameter specifies a previously defined variable $context as the BatchAccountContext object.

$imageRef = New-Object -TypeName "Microsoft.Azure.Commands.Batch.Models.PSImageReference" -ArgumentList @("UbuntuServer","Canonical","20.04-LTS")

$configuration = New-Object -TypeName "Microsoft.Azure.Commands.Batch.Models.PSVirtualMachineConfiguration" -ArgumentList @($imageRef, "batch.node.ubuntu 20.04")

New-AzBatchPool -Id "mypspool" -VirtualMachineSize "Standard_a1" -VirtualMachineConfiguration $configuration -AutoScaleFormula '$TargetDedicated=4;' -BatchContext $context

The target number of compute nodes in the new pool is calculated by an autoscaling formula. In this case, the formula is simply $TargetDedicated=4, indicating the number of compute nodes in the pool is 4 at most.

Query for pools, jobs, tasks, and other details

Use cmdlets such as Get-AzBatchPool, Get-AzBatchJob, and Get-AzBatchTask to query for entities created under a Batch account.

Fråga efter data

Som ett exempel, använd Get-AzBatchPools för att hitta dina pooler. Som standard frågar den här efter alla pooler under ditt konto, förutsatt att du redan har lagrat objektet BatchAccountContext i $context:

Get-AzBatchPool -BatchContext $context

Använd ett OData-filter

Du kan tillhandahålla ett OData-filter genom att använda Filter-parametern för att endast hitta de objekt du är intresserad av. For example, you can find all pools with IDs starting with “myPool”:

$filter = "startswith(id,'myPool')"

Get-AzBatchPool -Filter $filter -BatchContext $context

This method is not as flexible as using “Where-Object” in a local pipeline. Frågan skickas dock direkt till Batch-tjänsten så att all filtrering sker på serversidan, vilket sparar internetbandbredd.

Use the Id parameter

Ett alternativ till ett OData-filter är att använda parametern Id. För att fråga efter en specifik pool med id "myPool":

Get-AzBatchPool -Id "myPool" -BatchContext $context

The Id parameter supports only full-ID search; not wildcards or OData-style filters.

Use the MaxCount parameter

Som standard returnerar varje cmdlet högst 1000 objekt. If you reach this limit, either refine your filter to bring back fewer objects, or explicitly set a maximum using the MaxCount parameter. Till exempel:

Get-AzBatchTask -MaxCount 2500 -BatchContext $context

För att ta bort den övre gränsen, sätt MaxCount till 0 eller mindre.

Use the PowerShell pipeline

Batch-cmdletar använder PowerShell-pipelinen för att skicka data mellan cmdletar. Detta har samma effekt som att specificera en parameter, men gör det enklare att arbeta med flera enheter.

Till exempel, hitta och visa alla uppgifter under ditt konto.

Get-AzBatchJob -BatchContext $context | Get-AzBatchTask -BatchContext $context

Restart (reboot) every compute node in a pool:

Get-AzBatchComputeNode -PoolId "myPool" -BatchContext $context | Restart-AzBatchComputeNode -BatchContext $context

Application package management

Application packages provide a simplified way to deploy applications to the compute nodes in your pools. Med hjälp av Batch PowerShell-cmdlets kan du ladda upp och hantera applikationspaket i ditt Batch-konto samt distribuera paketversioner till datorkluster.

Viktigt!

You must link an Azure Storage account to your Batch account to use application packages.

Skapa en applikation:

New-AzBatchApplication -AccountName <account_name> -ResourceGroupName <res_group_name> -ApplicationId "MyBatchApplication"

Lägg till ett applikationspaket:

New-AzBatchApplicationPackage -AccountName <account_name> -ResourceGroupName <res_group_name> -ApplicationId "MyBatchApplication" -ApplicationVersion "1.0" -Format zip -FilePath package001.zip

Ställ in standardversionen för applikationen:

Set-AzBatchApplication -AccountName <account_name> -ResourceGroupName <res_group_name> -ApplicationId "MyBatchApplication" -DefaultVersion "1.0"

Lista en applikations paket

$application = Get-AzBatchApplication -AccountName <account_name> -ResourceGroupName <res_group_name> -ApplicationId "MyBatchApplication"

$application.ApplicationPackages

Delete an application package

Remove-AzBatchApplicationPackage -AccountName <account_name> -ResourceGroupName <res_group_name> -ApplicationId "MyBatchApplication" -ApplicationVersion "1.0"

Ta bort en applikation

Remove-AzBatchApplication -AccountName <account_name> -ResourceGroupName <res_group_name> -ApplicationId "MyBatchApplication"

Anmärkning

Du måste radera alla versioner av en applikations applikationspaket innan du raderar applikationen. Du kommer att få ett 'Konflikt'-fel om du försöker radera en applikation som för närvarande har applikationspaket.

Distribuera ett applikationspaket

You can specify one or more application packages for deployment when you create a pool. När du anger ett paket vid skapandet av poolen distribueras det till varje nod när noden går med i poolen. Paket distribueras också när en nod startas om eller ombildas.

Specify the -ApplicationPackageReference option when creating a pool to deploy an application package to the pool's nodes as they join the pool. Först, skapa ett PSApplicationPackageReference-objekt och konfigurera det med applikations-ID:t och paketversionen du vill distribuera till poolens beräkningsnoder.

$appPackageReference = New-Object Microsoft.Azure.Commands.Batch.Models.PSApplicationPackageReference

$appPackageReference.ApplicationId = "MyBatchApplication"

$appPackageReference.Version = "1.0"

Skapa nu poolen och ange paketreferensobjektet som argument till ApplicationPackageReferences-alternativet.

New-AzBatchPool -Id "PoolWithAppPackage" -VirtualMachineSize "Small" -VirtualMachineConfiguration $configuration -BatchContext $context -ApplicationPackageReferences $appPackageReference

You can find more information on application packages in Deploy applications to compute nodes with Batch application packages.

Uppdatera poolens applikationspaket

To update the applications assigned to an existing pool, first create a PSApplicationPackageReference object with the desired properties (application ID and package version):

$appPackageReference = New-Object Microsoft.Azure.Commands.Batch.Models.PSApplicationPackageReference

$appPackageReference.ApplicationId = "MyBatchApplication"

$appPackageReference.Version = "2.0"

Next, get the pool from Batch, clear out any existing packages, add the new package reference, and update the Batch service with the new pool settings:

$pool = Get-AzBatchPool -BatchContext $context -Id "PoolWithAppPackage"

$pool.ApplicationPackageReferences.Clear()

$pool.ApplicationPackageReferences.Add($appPackageReference)

Set-AzBatchPool -BatchContext $context -Pool $pool

Du har nu uppdaterat poolens egenskaper i Batch-tjänsten. To actually deploy the new application package to compute nodes in the pool, however, you must restart or reimage those nodes. Du kan starta om varje nod i en pool med det här kommandot:

Get-AzBatchComputeNode -PoolId "PoolWithAppPackage" -BatchContext $context | Restart-AzBatchComputeNode -BatchContext $context

Tips/Råd

Du kan distribuera flera applikationspaket till beräkningsnoderna i en pool. If you'd like to add an application package instead of replacing the currently deployed packages, omit the $pool.ApplicationPackageReferences.Clear() line above.

Nästa steg