PowerShell 작업을 사용하여 병렬로 cmdlet 실행Running cmdlets in parallel using PowerShell jobs

PowerShell에서는 PowerShell 작업을 통해 비동기 작업을 지원합니다.PowerShell supports asynchronous action with PowerShell Jobs. Azure PowerShell은 Azure에 대한 네트워크 호출 만들기 및 대기에 크게 의존합니다.Azure PowerShell is heavily dependent on making, and waiting for, network calls to Azure. 일부 경우에는 비차단 호출을 수행해야 할 수 있습니다.You may often find yourself needing to make non-blocking calls. 이러한 요구를 해결하기 위해 Azure PowerShell은 최고 수준의 PSJob 지원을 제공합니다.To address this need, Azure PowerShell provides first-class PSJob support.

컨텍스트 지속성 및 PSJobContext Persistence and PSJobs

PSJob은 개별 프로세스로 실행되므로 Azure 연결을 공유해야 합니다.Since PSJobs are run as separate processes, your Azure connection must be shared with them. Connect-AzureRmAccount로 Azure 계정에 로그인한 후 작업에 컨텍스트를 전달합니다.After signing in to your Azure account with Connect-AzureRmAccount, pass the context to a job.

$creds = Get-Credential
$job = Start-Job { param($context,$vmadmin) New-AzureRmVM -Name MyVm -AzureRmContext $context -Credential $vmadmin} -Arguments (Get-AzureRmContext),$creds

그러나 컨텍스트가 Enable-AzureRmContextAutosave로 자동 저장되도록 선택한 경우 컨텍스트는 작성한 모든 작업과 자동으로 공유됩니다.However, if you have chosen to have your context automatically saved with Enable-AzureRmContextAutosave, the context is automatically shared with any jobs you create.

Enable-AzureRmContextAutosave
$creds = Get-Credential
$job = Start-Job { param($vmadmin) New-AzureRmVM -Name MyVm -Credential $vmadmin} -Arguments $creds

-AsJob을 포함한 자동 작업Automatic Jobs with -AsJob

편의를 위해 Azure PowerShell에서는 장기 실행 cmdlet에 -AsJob 스위치를 제공합니다.As a convenience, Azure PowerShell also provides an -AsJob switch on some long-running cmdlets. -AsJob 스위치를 사용하면 PSJob을 보다 쉽게 만들 수 있습니다.The -AsJob switch makes creating PSJobs even easier.

$creds = Get-Credential
$job = New-AzureRmVM -Name MyVm -Credential $creds -AsJob

Get-JobGet-AzureRmVM을 사용하여 작업 및 진행률을 언제든지 검사할 수 있습니다.You can inspect the job and progress at any time with Get-Job and Get-AzureRmVM.

