VMware Cloud Community
mprazeres183
Enthusiast
Enthusiast
Jump to solution

Timezone and Location Script to Annotation or Tag field

Hi to everyone,

I have a question and hope that a crack out there can help me on this.

We do have an International infrastructure with over 180 Locations. Our names that we use are abbreviations of the cities, example: LIM for Lima, or BEI for Beijing and so on.
I would like to script and run to all of our ESX Hosts the possibility to add an Annotation or a Tag with the following Information's:

Name                  Value

Location:             Lima

Timezone:           GMT-7

The script would get the Timezone either directly from the ESX Host configuration or get it from a CSV or from the Internet.

Regarding the Names, I do have an List with the Abbreviation and the full name, so I could create a CSV who shows the Abbreviation and the Fullname, so that the script would then check the CSV for the Abbreviation and write to the Annotation / Tag the correct Location name. Our Abbreviations are all stated on the Cluster name, (Example BAN Cluster, BEI Cluster, LIM Cluster) and so on. If there is a possibility that the script checks the clustername starts with 3 Letter Abbreviation and check CSV for output of Location. And the Timezones can be also on the same CSV, they just need to be inserted once, and it doesn't matter if Winter or Summertime, as it's the GMT Timezone.

I'm very open to any help on this Toppic, thanks a lot for the contribution.

Best regards,

Marco

Check my blog, and if my answere resolved the issue, please provide a feedback. Marco Frias - VMware is my World www.vmtn.blog
Tags (3)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

If you have a CSV with the following layout

Acronym,Location

LIM,Lime

BEI,Beijing

You could try something like this.
It creates two tag categories, Location and TimeZone.
For each VM it will create two tags in that category.
For a Lima VM that would be tag name Lima and a tag named GMT-7.


$countryCodes = @{}

Import-Csv -Path countrycodes.csv -UseCulture | %{

    $countryCodes.Add($_.Acronym,$_.Location)

}

Try{

    $lCat = Get-TagCategory -Name Location -ErrorAction stop

}

Catch{

    $lCat = New-TagCategory -Name Location -Description 'Location Info' -EntityType VirtualMachine -Cardinality Single

}

Try{

    $tCat = Get-TagCategory -Name TimeZone -ErrorAction stop

}

Catch{

    $tCat = New-TagCategory -Name TimeZone -Description 'TimeZone Info' -EntityType VirtualMachine -Cardinality Single

}

foreach($cluster in Get-Cluster){

    $location = $countryCodes[$cluster.Name.Split(" ")[0]]

    $tz = "$((Get-VMHost -Location $cluster | select -First 1).TimeZone.Key)"

    foreach($vm in Get-VM -Location $cluster){

        Try{

            $cTag = Get-Tag -Category $lCat -Name "$($location)" -ErrorAction Stop

        }

        Catch{

            $cTag = New-Tag -Name "$($location)" -Category $lCat -Description 'City'

            New-TagAssignment -Entity $vm -Tag $cTag

        }

        Try{

            $tTag = Get-Tag -Category $tCat -Name "$($tz)" -ErrorAction Stop

        }

        Catch{

            $tTag = New-Tag -Name "$($tz)" -Category $tCat -Description 'TimeZone'

            New-TagAssignment -Entity $vm -Tag $tTag

        }

    }

}


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

View solution in original post

0 Kudos
1 Reply
LucD
Leadership
Leadership
Jump to solution

If you have a CSV with the following layout

Acronym,Location

LIM,Lime

BEI,Beijing

You could try something like this.
It creates two tag categories, Location and TimeZone.
For each VM it will create two tags in that category.
For a Lima VM that would be tag name Lima and a tag named GMT-7.


$countryCodes = @{}

Import-Csv -Path countrycodes.csv -UseCulture | %{

    $countryCodes.Add($_.Acronym,$_.Location)

}

Try{

    $lCat = Get-TagCategory -Name Location -ErrorAction stop

}

Catch{

    $lCat = New-TagCategory -Name Location -Description 'Location Info' -EntityType VirtualMachine -Cardinality Single

}

Try{

    $tCat = Get-TagCategory -Name TimeZone -ErrorAction stop

}

Catch{

    $tCat = New-TagCategory -Name TimeZone -Description 'TimeZone Info' -EntityType VirtualMachine -Cardinality Single

}

foreach($cluster in Get-Cluster){

    $location = $countryCodes[$cluster.Name.Split(" ")[0]]

    $tz = "$((Get-VMHost -Location $cluster | select -First 1).TimeZone.Key)"

    foreach($vm in Get-VM -Location $cluster){

        Try{

            $cTag = Get-Tag -Category $lCat -Name "$($location)" -ErrorAction Stop

        }

        Catch{

            $cTag = New-Tag -Name "$($location)" -Category $lCat -Description 'City'

            New-TagAssignment -Entity $vm -Tag $cTag

        }

        Try{

            $tTag = Get-Tag -Category $tCat -Name "$($tz)" -ErrorAction Stop

        }

        Catch{

            $tTag = New-Tag -Name "$($tz)" -Category $tCat -Description 'TimeZone'

            New-TagAssignment -Entity $vm -Tag $tTag

        }

    }

}


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

0 Kudos