skip to Main Content

I am to bulk update tags on specific resources listed in a CSV file. But get the error in the title. Any Idea How I Can Convert to string?

CSV Example:

ResourceName ResourceGroup
vm10 rg01
vm11 rg02

Code:

$ResourceList = Import-Csv 'C:Users43263Desktopresources.csv'

$tags = @{"DeptNumber"="123G"; "OwnerEmail"="[email protected]"}

$getresource = Get-AzResource -Name $ResourceList.ResourceName -ResourceGroup $ResourceList.ResourceGroup

ForEach ($Resource in $ResourceList.ResourceName){
New-AzTag -ResourceId $getresource.id -Tag $tags
}

2

Answers


  1. You need to loop over each row in your CSV and query each resource separately. Also, the property value you should use for New-AzTag -ResourceId is .ResourceId not .Id.

    $tags = @{
        DeptNumber = '123G'
        OwnerEmail = '[email protected]'
    }
    
    Import-Csv 'C:Users43263Desktopresources.csv' | ForEach-Object {
        try {
            $getAzResourceSplat = @{
                Name          = $_.ResourceName
                ResourceGroup = $_.ResourceGroup
                ErrorAction   = 'Stop'
            }
    
            $resource = Get-AzResource @getAzResourceSplat
            New-AzTag -ResourceId $resource.ResourceId -Tag $tags
        }
        catch {
            Write-Warning $_
        }
    }
    
    Login or Signup to reply.
  2. I have reproduced in my environment and got expected results as below:

    CSV File:

    enter image description here

    Then used below commands:

    $r = Import-Csv "C:Users\Downloadsemo1.csv"
    $T = @{ID='0008'
     Name='rithwik'}
    ForEach ($e in $r){
        $res= Get-AzResource -Name $e.ResourceName -ResourceGroup $e.ResourceGroup
        $x= $res.ResourceId | Out-String
    }
    ForEach ($v in $res){
        New-AzTag  -ResourceId $v.ResourceId -Tag $T }
    

    Output:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search