Get-Job $job
Get-AzureRmVM MyVm
Id Name                                       PSJobTypeName         State   HasMoreData Location  Command
-- ----                                       -------------         -----   ----------- --------  -------
1  Long Running Operation for 'New-AzureRmVM' AzureLongRunningJob`1 Running True        localhost New-AzureRmVM

ResourceGroupName    Name Location          VmSize  OsType     NIC ProvisioningState Zone
-----------------    ---- --------          ------  ------     --- ----------------- ----
MyVm                 MyVm   eastus Standard_DS1_v2 Windows    MyVm          Creating

작업이 완료되면 Receive-Job으로 작업 결과를 가져옵니다.When the job completes, get the result of the job with Receive-Job.

注意

Receive-Job-AsJob 플래그가 없는 것처럼 cmdlet으로부터 결과를 반환합니다.Receive-Job returns the result from the cmdlet as if the -AsJob flag were not present. 예를 들어 Do-Action -AsJobReceive-Job 결과는 Do-Action의 결과와 같은 형식입니다.For example, the Receive-Job result of Do-Action -AsJob is of the same type as the result of Do-Action.

$vm = Receive-Job $job
$vm
ResourceGroupName        : MyVm
Id                       : /subscriptions/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/resourceGroups/MyVm/providers/Microsoft.Compute/virtualMachines/MyVm
VmId                     : dff1f79e-a8f7-4664-ab72-0ec28b9fbb5b
Name                     : MyVm
Type                     : Microsoft.Compute/virtualMachines
Location                 : eastus
Tags                     : {}
HardwareProfile          : {VmSize}
NetworkProfile           : {NetworkInterfaces}
OSProfile                : {ComputerName, AdminUsername, WindowsConfiguration, Secrets}
ProvisioningState        : Succeeded
StorageProfile           : {ImageReference, OsDisk, DataDisks}
FullyQualifiedDomainName : myvmmyvm.eastus.cloudapp.azure.com

예제 시나리오Example Scenarios

한 번에 여러 VM을 만들기:Create several VMs at once:

$creds = Get-Credential
# Create 10 jobs.
for($k = 0; $k -lt 10; $k++) {
    New-AzureRmVm -Name MyVm$k  -Credential $creds -AsJob
}

# Get all jobs and wait on them.
Get-Job | Wait-Job
"All jobs completed"
Get-AzureRmVM

이 예제에서 Wait-Job cmdlet을 사용하면 작업이 실행되는 동안 스크립트가 일시 중지됩니다.In this example, the Wait-Job cmdlet causes the script to pause while jobs run. 스크립트는 모든 작업이 완료되면 실행을 계속합니다.The script continues executing once all of the jobs have completed. 여러 작업이 병렬로 실행되고 스크립트가 완료될 때까지 기다린 후 작업을 계속합니다.Several jobs run in parallel then the script waits for completion before continuing.

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
2      Long Running... AzureLongRun... Running       True            localhost            New-AzureRmVM
3      Long Running... AzureLongRun... Running       True            localhost            New-AzureRmVM
4      Long Running... AzureLongRun... Running       True            localhost            New-AzureRmVM
5      Long Running... AzureLongRun... Running       True            localhost            New-AzureRmVM
6      Long Running... AzureLongRun... Running       True            localhost            New-AzureRmVM
7      Long Running... AzureLongRun... Running       True            localhost            New-AzureRmVM
8      Long Running... AzureLongRun... Running       True            localhost            New-AzureRmVM
9      Long Running... AzureLongRun... Running       True            localhost            New-AzureRmVM
10     Long Running... AzureLongRun... Running       True            localhost            New-AzureRmVM
11     Long Running... AzureLongRun... Running       True            localhost            New-AzureRmVM
2      Long Running... AzureLongRun... Completed     True            localhost            New-AzureRmVM
3      Long Running... AzureLongRun... Completed     True            localhost            New-AzureRmVM
4      Long Running... AzureLongRun... Completed     True            localhost            New-AzureRmVM
5      Long Running... AzureLongRun... Completed     True            localhost            New-AzureRmVM
6      Long Running... AzureLongRun... Completed     True            localhost            New-AzureRmVM
7      Long Running... AzureLongRun... Completed     True            localhost            New-AzureRmVM
8      Long Running... AzureLongRun... Completed     True            localhost            New-AzureRmVM
9      Long Running... AzureLongRun... Completed     True            localhost            New-AzureRmVM
10     Long Running... AzureLongRun... Completed     True            localhost            New-AzureRmVM
11     Long Running... AzureLongRun... Completed     True            localhost            New-AzureRmVM
All Jobs completed.

ResourceGroupName        Name   Location          VmSize  OsType           NIC ProvisioningState Zone
-----------------        ----   --------          ------  ------           --- ----------------- ----
MYVM                     MyVm     eastus Standard_DS1_v2 Windows          MyVm         Succeeded
MYVM0                   MyVm0     eastus Standard_DS1_v2 Windows         MyVm0         Succeeded
MYVM1                   MyVm1     eastus Standard_DS1_v2 Windows         MyVm1         Succeeded
MYVM2                   MyVm2     eastus Standard_DS1_v2 Windows         MyVm2         Succeeded
MYVM3                   MyVm3     eastus Standard_DS1_v2 Windows         MyVm3         Succeeded
MYVM4                   MyVm4     eastus Standard_DS1_v2 Windows         MyVm4         Succeeded
MYVM5                   MyVm5     eastus Standard_DS1_v2 Windows         MyVm5         Succeeded
MYVM6                   MyVm6     eastus Standard_DS1_v2 Windows         MyVm6         Succeeded
MYVM7                   MyVm7     eastus Standard_DS1_v2 Windows         MyVm7         Succeeded
MYVM8                   MyVm8     eastus Standard_DS1_v2 Windows         MyVm8         Succeeded
MYVM9                   MyVm9     eastus Standard_DS1_v2 Windows         MyVm9         Succeeded