VMware Cloud Community
vmstoani
Contributor
Contributor
Jump to solution

PowerCLi Script loops

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

26 Replies
vmstoani
Contributor
Contributor
Jump to solution

From the PowerShell Prompt and ISE. Both with the same output.

PS C:\Users\z267497> $PSVersionTable

Name                           Value

----                           -----

PSVersion                      5.1.14393.2608

PSEdition                      Desktop

PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}

BuildVersion                   10.0.14393.2608

CLRVersion                     4.0.30319.42000

WSManStackVersion              3.0

PSRemotingProtocolVersion      2.3

SerializationVersion           1.1.0.1

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I give up, there must be something very fishy in your environment, script or data file.

And I'm out of ideas to determine, from where I'm sitting, what it is Smiley Sad


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

LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
vmstoani
Contributor
Contributor
Jump to solution

Hi Luc,

Now it worksSmiley Happy

vm00300.org.oebb.at            443   ORG\z267497                  

Category : Location

Category: Location - Tag : ARS - Entity: ARS_SVC_MS-ONLY_VOL_17

Category: Location - Tag : ARS - Entity: ARS_SVC_MS-ONLY_VOL_26

Category: Location - Tag : LA2 - Entity: SYNC_LA2_SVC_INFRATIS_VOL_01

Category: Location - Tag : LA2 - Entity: SYNC_LA2_SVC_COMMVAULT_VOL_01

Category : StoragePool

Category: StoragePool - Tag : FLASH - Entity: SYNC_LA2_SVC_COMMVAULT_VOL_01

Category: StoragePool - Tag : MIXED - Entity: SYNC_LA2_SVC_INFRATIS_VOL_01

Category: StoragePool - Tag : MIXED - Entity: ARS_SVC_MS-ONLY_VOL_17

Category: StoragePool - Tag : NEARLINE - Entity: ARS_SVC_MS-ONLY_VOL_26

Category : Sync

Category: Sync - Tag : JA - Entity: SYNC_LA2_SVC_INFRATIS_VOL_01

Category: Sync - Tag : JA - Entity: SYNC_LA2_SVC_COMMVAULT_VOL_01

Category: Sync - Tag : NEIN - Entity: ARS_SVC_MS-ONLY_VOL_17

Category: Sync - Tag : NEIN - Entity: ARS_SVC_MS-ONLY_VOL_26

Because I'm a newbie in PowerShell/PowerCLi - why does it work?

Thank you so much for your assistance.

Great:smileyplus::smileyplus:

Andi

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I was checking all the cmdlets, and I discovered (didn't know tat either) that the Get-Unique cmdlet expects a sorted list.

Since the data in your CSV is not sorted, the Get-Unique provided several names twice.

By adding the Sort-Object before the Get-Unique, we are guaranteed that a sorted list is passed to Get-Unique.

Pweh, this took a while :smileygrin:


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

vmstoani
Contributor
Contributor
Jump to solution

Hi Luc,

It's absolutely fantastic how you are working on all those problems and answer them again and again.

Greatest respect to you.!!!!

Thank you very, very much

Andi

Reply
0 Kudos
Jrt1general
Enthusiast
Enthusiast
Jump to solution

Came here for the same problem and learned something new with this solution. Kudos LucD, you really do help me be a better engineer.

Reply
0 Kudos