Hi
$array1 = @("1", "2", "3")
$array2 = @("D:\folder1", "D:\folder2", "D:\folder3")
foreach ($e in $array1) {
$QMGR = "QM.$e"
$server = Get-ChildItem -Path $f | Select-String -Pattern 'Server' |%{ $_ -replace '\s+$', '' }
}
$e belongs to array1
$f belongs to array2
I want to run loop where first value in array1 works parallelly with first value in array2 , second value in array1 works parallelly with second value in array2 and so on .
for example , the first loop will be
$QMGR = “QM.1”
$server = Get-ChildItem -Path D:\folder1 | Select-String -Pattern ‘Server’ |%{ $_ -replace ‘\s+$’, ‘’ }
Hashtable will help you here. An example below.
$Collection = @{
First = "FirstItem"
Second = "SecondItem"
Third = "ThirdItem"
}
$Collection.GetEnumerator() | Foreach-Object -Process { "$($_.Key) and $($_.Value)" }
Well, have you considered a multidimensional array vs two separate ones?
$MDArray = @(
('1', '2', '3'),
('D:\folder1', 'D:\folder2', 'D:\folder3')
)
$ArraryElement = -1
ForEach ($Item in $MDArray[0])
{
($ArraryElement += 1) # the parens is called variable squeezing, which
# assigns that resutls to a variable and outputs to
# the screen. Remove them if you don't want the output
$MDArray[1][$ArraryElement]
}
# Results
0
D:\folder1
1
D:\folder2
2
D:\folder3
# Without squeezing
$MDArray = @(
('1', '2', '3'),
('D:\folder1', 'D:\folder2', 'D:\folder3')
)
$ArraryElement = -1
ForEach ($Item in $MDArray[0])
{
$ArraryElement += 1
$MDArray[1][$ArraryElement]
}
# Results
D:\folder1
D:\folder2
D:\folder3
# Real folders
$MDArray = @(
('1', '2', '3'),
('D:\temp', 'E:\temp', 'F:\temp')
)
$ArraryElement = -1
ForEach ($Item in $MDArray[0])
{
$ArraryElement += 1
Get-ChildItem -Path $MDArray[1][$ArraryElement]
}
# Results
Directory: D:\temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2/20/2019 3:26 PM abcpath0
...
Directory: E:\temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 4/19/2019 12:40 AM Folder
...
Directory: F:\temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 12/7/2017 12:40 PM Duplicates
...
js2010
April 21, 2019, 10:43pm
4
How about a for loop? The -replace had a typo.
$array1 = 1..3
$array2 = echo D:\folder1 D:\folder2 D:\folder3
for ($i = 0; $i -lt $array1.count; $i++) {
$e = $array1[$i]; $f = $array2[$i]
$QMGR = "QM.$e"
$server = (Get-ChildItem $f | Select-String Server).trim()
}
Thanks all
I used js solution which is working .