Ahhhh, I see it now. Your objects have different properties. One has ExistsTagList and the other has ExistsTagName.
If the column should be ExistsTagName then change ExistsTagList to ExistsTagName.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi,
Below is my VMWare tagging assignment script. I have seen that the Description/Title is repeating every time when I set Format-Table -AutoSize
clear
$error.clear()
$FormatEnumerationLimit = 8
#Get-Tag | Export-Csv -Path C:\temp\gettaglist.csv -NoTypeInformation
$importtaglist = Import-Csv -Path C:\temp\gettaglist.csv
$newtaglist = Import-Csv -Path C:\temp\newtaglist.csv
$taglistname = @()
$taglistcategory = @()
$output =@()
for($i=0; $i -lt $importtaglist.Count; $i++)
{
$taglistname += $importtaglist[$i].Name
$taglistcategory += $importtaglist[$i].Category
}
foreach($tags in $newtaglist)
{
$taglist = $tags.Name
$tagcategory = $tags.Category
if(($taglistname -contains $taglist) -and ($taglistcategory -contains $tagcategory))
{
$output += [PSCustomObject]@{
NewTagName = $taglist;
ExistsTagList = "Match";
NewCategory=$tagcategory;
ExistsCategory=$tagcategory
} | ft -AutoSize
}
else
{
$output += [PSCustomObject]@{
NewTagName=$taglist;
ExistsTagName="Not Match";
NewCategory=$tagcategory;
ExistsCategory="Not Match"
} | ft -AutoSize
}
}
$output
I need it Below output instead of multiple headings / Descriptions /Title from
Require Output
Result output.
Ahhhh, I see it now. Your objects have different properties. One has ExistsTagList and the other has ExistsTagName.
If the column should be ExistsTagName then change ExistsTagList to ExistsTagName.
Don't pipe each custom object to format-table. Just add it to the output variable.
$output += [PSCustomObject]@{
NewTagName=$taglist;
ExistsTagName="Not Match";
NewCategory=$tagcategory;
ExistsCategory="Not Match"
}
After all the data has been gathered, pipe the entire $output variable to format-table -autosize.
Is it important that name and category in both data sets match? In other words, that the name in one list and its associated value, match the name and category in the other? Because your code only requires that the name and category are simply present in the 1st list. In a very simple example, you might have this condition:
List 1:
Name Category
A One
B Two
List 2:
Name Category
A Two
B One
. . . and the script will say they match!
Here's another way to rewrite the script and, if the script should be verifying that the Name/Category pairs match in both lists, it's a simple matter to modify the code to accomplish that.
Clear-Host
$error.clear()
#Get-Tag | Export-Csv -Path C:\temp\gettaglist.csv -NoTypeInformation
# Create two hashes instead of two arrays (faster lookups)
# only the keys are necessary, leave the values as empty strings
$taglistname = @{}
$taglistcategory = @{}
Import-Csv -Path C:\temp\gettaglist.csv|
ForEach-Object{
$taglistname[$_.Name] = ""
$taglistcategory[$_.Category] = ""
}
Import-Csv -Path C:\temp\newtaglist.csv |
ForEach-Object{
$match = "Not Match"
if ($taglistname.ContainsKey($_.Name) -AND $taglistcategory.ContainsKey($_.Category){
$match = "Match"
}
[PSCustomObject]@{
NewTagName = $_.Name
ExistsTagName = $Match
NewCategory=$_.Category
ExistsCategory=$_.Category
}
}