VMware Cloud Community
suttonsa90
Contributor
Contributor
Jump to solution

Skipping over empty cells in csv-imported array for tags

I found this code to create categories and tags from an imported csv file. This works quite well, but I'd like a way to skip over empty cells as the script iterates over the tags.

If tag one array is shorter than the other, it will produce an error since the iterated value is null. Any ideas? I'm a powershell noob so any help would be greatly appreciated!

$CMDBInfo = Import-CSV /tmp/scripts/tags.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"})) {

    if (-Not (Get-TagCategory $Name -ea SilentlyContinue)) {

        Write-Host "Creating Tag Category $Name"

        New-TagCategory -Name $Name -EntityType "VirtualMachine" | 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  | Out-Null

            }

        }         

    }

}

Error snippet:

Get-Tag : Cannot validate argument on parameter 'Name'. The argument is null, empty, or an element of the argument collection contains a null value. Supply a collection that does

not contain any null values and then try the command again.                            

At /tmp/scripts/tag_create_csv.ps1:32 char:31                                           

+             if (-Not (Get-Tag $Tag -ea SilentlyContinue)) {                           

+                               ~~~~                                                    

+ CategoryInfo          : InvalidData: (:) [Get-Tag], ParameterBindingValidationException

+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.GetTag

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Can you try this?

$CMDBInfo = Import-Csv /tmp/scripts/tags.csv

# Get the header names to use as tag category names 

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

# Create the Tag Category if it doesnt exist 

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

    if (-Not (Get-TagCategory $Name -ea SilentlyContinue)) {

        Write-Host "Creating Tag Category $Name"

        New-TagCategory -Name $Name -EntityType "VirtualMachine" | Out-Null

         

        # Create Tags under the Tag Categories 

        $UniqueTags = $cmdbinfo | Select-Object -expand $name | Get-Unique

        Foreach ($Tag in $UniqueTags) {

            if ($Tag -and (-Not (Get-Tag $Tag -ea SilentlyContinue))) {

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

                New-Tag -Name $Tag -Category $name | Out-Null

            }

        }          

    }

}


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

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

Can you try this?

$CMDBInfo = Import-Csv /tmp/scripts/tags.csv

# Get the header names to use as tag category names 

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

# Create the Tag Category if it doesnt exist 

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

    if (-Not (Get-TagCategory $Name -ea SilentlyContinue)) {

        Write-Host "Creating Tag Category $Name"

        New-TagCategory -Name $Name -EntityType "VirtualMachine" | Out-Null

         

        # Create Tags under the Tag Categories 

        $UniqueTags = $cmdbinfo | Select-Object -expand $name | Get-Unique

        Foreach ($Tag in $UniqueTags) {

            if ($Tag -and (-Not (Get-Tag $Tag -ea SilentlyContinue))) {

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

                New-Tag -Name $Tag -Category $name | Out-Null

            }

        }          

    }

}


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

0 Kudos
suttonsa90
Contributor
Contributor
Jump to solution

Hey LucD,

That worked like a charm. Thank you so much! I will dissect your code and study the conditionals more to get a full understanding. I need to spend some more time learning the basics of powershell. Thanks again.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

If something isn't immediately clear, feel free to ask.


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

0 Kudos