Azure PowerShell을 사용하여 태그 적용

이 문서에서는 Azure PowerShell을 사용하여 리소스, 리소스 그룹 및 구독에 태그를 지정하는 방법을 설명합니다. 태그 권장 사항 및 제한 사항은 태그를 사용하여 Azure 리소스 및 관리 계층 구조를 구성하는 방법을 참조하세요.

태그 적용

Azure PowerShell은 태그를 적용하기 위한 두 가지 명령(New-AzTagUpdate-AzTag)을 제공합니다. Az.Resources 모듈 1.12.0 버전 이상이 있어야 합니다. Get-InstalledModule -Name Az.Resources을 사용하여 버전을 확인할 수 있습니다. 해당 모듈을 설치하거나 Azure PowerShell 버전 3.6.1 이상을 설치할 수 있습니다.

New-AzTag는 리소스, 리소스 그룹 또는 구독의 모든 태그를 바꿉니다. 이 명령을 호출할 때는 태그를 지정하려는 엔터티의 리소스 ID를 제공합니다.

다음 예제에서는 스토리지 계정에 태그 집합을 적용합니다.

$tags = @{"Dept"="Finance"; "Status"="Normal"}
$resource = Get-AzResource -Name demostorage -ResourceGroup demoGroup
New-AzTag -ResourceId $resource.id -Tag $tags

명령이 완료되면 리소스에 두 개의 태그가 지정됩니다.

Properties :
        Name    Value
        ======  =======
        Dept    Finance
        Status  Normal

명령을 다시 실행하되, 이번에는 다른 태그를 사용할 경우 이전 태그가 사라집니다.

$tags = @{"Team"="Compliance"; "Environment"="Production"}
$resource = Get-AzResource -Name demostorage -ResourceGroup demoGroup
New-AzTag -ResourceId $resource.id -Tag $tags
Properties :
        Name         Value
        ===========  ==========
        Environment  Production
        Team         Compliance

이미 태그가 있는 리소스에 태그를 추가하려면 Update-AzTag를 사용합니다. -Operation 매개 변수를 Merge로 설정합니다.

$tags = @{"Dept"="Finance"; "Status"="Normal"}
$resource = Get-AzResource -Name demostorage -ResourceGroup demoGroup
Update-AzTag -ResourceId $resource.id -Tag $tags -Operation Merge

두 개의 새 태그가 추가되면서 기존 태그가 증가합니다.

Properties :
        Name         Value
        ===========  ==========
        Status       Normal
        Dept         Finance
        Team         Compliance
        Environment  Production

각 태그 이름에는 값이 하나만 있을 수 있습니다. 태그에 새 값을 제공하면 병합 작업을 사용하는 경우에도 이전 값이 바뀝니다. 다음 예제에서는 Status 태그를 Normal에서 Green으로 변경합니다.

$tags = @{"Status"="Green"}
$resource = Get-AzResource -Name demostorage -ResourceGroup demoGroup
Update-AzTag -ResourceId $resource.id -Tag $tags -Operation Merge
Properties :
        Name         Value
        ===========  ==========
        Status       Green
        Dept         Finance
        Team         Compliance
        Environment  Production

-Operation 매개 변수를 Replace로 설정하면 기존 태그가 새 태그 집합으로 바뀝니다.

$tags = @{"Project"="ECommerce"; "CostCenter"="00123"; "Team"="Web"}
$resource = Get-AzResource -Name demostorage -ResourceGroup demoGroup
Update-AzTag -ResourceId $resource.id -Tag $tags -Operation Replace

새 태그만 리소스에 남아 있습니다.

Properties :
        Name        Value
        ==========  =========
        CostCenter  00123
        Team        Web
        Project     ECommerce

리소스 그룹 또는 구독에도 동일한 명령을 사용할 수 있습니다. 태그를 지정할 리소스 그룹 또는 구독에 대한 식별자를 전달합니다.

