0

I have sample code as below:

$IP=@("IP1","IP2","IP3", "IP4" , "IP5")
$Hostname=@("Host1","Host2","Host3","HOST4", "HOST5")

$data = Read-Host -Prompt 'Please enter the data.'

if ($data -match 'zone1') {
    $Unity=$IP[0]
    $show=$hostname[0]
} elseif ($data -match 'site2'){
    $Unity=$IP[1]
    $show=$hostname[1]
} elseif ($data -match 'unit5'){
    $Unity=$IP[2]
    $show=$hostname[2]
} elseif ($data -match 'ALL DC'){
    $Unity=$IP
    $show=$hostname
}



foreach ($u in $Unity){

  echo "This is my IP" $u
  echo "This is my hostname" $show

}

Codework fine for all the options except ALL DC. I want to run some command which uses hostname and Ip.

Desired Output when ALL DC is input by the user is

This is my IP IP1 This is my hostname Host1 This is my IP IP2 This is my hostname Host2 This is my IP IP3 This is my hostname Host3 This is my IP IP4 This is my hostname Host4 This is my IP IP5 This is my hostname Host5

6
  • Is this the actual code? You access $data before assigning it.
    – marsze
    Commented Oct 18, 2020 at 11:17
  • @marsze-This is the sample code and I have corrected it
    – GURU SINGH
    Commented Oct 18, 2020 at 11:23
  • It's unclear what you're actually trying to achieve. Please explain your intention better, and include expected input/output
    – marsze
    Commented Oct 18, 2020 at 11:48
  • @marsze if you ll run above code and will enter input as ALL DC, you ll come to know about my issue. For one IP it’s showing all hostname
    – GURU SINGH
    Commented Oct 18, 2020 at 11:51
  • Yes, because you assign all hostnames $show = $hostname. What exactly do you want the output to be? Add that to your question!
    – marsze
    Commented Oct 18, 2020 at 11:52

2 Answers 2

2

If I understand what you are trying to do correctly, I would probably not use separate arrays for this, but one single array containing objects in order to keep the units, ip's and hostnames together.

Then you could do:

$units = [PsCustomObject]@{unit = 'zone1'  ; ip = 'IP1'; hostname = 'Host1'},
         [PsCustomObject]@{unit = 'site2'  ; ip = 'IP2'; hostname = 'Host2'},
         [PsCustomObject]@{unit = 'unit5'  ; ip = 'IP3'; hostname = 'Host3'},
         [PsCustomObject]@{unit = 'region3'; ip = 'IP4'; hostname = 'Host4'},
         [PsCustomObject]@{unit = 'zone5'  ; ip = 'IP5'; hostname = 'Host5'}

$data = Read-Host -Prompt 'Please enter the unit'

$choice = if (![string]::IsNullOrWhiteSpace($data)) { $units | Where-Object { $_.unit -match $data }}
if ($choice) {
    Write-Host "You have chosen:" -ForegroundColor Green
    $choice | Format-Table -AutoSize
}
else {
    Write-Host "You have given invalid input.." -ForegroundColor Red
    Write-Host "These are all DC's:"
    $units | Format-Table -AutoSize
}

Is that your intent? If not, please edit your question to make it more clear.

2
  • your code is fine but in this, I have to mention a huge set of commands which are using variable IP and hostname twice under if and else statement which I am trying to avoid.
    – GURU SINGH
    Commented Oct 18, 2020 at 12:27
  • @GURUSINGH There is no mention of that in the question.. However, I don't see the problem. In the if {} you do something using $choice.ip and $choice.hostname while in the else{} you do something different, looping over all items in the $units array.
    – Theo
    Commented Oct 18, 2020 at 12:37
1

You can reduce the number of assignments required by using a switch instead of if/elseif/else, and then only picking the array indices (instead of the target items):

$indices = switch -regex ($data){
    'zone1' { 0 }
    'site2' { 1 } 
    'unit5' { 2 }
    'ALL DC' { 0..4 }
}

$Unity=$IP[$indices]
$show=$hostname[$indices]

Now you just need to replace the foreach loop with a for loop:

for($i = 0; $i -lt $Indices.Length; $i++){
  $u = $Unity[$i]
  $h = $show[$i]

  echo "This is my IP" $u
  echo "This is my hostname" $h
}

Alternatively, construct objects based on the original arrays before letting the user choose:

$IP=@("IP1","IP2","IP3", "IP4" , "IP5")
$Hostname=@("Host1","Host2","Host3","HOST4", "HOST5")

# construct a new array of objects with corresponding IP-Host details
$HostIPDetails = for($i = 0; $i -lt $IP.Length; $i++){
  [pscustomobject]@{ IP = $IP[$i]; HostName = $Hostname[$i] }
}

$data = Read-Host 'Input the data... '

$indices = switch -regex ($data){
    'zone1' { 0 }
    'site2' { 1 } 
    'unit5' { 2 }
    'ALL DC' { 0..4 }
}

foreach($machine in $HostIPDetails[$indices]){
  echo "This is my IP" $machine.IP
  echo "This is my hostname" $machine.HostName 
}
1
  • Thank you so very much Theo and Mathias for your help and effort.
    – GURU SINGH
    Commented Oct 19, 2020 at 8:24

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.