Author Posts

December 2, 2016 at 4:36 am

$ZZSG1 = "SGvcenter.domain.com"
$ZZBLR2 = "BlgvCenter.domain.com"
$ZZUS2 = "USvCenter.domain.com"
$siteid = Read-Host "enter physical site id eg:ZZUS2"
$siteids = "ZZSG1","ZZBLR2","ZZUS2"
if ($siteids -notcontains $siteid) {
Write-Host "siteid not found"}
else{
$SID = "$"+"$siteid"
Write-Host "$siteid contains this vCenter $SID"}
$SID
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The above is my script and the output is
ZZSG1 contains this vCenter $ZZSG1
$ZZSG1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I am expecting output as
ZZSG1 contains this vCenter SGvcenter.domain.com
SGvcenter.domain.com

December 2, 2016 at 5:22 am

My answer:

$dict1=@{
'ZZSG1' = "SGvcenter.domain.com";
'ZZBLR2' = "BlgvCenter.domain.com";
'ZZUS2' = "USvCenter.domain.com"}

$siteid = Read-Host "enter physical site id eg:ZZUS2"
$siteids = "ZZSG1","ZZBLR2","ZZUS2"
if ($siteids -notcontains $siteid) {
Write-Host "siteid not found"}
else{
$SID =$dict1[$sid]
Write-Host "$siteid contains this vCenter $sid "}
$SID

December 2, 2016 at 5:35 am

Thanks for your reply.
I am getting the below error and $SID is blank.
"Index operation failed; the array index evaluated to null."

Please suggest

December 2, 2016 at 9:00 am

$dict1=@{
'ZZSG1' = "SGvcenter.domain.com";
'ZZBLR2' = "BlgvCenter.domain.com";
'ZZUS2' = "USvCenter.domain.com"}

$siteid = Read-Host "enter physical site id eg:ZZUS2"
$siteids = "ZZSG1","ZZBLR2","ZZUS2"
if ($siteids -notcontains $siteid) {
Write-Host "siteid not found"}
else{
$SID =$dict1.$siteid
Write-Host "$siteid contains this vCenter $sid "}
$SID

BTW: Make yours and our life easier and use the code formatting options you have here in the forum please.

December 2, 2016 at 11:36 am

That worked perfect.

Thanks beanxyz and olaf.
I am new to these forums. This is my first post. Thank you guys to make my life easier.
@olaf: I will follow code format rules from the next time onwards.

Happy Christmas.

December 2, 2016 at 3:48 pm

The hash table is a better solution, but if you need to make something work like your original example, there is a way to determine the contents of a variable with the name stored as a string:

$ZZSG1 = "SGvcenter.domain.com"
$ZZBLR2 = "BlgvCenter.domain.com"
$ZZUS2 = "USvCenter.domain.com"
$siteid = "ZZSG1"
$siteids = "ZZSG1","ZZBLR2","ZZUS2"
if ($siteids -notcontains $siteid) {
  Write-Host "$siteid not found"}
else{
  Write-Host "$siteid contains this vCenter $((Get-Variable $siteid).value)"
}

December 4, 2016 at 11:37 am

Hi Ron,
Excellent. Thanks a lot for the tip.