리소스 그룹에 새 태그 집합을 추가하려면 다음을 사용합니다.

$tags = @{"Dept"="Finance"; "Status"="Normal"}
$resourceGroup = Get-AzResourceGroup -Name demoGroup
New-AzTag -ResourceId $resourceGroup.ResourceId -tag $tags

리소스 그룹의 태그를 업데이트하려면 다음을 사용합니다.

$tags = @{"CostCenter"="00123"; "Environment"="Production"}
$resourceGroup = Get-AzResourceGroup -Name demoGroup
Update-AzTag -ResourceId $resourceGroup.ResourceId -Tag $tags -Operation Merge

구독에 새 태그 집합을 추가하려면 다음을 사용합니다.

$tags = @{"CostCenter"="00123"; "Environment"="Dev"}
$subscription = (Get-AzSubscription -SubscriptionName "Example Subscription").Id
New-AzTag -ResourceId "/subscriptions/$subscription" -Tag $tags

구독의 태그를 업데이트하려면 다음을 사용합니다.

$tags = @{"Team"="Web Apps"}
$subscription = (Get-AzSubscription -SubscriptionName "Example Subscription").Id
Update-AzTag -ResourceId "/subscriptions/$subscription" -Tag $tags -Operation Merge

리소스 그룹에 동일한 이름의 리소스가 둘 이상 있을 수 있습니다. 이 경우 다음 명령을 사용하여 각 리소스를 설정할 수 있습니다.

$resource = Get-AzResource -ResourceName sqlDatabase1 -ResourceGroupName examplegroup
$resource | ForEach-Object { Update-AzTag -Tag @{ "Dept"="IT"; "Environment"="Test" } -ResourceId $_.ResourceId -Operation Merge }

태그 나열

리소스, 리소스 그룹 또는 구독의 태그를 가져오려면 Get-AzTag 명령을 사용하여 엔터티의 리소스 ID를 전달합니다.

리소스의 태그를 보려면 다음을 사용합니다.

$resource = Get-AzResource -Name demostorage -ResourceGroup demoGroup
Get-AzTag -ResourceId $resource.id

리소스 그룹의 태그를 보려면 다음을 사용합니다.

$resourceGroup = Get-AzResourceGroup -Name demoGroup
Get-AzTag -ResourceId $resourceGroup.ResourceId

구독의 태그를 보려면 다음을 사용합니다.

$subscription = (Get-AzSubscription -SubscriptionName "Example Subscription").Id
Get-AzTag -ResourceId "/subscriptions/$subscription"

태그로 나열

특정 태그 이름 및 값이 있는 리소스를 가져오려면 다음을 사용합니다.

(Get-AzResource -Tag @{ "CostCenter"="00123"}).Name

태그 값에 관계없이 특정 태그 이름이 있는 리소스를 가져오려면 다음을 사용합니다.

(Get-AzResource -TagName "Dept").Name

특정 태그 이름 및 값이 있는 리소스 그룹을 가져오려면 다음을 사용합니다.

(Get-AzResourceGroup -Tag @{ "CostCenter"="00123" }).ResourceGroupName

태그 제거

특정 태그를 제거하려면 Update-AzTag를 사용하고 -OperationDelete로 설정합니다. 삭제하려는 태그의 리소스 ID를 전달합니다.

$removeTags = @{"Project"="ECommerce"; "Team"="Web"}
$resource = Get-AzResource -Name demostorage -ResourceGroup demoGroup
Update-AzTag -ResourceId $resource.id -Tag $removeTags -Operation Delete

지정된 태그가 제거됩니다.

Properties :
        Name        Value
        ==========  =====
        CostCenter  00123

모든 태그를 제거하려면 Remove-AzTag 명령을 사용합니다.

$subscription = (Get-AzSubscription -SubscriptionName "Example Subscription").Id
Remove-AzTag -ResourceId "/subscriptions/$subscription"

다음 단계