$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
beanxyz
December 1, 2016, 11:52pm
2
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
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
Olaf
December 2, 2016, 3:30am
4
$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.
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.
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)"
}
Hi Ron,
Excellent. Thanks a lot for the tip.