variableB = $+variableA"

This topic contains 6 replies, has 4 voices, and was last updated by Profile photo of Ajay Kumar Ajay Kumar 2 days, 16 hours ago.

  • Author
    Posts
  • #59209
    Profile photo of Ajay Kumar
    Ajay Kumar
    Participant

    $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

  • #59212
    Profile photo of beanxyz
    beanxyz
    Participant

    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

    • #59214
      Profile photo of Ajay Kumar
      Ajay Kumar
      Participant

      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

    • #59223
      Profile photo of Olaf Soyk
      Olaf Soyk
      Participant
      $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.

  • #59230
    Profile photo of Ajay Kumar
    Ajay Kumar
    Participant

    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.

  • #59238
    Profile photo of Ron
    Ron
    Participant

    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)"
    }
    
  • #59322
    Profile photo of Ajay Kumar
    Ajay Kumar
    Participant

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

You must be logged in to reply to this topic.