vmstoani
Contributor
Contributor

PowerCLi Script loops

Jump to solution

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

1 Solution

Accepted Solutions
LucD
Leadership
Leadership

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

View solution in original post

0 Kudos
26 Replies
LucD
Leadership
Leadership

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

0 Kudos
vmstoani
Contributor
Contributor

 

 

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
vmstoani
Contributor
Contributor

bbb

0 Kudos
vmstoani
Contributor
Contributor

bb

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
vmstoani
Contributor
Contributor

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

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
vmstoani
Contributor
Contributor

bb

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
vmstoani
Contributor
Contributor

bb

0 Kudos
LucD
Leadership
Leadership

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

  • do a trial run with a smaller CSV file. Perhaps 4 or 5 rows. This to better see what is happening.
  • remove the pipe to Out-Null on the New-TagAssignment line. This might be hiding some messages from the cmdlet.

New-TagAssignment -Entity $($_.Name) -Tag $tTag


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

0 Kudos
vmstoani
Contributor
Contributor

bb

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
vmstoani
Contributor
Contributor

bb

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
vmstoani
Contributor
Contributor

bb

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos