Reply to Message

View discussion in a popup

Replying to:
LucD
Leadership
Leadership

Can you try with this variation on Alan's script?

Connect-viserver myvc.corp.local -user administrator@vsphere.local -pass Pa$$w0rd

$CMDBInfo = Import-CSV .\cmdbinfo.csv

# Get the header names to use as tag category names

$TagCatNames = $cmdbinfo | Get-Member | Where {$_.MemberType -eq "NoteProperty"} | Select -Expand Name

# Create the Tag Category if it doesnt exist

Foreach ($Name in ($TagCatNames | Where {$_ -ne "Name"})) {

  Try {

   $tCat = Get-TagCategory $Name -ErrorAction Stop

  }

  Catch {

   Write-Host "Creating Tag Category $Name"

   $tCat = New-TagCategory -Name $Name -Description "$Name from CMDB"

  }


  # Create Tags under the Tag Categories

  $UniqueTags = $cmdbinfo | Select -expand $Name | Get-Unique

  Foreach ($Tag in $UniqueTags) {

   Try {

   $tTag = Get-Tag $Tag -Category $tCat -ErrorAction Stop

   }

   Catch {

   Write-Host "..Creating Tag under $Name of $Tag"

   $tTag = New-Tag -Name $Tag -Category $tCat -Description "$Tag from CMDB"

   }


   # Assign the Tags to the VMs/Hosts

   $cmdbinfo | Where {$_.($Name) -eq $Tag} | Foreach {

   Write-Host ".... Assigning $Tag in Category of $Name to $($_.Name)"

   New-TagAssignment -Entity $($_.Name) -Tag $tTag | Out-Null

   } 

  }

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos