Hi,
I am having issue importing tags and getting error as below
Please help
CSV file data
"Folder","VM","IPAddress","PowerState","OS","Application","Department","Owner"
"Testing","HADOOP02",,"PoweredOff",,"Hadoop","Operations","Karl"
Script
$CMDBInfo = Import-CSV ".\Tags1.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 "VM" -and $_ -ne "Folder" -and $_ -ne "IPAddress" -and $_ -ne "OS" -and $_ -ne "PowerState"})) {
if (-Not (Get-TagCategory $Name -ea SilentlyContinue)) {
Write-Host "Creating Tag Category $Name"
New-TagCategory -Name $Name -Description "$Name from CMDB" | Out-Null
# Create Tags under the Tag Categories
$UniqueTags = $cmdbinfo | Select -expand $name | Get-Unique
Foreach ($Tag in $UniqueTags) {
if (-Not (Get-Tag $Tag -ea SilentlyContinue)) {
Write-Host "..Creating Tag under $Name of $Tag"
New-Tag -Name $Tag -Category $name -Description "$Tag from CMDB" | Out-Null
}
# Assign the Tags to the VMs/Hosts
$cmdbinfo | Where {$_.($Name) -eq $Tag} | Foreach {
Write-Host ".... Assigning $Tag in Category of $Name to $($_.Name)"
$TagAssignment = Get-Tag -Category $Name -name $Tag
New-TagAssignment -entity $($_.Name) -Tag $Tagassignment | Out-Null
}
}
}
}
Output
Get-Tag : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again
At D:\Tags\Create_Import_Category_Tags\Tags.ps1:24 char:64
+ $TagAssignment = Get-Tag -Category $Name -name $Tag
+ ~~~~
+ CategoryInfo : InvalidData: (:) [Get-Tag], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.GetTag
New-TagAssignment : Cannot bind argument to parameter 'Entity' because it is null.
At D:\Tags\Create_Import_Category_Tags\Tags.ps1:25 char:43
+ New-TagAssignment -entity $($_.Name) -Tag $Tagassignm ...
+ ~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [New-TagAssignment], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTagAssignment
It seems the loop over the complete CSV was in the wrong place.
This should fix that
# Get the header names to use as tag category names
$TagCatNames = $cmdbinfo | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" } | Select-Object -Expand Name
foreach ($row in $CMDBInfo) {
# Create the Tag Category if it doesnt exist
Foreach ($Name in ($TagCatNames | Where-Object { $_ -ne "VM" -and $_ -ne "Folder" -and $_ -ne "IPAddress" -and $_ -ne "OS" -and $_ -ne "PowerState" })) {
try {
$cat = Get-TagCategory -Name $Name -ErrorAction Stop
} catch {
Write-Host "Creating Tag Category $Name"
$cat = New-TagCategory -Name $Name -Description "$Name from CMDB"
}
# Create Tags under the Tag Categories
$UniqueTags = $row | Select-Object -expand $Name | Get-Unique
if ($UniqueTags -ne '' -and $UniqueTags -ne $null) {
Foreach ($TagName in $UniqueTags) {
try {
$tag = Get-Tag -Name $TagName -ErrorAction Stop
} catch {
Write-Host "..Creating Tag under $Name of $TagName"
$tag = New-Tag -Name $TagName -Category $name -Description "$TagName from CMDB"
}
# Assign the Tags to the VMs/Hosts
Write-Host ".... Assigning $($Tag.Name) in Category of $Name to $($row.VM)"
New-TagAssignment -entity "$($row.VM)" -Tag $tag | Out-Null
}
} else {
Write-Host "..Blank tag for $name on $($row.VM)"
}
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I think the substitution of those values is not correct.
Try like this
# 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 "VM" -and $_ -ne "Folder" -and $_ -ne "IPAddress" -and $_ -ne "OS" -and $_ -ne "PowerState"})) {
if (-Not (Get-TagCategory $Name -ea SilentlyContinue)) {
Write-Host "Creating Tag Category $Name"
New-TagCategory -Name $Name -Description "$Name from CMDB" | Out-Null
# Create Tags under the Tag Categories
$UniqueTags = $cmdbinfo | Select -expand $name | Get-Unique
Foreach ($Tag in $UniqueTags) {
if (-Not (Get-Tag $Tag -ea SilentlyContinue)) {
Write-Host "..Creating Tag under $Name of $Tag"
New-Tag -Name $Tag -Category $name -Description "$Tag from CMDB" | Out-Null
}
# Assign the Tags to the VMs/Hosts
$cmdbinfo | Where {$_.($Name) -eq $Tag} | Foreach {
Write-Host ".... Assigning $Tag in Category of $Name to $($_.$($name))"
$TagAssignment = Get-Tag -Category $Name -name $Tag
New-TagAssignment -entity "$($_.VM)" -Tag $Tagassignment | Out-Null
}
}
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
In the screen output, how can I get the name of the VM
Write-Host ".... Assigning $Tag in Category of $Name to $($_.$($name))"
For the above line, it is not showing the VM Name.
Change that line to
Write-Host ".... Assigning $Tag in Category of $Name to $($_.VM)"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
In the input csv file, there are few blank Tags and I am getting below error while importing and after the import, the VM`s those had blank Tags shows wrong tag names
Get-Tag : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again
At D:\Tags\Create_Import_Category_Tags\Tags2.ps1:28 char:64
+ $TagAssignment = Get-Tag -Category $Name -name $Tag
+ ~~~~
+ CategoryInfo : InvalidData: (:) [Get-Tag], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.GetTag
New-TagAssignment : 05/19/2020 3:21:17 AM New-TagAssignment Tag urn:vmomi:InventoryServiceTag:461ba915-153b-4070-9158-6c071807cdb4:GLOBAL is ine
h to
urn:vmomi:VirtualMachine:vm-184:E3EE19C6-3F1C-488F-9E6B-FD9863E6E7D8 because of a cardinality violation.
At D:\Tags\Create_Import_Category_Tags\Tags2.ps1:29 char:17
+ ... New-TagAssignment -entity "$($_.VM)" -Tag $Tagassignment ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-TagAssignment], CisException
+ FullyQualifiedErrorId : ViCore.Impl.V1.Service.Tagging.Cis.TaggingServiceCisImpl.CheckBatchResult.Error,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.N
t
Ok, I added a test for blank tags.
I also used a try-catch construct for the tags and tagcategories.
# 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 "VM" -and $_ -ne "Folder" -and $_ -ne "IPAddress" -and $_ -ne "OS" -and $_ -ne "PowerState"})) {
try{
$cat = Get-TagCategory -Name $Name -ErrorAction Stop
}
catch{
Write-Host "Creating Tag Category $Name"
$cat = New-TagCategory -Name $Name -Description "$Name from CMDB"
}
# Create Tags under the Tag Categories
$UniqueTags = $cmdbinfo | Select -expand $Name | Get-Unique
if($UniqueTags -ne ''){
Foreach ($Tag in $UniqueTags) {
try{
$tag = Get-Tag -Name $Tag -ErrorAction Stop
}
catch{
Write-Host "..Creating Tag under $Name of $Tag"
$tag = New-Tag -Name $Tag -Category $name -Description "$Tag from CMDB"
}
# Assign the Tags to the VMs/Hosts
$cmdbinfo | Where {"$($_.($Name))" -eq $Tag.Name} | Foreach {
Write-Host ".... Assigning $($Tag.Name) in Category of $Name to $($_.VM)"
New-TagAssignment -entity "$($_.VM)" -Tag $tag | Out-Null
}
}
}
else{
Write-Host "..Blank tag for $name"
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
Import looks good without any wrong update. but I don't see blank tag info in the output
Write-Host "..Blank tag for $name"
Also I see the below error
New-Tag : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At D:\Tags\Create_Import_Category_Tags\Tags3.ps1:27 char:38
+ $tag = New-Tag -Name $Tag -Category $name -Descriptio ...
+ ~~~~
+ CategoryInfo : InvalidData: (:) [New-Tag], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTag
And this one?
I changed the variable in the last foreach loop.
# 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 "VM" -and $_ -ne "Folder" -and $_ -ne "IPAddress" -and $_ -ne "OS" -and $_ -ne "PowerState"})) {
try{
$cat = Get-TagCategory -Name $Name -ErrorAction Stop
}
catch{
Write-Host "Creating Tag Category $Name"
$cat = New-TagCategory -Name $Name -Description "$Name from CMDB"
}
# Create Tags under the Tag Categories
$UniqueTags = $cmdbinfo | Select -expand $Name | Get-Unique
if($UniqueTags -ne ''){
Foreach ($TagName in $UniqueTags) {
try{
$tag = Get-Tag -Name $TagName -ErrorAction Stop
}
catch{
Write-Host "..Creating Tag under $Name of $TagName"
$tag = New-Tag -Name $TagName -Category $name -Description "$TagName from CMDB"
}
# Assign the Tags to the VMs/Hosts
$cmdbinfo | Where {"$($_.($Name))" -eq $Tag.Name} | Foreach {
Write-Host ".... Assigning $($Tag.Name) in Category of $Name to $($_.VM)"
New-TagAssignment -entity "$($_.VM)" -Tag $tag | Out-Null
}
}
}
else{
Write-Host "..Blank tag for $name"
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
Still I dont see the info regarding the blank tag in the output. Also I see the error as mentioned in the screenshot below
I see, Once the tag is created, it loops multiple times to assign the same tag for same machines
I observed a strange phenomena with Import-Csv and empty cells.
A normal empty cell is presented as an empty string.
But when the empty cell is at the end of a row, it is presented as $null.
I adapted the test for blank cells to handle both.
See if that makes a difference?
# 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 "VM" -and $_ -ne "Folder" -and $_ -ne "IPAddress" -and $_ -ne "OS" -and $_ -ne "PowerState"})) {
try{
$cat = Get-TagCategory -Name $Name -ErrorAction Stop
}
catch{
Write-Host "Creating Tag Category $Name"
$cat = New-TagCategory -Name $Name -Description "$Name from CMDB"
}
# Create Tags under the Tag Categories
$UniqueTags = $cmdbinfo | Select -expand $Name | Get-Unique
if($UniqueTags -ne '' -and $null -ne $UniqueTags){
Foreach ($TagName in $UniqueTags) {
try{
$tag = Get-Tag -Name $TagName -ErrorAction Stop
}
catch{
Write-Host "..Creating Tag under $Name of $TagName"
$tag = New-Tag -Name $TagName -Category $name -Description "$TagName from CMDB"
}
# Assign the Tags to the VMs/Hosts
$cmdbinfo | Where {"$($_.($Name))" -eq $Tag.Name} | Foreach {
Write-Host ".... Assigning $($Tag.Name) in Category of $Name to $($_.VM)"
New-TagAssignment -entity "$($_.VM)" -Tag $tag | Out-Null
}
}
}
else{
Write-Host "..Blank tag for $name"
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
No difference, still the same error
Works for me.
Can you show an extract of your CSV file?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
It seems the loop over the complete CSV was in the wrong place.
This should fix that
# Get the header names to use as tag category names
$TagCatNames = $cmdbinfo | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" } | Select-Object -Expand Name
foreach ($row in $CMDBInfo) {
# Create the Tag Category if it doesnt exist
Foreach ($Name in ($TagCatNames | Where-Object { $_ -ne "VM" -and $_ -ne "Folder" -and $_ -ne "IPAddress" -and $_ -ne "OS" -and $_ -ne "PowerState" })) {
try {
$cat = Get-TagCategory -Name $Name -ErrorAction Stop
} catch {
Write-Host "Creating Tag Category $Name"
$cat = New-TagCategory -Name $Name -Description "$Name from CMDB"
}
# Create Tags under the Tag Categories
$UniqueTags = $row | Select-Object -expand $Name | Get-Unique
if ($UniqueTags -ne '' -and $UniqueTags -ne $null) {
Foreach ($TagName in $UniqueTags) {
try {
$tag = Get-Tag -Name $TagName -ErrorAction Stop
} catch {
Write-Host "..Creating Tag under $Name of $TagName"
$tag = New-Tag -Name $TagName -Category $name -Description "$TagName from CMDB"
}
# Assign the Tags to the VMs/Hosts
Write-Host ".... Assigning $($Tag.Name) in Category of $Name to $($row.VM)"
New-TagAssignment -entity "$($row.VM)" -Tag $tag | Out-Null
}
} else {
Write-Host "..Blank tag for $name on $($row.VM)"
}
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Cool, this one worked flawlessly
Thank you very much