Hi guys,
So I have been stuck on a problem and I think I need a bit of guidance in dealing with it.
My issue is that I have two arrays and I need to output an object with elements of both arrays in it. Each Element in both arrays has an opposite element that is similar and I’d like to output each line with the corresponding element… (hope this makes sense)
For Example - consider this below. You will see that each disk has a snapshot with a similar name (I did get this code off the Internet)
$Snapshots = 'Server1_disk1_Snapshot','Server_disk2_Snapshot','Server_disk3_Snapshot','Server_disk4_Snapshot' $Drives = 'Server1_disk1','Server_disk2','Server_disk3','Server_disk4' $h = @{} if ($Drives.Length -ne $Snapshots.Length) { Write-Error -Message "Array lengths do not match" ` -Category InvalidData ` } else { for ($i = 0; $i -lt $Drives.Length; $i++) { $h[$Drives[$i]] = $Snapshots[$i] } } Write-Output $h
Now the above gave me a output like:
Name Value ---- ----- Server_disk4 Server_disk4_Snapshot Server_disk3 Server_disk3_Snapshot Server_disk6 Server_disk6_Snapshot Server_disk5 Server_disk5_Snapshot Server_disk2 Server_disk2_Snapshot Server1_disk1 Server1_disk1_Snapshot
Id like to be sure that if the order in the array is out, that it displays with the corresponding drive.
Example (note the different position of the Server_disk6 and Server_disk5 in $Drives
$Snapshots = 'Server1_disk1_Snapshot','Server_disk2_Snapshot','Server_disk3_Snapshot','Server_disk4_Snapshot','Server_disk5_Snapshot','Server_disk6_Snapshot' $Drives = 'Server1_disk1','Server_disk2','Server_disk3','Server_disk4','Server_disk6','Server_disk5' $h = @{} if ($Drives.Length -ne $Snapshots.Length) { Write-Error -Message "Array lengths do not match" ` -Category InvalidData ` } else { for ($i = 0; $i -lt $Drives.Length; $i++) { $h[$Drives[$i]] = $Snapshots[$i] } } Write-Output $h
I get output like
Name Value ---- ----- Server_disk4 Server_disk4_Snapshot Server_disk3 Server_disk3_Snapshot Server_disk6 Server_disk5_Snapshot Server_disk5 Server_disk6_Snapshot Server_disk2 Server_disk2_Snapshot Server1_disk1 Server1_disk1_Snapshot