For loop for Array in Powershell

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 use hostname and Ip.



Read more here: https://stackoverflow.com/questions/64412160/for-loop-for-array-in-powershell

Content Attribution

This content was originally published by GURU SINGH at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: