Looping each value from 2 tables

Welcome Forums General PowerShell Q&A Looping each value from 2 tables

This topic contains 5 replies, has 4 voices, and was last updated by

 
Participant
3 months, 3 weeks ago.

  • Author
    Posts
  • #109879

    Participant
    Points: 0
    Rank: Member

    Trying to run the dsfrbacklog, the sending members and receiving members are saved in variable. However on running the backlog report I need to enter 1 value from sending member to 1 value of receiving member. I am not sure how to loop this up.

    Example

    $sendingservers = (Get-DfsrConnection -DomainName $Domain | Where {$_.GroupName -eq $replicationgroup}).SourceComputerName

    $receivingservers = (Get-DfsrConnection -DomainName $Domain | Where {$_.GroupName -eq $replicationgroup}).DestinationComputerName

    foreach ($sendingserver in $sendingservers ) {

    # Get backlog count with dfsrdiag

    $backlogcount=Get-DfsrBacklog -GroupName $replicationgroup -FolderName  $replicationfolder -SourceComputerName $sendingserver -DestinationComputerName $receivingserver

     

    In the above, I can run this for each sending member, but then it target to whole list of receiving member, then been for each receiving member.

    So I am looking something like below or similar.

    foreach ($sendingserver in $sendingservers  and  $receivingserver in  $receivingservers)

    Any help will be greatly appreciated

  • #109888

    Participant
    Points: 319
    Helping Hand
    Rank: Contributor

    I have not DFS deployment to test this with, yet, there is no concept of doing this...

    foreach ($sendingserver in $sendingservers  and  $receivingserver in  $receivingservers)
    

    ...that I am aware of or ever tried.

    This is two separate ForLoops, nested. Which takes you down this path...

    $sendingservers = 'S1','S2','S3'
    $receivingservers = 'R1','R2','R3'
    
    foreach ($sendingserver in $sendingservers)
    {
        # other outloop action here
        'Processing Sending server list'
        $sendingserver 
    
        ForEach ($receivingserver in  $receivingservers)
        {
            # Inner loop actions here
            'Processing Receiving Server list'
            $sendingserver 
            $receivingserver 
    
        }
    
    }
    
    # Results
    
    Processing Sending server list
    S1
    Processing Receiving Server list
    S1
    R1
    Processing Receiving Server list
    S1
    R2
    Processing Receiving Server list
    S1
    R3
    Processing Sending server list
    S2
    Processing Receiving Server list
    S2
    R1
    Processing Receiving Server list
    S2
    R2
    Processing Receiving Server list
    S2
    R3
    Processing Sending server list
    S3
    Processing Receiving Server list
    S3
    R1
    Processing Receiving Server list
    S3
    R2
    Processing Receiving Server list
    S3
    R3
    

    … but are you sure you wan this, this way?

    Can you show an end result that you are expecting?

    • #109957

      Participant
      Points: 0
      Rank: Member

      The result I am expecting as below:-

      $sendingservers = 'S1','S2','S3'
      $receivingservers = 'R1','R2','R3'

      Processing Receiving Server list
      S1
      R1

      Processing Receiving Server list
      S2
      R2

      Processing Receiving Server list
      S3
      R3

  • #109958

    Participant
    Points: 878
    Helping Hand
    Rank: Major Contributor

    That means you have mapping between Receiving and Sending Servers.

    You can have a hashtable with this mapping. Here mapping is done with index.

    [Array]$SendingServers   = (Get-DfsrConnection -DomainName $Domain | Where-Object -FilterScript  {$_.GroupName -eq $replicationgroup}).SourceComputerName
    [Array]$ReceivingServers = (Get-DfsrConnection -DomainName $Domain | Where-Object -FilterScript {$_.GroupName -eq $replicationgroup}).DestinationComputerName
    $C = 0
    
    $MappedHashTable = $SendingServers | Foreach-Object -Process {
                            @{ $_ = $ReceivingServers[$C] }
                            $C++
                        }
    
    foreach ($SendingServer in $SendingServers ) {
        $BacklogCount = Get-DfsrBacklog -GroupName $replicationgroup -FolderName  $replicationfolder -SourceComputerName $sendingserver -DestinationComputerName $MappedHashTable[$sendingserver]
    }
    
  • #109964

    Participant
    Points: 0
    Rank: Member

    Thanks kvprsoon, though hasttable didnt helped, but the array thing help me out what exactly i need to do.

    
    Foreach ($Domain in $domains) {
    
     
    
    # Get all replication groups
    
    $replicationgroups= (Get-DfsReplicatedFolder -DomainName $Domain).GroupName
    
     
    
    # Loop through each replication group
    
    foreach ($replicationgroup in $replicationgroups) {
    
     
    
    # Get servers for the current replication group
    
    $replicationfolder = (Get-DfsReplicatedFolder -DomainName $Domain | Where {$_.GroupName -eq $replicationgroup}).FolderName
    
    [Array]$SendingServers   = (Get-DfsrConnection -DomainName $Domain | Where-Object -FilterScript  {$_.GroupName -eq $replicationgroup}).SourceComputerName
    
    [Array]$ReceivingServers = (Get-DfsrConnection -DomainName $Domain | Where-Object -FilterScript {$_.GroupName -eq $replicationgroup}).DestinationComputerName
    
     
    
    $C = 0
    
    # Loop through each replication member server
    
    foreach ($sendingserver in $sendingservers ) {
    
    $receivingserver= $ReceivingServers[$C]
    
    $C++
    
    foreach ($SendingServer in $SendingServers ) {
    
    $BacklogCount = Get-DfsrBacklog -GroupName $replicationgroup -FolderName  $replicationfolder -SourceComputerName $sendingserver -DestinationComputerName $receivingserver
    
    }
    
    
  • #109979

    Participant
    Points: 324
    Helping Hand
    Rank: Contributor

    If you are processing using an index, this is a more typical approach utilizing a for loop:

    $sendingservers = 'S1','S2','S3'
    $receivingservers = 'R1','R2','R3'
    
    for ($i = 0;$i -le ($sendingservers.Count -1);$i++) {
        $sendingservers[$i]
        $receivingservers[$i]
    }
    
    

The topic ‘Looping each value from 2 tables’ is closed to new replies.