title: Azure Powershell Tagging VM's from CSV file
body: <p>I'm quite new to Powershell scripting. I'm trying to generate tags for azure vm's from a CSV file. </p>
  
  <p>Inside the CSV I have the following column headings:</p>
  
  <ul>
  <li>VMName</li>
  <li>Application</li>
  <li>SubCat</li>
  <li>Environment</li>
  <li>AppOwner</li>
  <li>Location</li>
  </ul>
  
  <p>I've got a test CSV which literally has the following data in it:
  MattTestVM, TestApp, TestSubapp, Dev, Matt, UK South</p>
  
  <p>I'm not sure what i've put wrong in my code to get it to add the tags.</p>
  
  <p><strong>Code</strong></p>
  
  <pre><code>#Set Credentials
  
  $cred = Get-credential
  
  # Sign-in with Azure account credentials
  
  add-azurermaccount -credential $cred
  
  # Select Azure Subscription
  
  $subscriptionId = (Get-AzureRmSubscription | Out-GridView -Title "Select an Azure Subscription ..." -PassThru).SubscriptionId
  
  #Select specified subscription ID
  
  Select-AzureRmSubscription -SubscriptionId $subscriptionId
  
  
  
  $InputCSVFilePath = "C:\test\Tagging1.csv"
  
  #Start loop
  
  foreach ($eachRecord in $InputCSVFilePath)
  
  {
  
      $VMName = $eachrecord.VMName
  
      $Application = $eachrecord.Application
  
      $SubCat = $eachrecord.SubCat
  
      $Environment = $eachrecord.Environment
  
      $AppOwner = $eachrecord.AppOwner
  
      $Location = $eachrecord.Location
  
  
  
       $r = Get-AzureRmResource -ResourceId $ResourceId -ErrorAction Continue
  
  
  
      if($r -ne $null)
  
      {
  
          if($r.tags)
  
          {
  
              # Tag - Application
  
              if($r.Tags.ContainsKey("Application"))
  
              {
  
                  $r.Tags["Application"] = $Application
  
              }
  
              else
  
              {
  
                  $r.Tags.Add("Application", $Application)
  
              }
  
  
  
              # Tag - SubCat
  
              if($r.Tags.ContainsKey("subCat"))
  
              {
  
                  $r.Tags["subCat"] = $subCat
  
              }
  
              else
  
              {
  
                  $r.Tags.Add("subCat", $subCat)
  
              }
  
  
  
              # Tag - Environment
  
              if($r.Tags.ContainsKey("Environment"))
  
              {
  
                  $r.Tags["Environment"] = $Environment
  
              }
  
              else
  
              {
  
                  $r.Tags.Add("Environment", $Environment)
  
              }
  
  
  
              # Tag - AppOwner
  
              if($r.Tags.ContainsKey("AppOwner"))
  
              {
  
                  $r.Tags["AppOwner"] = $AppOwner
  
              }
  
              else
  
              {
  
                  $r.Tags.Add("AppOwner", $AppOwner)
  
              }
  
  
  
              # Tag - Location
  
              if($r.Tags.ContainsKey("Location"))
  
              {
  
                  $r.Tags["Location"] = $Location
  
              }
  
              else
  
              {
  
                  $r.Tags.Add("Location", $Location)
  
              }
  
  
  
              #Setting the tags on the resource
  
              Set-AzureRmResource -Tag $r.Tags -ResourceId $r.ResourceId -Force
  
          }
  
          else
  
          {
  
              #Setting the tags on a resource which doesn't have tags
  
              Set-AzureRmResource -Tag @{ Application=$Application; subCat=$subCat; Environment=$Environment; AppOwner=$AppOwner; Location=$Location } -ResourceId $r.ResourceId -Force
  
          }
  
      }
  
      else
  
      {
  
          Write-Host "Resource Not Found with Resource Id: " + $ResourceId
  
      }
  
  
  
  
  
  }
  </code></pre>
  
  <p><strong>Error message</strong></p>
  
  <pre><code> Get-AzureRmResource : Cannot validate argument on parameter 'ResourceId'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
  
      At line:10 char:43
  
      +      $r = Get-AzureRmResource -ResourceId $ResourceId -ErrorAction Co ...
  
      +                                           ~~~~~~~~~~~
  
          + CategoryInfo          : InvalidData: (:) [Get-AzureRmResource], ParameterBindingValidationException
  
          + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.GetAzureResourceCmdlet
  </code></pre>