Hello,
I've taken this Script for Datastore Tags:
Connect-viserver xxxxxxx -user xxxxxxxx -password xxxxxxxx
$CMDBInfo = Import-CSV .\DS-Tag.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
}
# 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
}
# 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
}
}
}
The csv File looke like this:
Name,Location,StoragePool,Sync
The Script generates only the catagory "Location" with the Tags xxx oder xxx. Then it runs in a loop with no errors. The other catagories and tags are not generated.
What's wrong?
Thank you
Best regards
Andi
Ok, one more try.
What does this produce?
$CMDBInfo = Import-CSV .\DS-Tag.csv -Delimiter ';'
$TagCatNames = $cmdbinfo | Get-Member | Where {$_.MemberType -eq "NoteProperty"} | Select -Expand Name
Foreach ($Name in ($TagCatNames | Where {$_ -ne "Name"})){
Write-Host "Category : $Name"
$UniqueTags = $cmdbinfo | Select -expand $Name | Sort-Object | Get-Unique
Foreach ($Tag in $UniqueTags){
$cmdbinfo | Where {$_.($Name) -eq $Tag} | Foreach {
Write-Host "`tCategory: $Name - Tag : $Tag - Entity: $($_.Name)"
}
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Do you see all properties when you do
Import-CSV .\DS-Tag.csv
If not, try doing
Import-CSV .\DS-Tag.csv -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Looks like the delimiter in the CSV file might not be interpreted correctly.
Can you try by explicitly specifying the delimiter.
Import-CSV .\DS-Tag.csv -Delimiter ','
And you check if there is a difference by doing
Import-CSV .\DS-Tag.csv | Get-Member
Import-CSV .\DS-Tag.csv -Delimiter ',' | Get-Member
Check if all the expected properties are listed.
You can also check what the system places in a CSV file as separator.
With something like for example
Get-VM | Select Name,NumCpu,PowerState |
Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture
In the file check how the columns are separated.
Are all the properties perhaps enclosed in quotes?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
bbb
bb
So what happens with the script when you explicitly add the -Delimiter ';' on the Import-Csv cmdlet?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Luc,
Adding the Delimiter to the Script changes nothing. Only the Tags of the category "Location" are added to the Datastores.
Then the Script starts form the scratch.
greetings
Andi
Did you ever try this?
What is the result?
Import-CSV .\DS-Tag.csv | Get-Member
Import-CSV .\DS-Tag.csv -Delimiter ';' | Get-Member
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You don't happen to have multiple connections open?
Can you check what $global:defaultviservers shows?
And would you mind attaching the script you are using as a file to this thread (bottom right - Attach button).
I just tested again with Alan's script, and for me it work without an issue.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
bb
Ok, thanks.
This confirms that you have to use the Delimiter parameter, otherwise the Import-Csv will read each row in the CSV as one property.
With the Delimiter, you'll notice that all columns in the CSV are listed as separate properties.
I find it hard to understand why using the Delimiter on the Import-Csv in the script makes no difference according to you.
Can you perhaps attach the current version of the script you are using?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The script looks ok.
From your previous script run output I deduce that the TagCategories are all already there.
But the Tags in those TagCategories apparently not.
Can you check with
Get-TagCategory
Get-Tag
And can you
New-TagAssignment -Entity $($_.Name) -Tag $tTag
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
bb
I ran a skeleton script with your input data, just displaying what kind of Tag Assignments are done.
And whatever I try, I can't replicate what you are seeing.
Can you run the following with your input data on your side?
$CMDBInfo = Import-CSV .\DS-Tag.csv -Delimiter ';'
$TagCatNames = $cmdbinfo | Get-Member | Where {$_.MemberType -eq "NoteProperty"} | Select -Expand Name
Foreach ($Name in ($TagCatNames | Where {$_ -ne "Name"})){
Write-Host "Category : $Name"
$UniqueTags = $cmdbinfo | Select -expand $Name | Get-Unique
Foreach ($Tag in $UniqueTags){
$cmdbinfo | Where {$_.($Name) -eq $Tag} | Foreach {
Write-Host "Category: $Name - Tag : $Tag - Entity: $($_.Name)"
}
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
bb
I have to admit I'm running out of ideas on what goes wrong here.
I tested the same script with the data you provided earlier, and it works perfectly.
One more shot, can you also try the following?
$CMDBInfo = Import-CSV .\DS-Tag.csv -Delimiter ';' |
Sort-Object -Property Name,Location -Unique
Write-Host "There are $($CMDBInfo.Count) entries"
$TagCatNames = $cmdbinfo | Get-Member | Where {$_.MemberType -eq "NoteProperty"} | Select -Expand Name
Foreach ($Name in ($TagCatNames | Where {$_ -ne "Name"})){
Write-Host "Category : $Name"
$UniqueTags = $cmdbinfo | Select -expand $Name | Get-Unique
Foreach ($Tag in $UniqueTags){
$cmdbinfo | Where {$_.($Name) -eq $Tag} | Foreach {
Write-Host "Category: $Name - Tag : $Tag - Entity: $($_.Name)"
}
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
bb
So now Location and Sync seem to be ok, but StoragePool has 2 out of 4 double assignments?
I'm not sure anymore what to make of that.
How and from where are you running the code?
From the prompt?
From the ISE or Visual Studio Code?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference