Automation

 View Only
  • 1.  Skipping over empty cells in csv-imported array for tags

    Posted Apr 10, 2020 08:05 PM

    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



  • 2.  RE: Skipping over empty cells in csv-imported array for tags
    Best Answer

    Posted Apr 10, 2020 08:15 PM

    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

                }

            }          

        }

    }



  • 3.  RE: Skipping over empty cells in csv-imported array for tags

    Posted Apr 13, 2020 01:15 PM

    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.



  • 4.  RE: Skipping over empty cells in csv-imported array for tags

    Posted Apr 13, 2020 01:19 PM

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