Powershell - creating DFS

sembi 21 Reputation points
2020-10-28T17:58:36.1+00:00

Hello,

please is there any options how to optimize this script? I have about 1340 folders which needs to be replicated. Thanks

$C = Import-Csv -Path "C:\temp\DFS_create.csv"

foreach ($bb in $C){

New-DfsnFolderTarget -Path "$($bb.dft)" -TargetPath "$($bb.tp)" -ReferralPriorityClass SiteCostNormal -State Offline
New-DfsReplicationGroup -GroupName "$($bb.conn)" | New-DfsReplicatedFolder -FolderName "$($bb.fl)" -DfsnPath $($bb.dft) | Add-DfsrMember -ComputerName "serverA","serverB" | Format-Table dnsname,groupname -auto -wrap
Add-DfsrConnection -GroupName "$($bb.conn)" -SourceComputerName "serverA" -DestinationComputerName "serverB" | Format-Table *name -wrap -auto
Set-DfsrMembership -GroupName "$($bb.conn)" -FolderName "$($bb.fl)" -ContentPath $($bb.pth) -ComputerName "serverA" -PrimaryMember $true -StagingPathQuotaInMB 20480 -Force | Format-Table *name,path,primary -auto -wrap
Set-DfsrMembership -GroupName "$($bb.conn)" -FolderName "$($bb.fl)" -ContentPath $($bb.pth) -ComputerName "serverB" -StagingPathQuotaInMB 20480 -Force | Format-Table *name,path,primary -auto -wrap
}

acctualy script is working, but it take about 3hours until it create one folder replication and just after that it will move to next line.

thanks
sembi

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
{count} votes

Accepted answer
  1. Rich Matheisen 44,776 Reputation points
    2020-10-29T19:11:10.72+00:00

    You obviously didn't read all of my last comment. Did you do a Receive-Job to check the output of each job?

    You don't need all those extra quotation marks, "$", and parentheses, either. They just clutter up your code and by using them you may get some unexpected results.

    A naïve way of doing the check for a job's output is something like this:

    foreach ($bb in $C){
        Start-Job -ScriptBlock {
            #
            # NOTE: You do no error handling!
            # Use Try/Catch and add -ErrorAction Stop to cmdlets if you want to do that
            #
            New-DfsnFolderTarget -Path $bb.dft -TargetPath $bb.tp -ReferralPriorityClass SiteCostNormal -State Offline
            New-DfsReplicationGroup -GroupName $bb.conn |
                New-DfsReplicatedFolder -FolderName $bb.fl -DfsnPath bb.dft |
                    Add-DfsrMember -ComputerName "serverA","serverB" |
                        Format-Table dnsname,groupname -auto -wrap
            Add-DfsrConnection -GroupName $bb.conn -SourceComputerName "serverA" -DestinationComputerName "serverB" |
                Format-Table *name -wrap -auto
            Set-DfsrMembership -GroupName $bb.conn -FolderName $bb.fl -ContentPath $bb.pth -ComputerName "serverA" -PrimaryMember $true -StagingPathQuotaInMB 20480 -Force |
                Format-Table *name,*path,primary* -auto -wrap
            Set-DfsrMembership -GroupName $bb.conn -FolderName $bb.fl -ContentPath $bb.pth -ComputerName "serverB" -StagingPathQuotaInMB 20480 -Force |
                Format-Table *name,*path,primary* -auto -wrap
            }
        }
        Get-Job | Wait-Job      # waits for all jobs to end
        Get-Job | Receive-Job   # You won't see any errors unless you look at the childjob in any job with the failed state
    

    You'll find much better examples of how to handle the "getting" and "receiving" of jobs in many blogs found by searching for something as simple as "Start-Job" in a search engine.


3 additional answers

Sort by: Most helpful
  1. Rich Matheisen 44,776 Reputation points
    2020-10-28T19:02:43.027+00:00

    You could create each replication as a job (Start-Job) within the Foreach-Object and outside the loop wait for all the jobs to complete. The existing code in the loop would be placed in a script block to be run by each job.

    Whether that will make things go faster depends on the workload each job generates, so be careful to not start too many jobs at once.


  2. sembi 21 Reputation points
    2020-10-29T16:05:08.387+00:00

    hello @Rich Matheisen
    I checked dfs management and no replication group was created -> so i chcecked it also here.

    here is the code:
    $C = Import-Csv -Path "C:\temp\DFS_create3.csv"

    foreach ($bb in $C){  
    	Start-Job -ScriptBlock {  
    		New-DfsnFolderTarget -Path "$($bb.dft)" -TargetPath "$($bb.tp)" -ReferralPriorityClass SiteCostNormal -State Offline  
    		New-DfsReplicationGroup -GroupName "$($bb.conn)" | New-DfsReplicatedFolder -FolderName "$($bb.fl)" -DfsnPath $($bb.dft) | Add-DfsrMember -ComputerName "serverA","serverB" | Format-Table dnsname,groupname -auto -wrap  
    		Add-DfsrConnection -GroupName "$($bb.conn)" -SourceComputerName "serverA" -DestinationComputerName "serverB" | Format-Table *name -wrap -auto  
    		Set-DfsrMembership -GroupName "$($bb.conn)" -FolderName "$($bb.fl)" -ContentPath $($bb.pth) -ComputerName "serverA" -PrimaryMember $true -StagingPathQuotaInMB 20480 -Force | Format-Table *name,*path,primary* -auto -wrap  
    		Set-DfsrMembership -GroupName "$($bb.conn)" -FolderName "$($bb.fl)" -ContentPath $($bb.pth) -ComputerName "serverB" -StagingPathQuotaInMB 20480 -Force | Format-Table *name,*path,primary* -auto -wrap  
    		}  
    	}  
    
    0 comments No comments

  3. Kiran Ayare 1 Reputation point
    2021-11-04T21:27:27.31+00:00

    Hi,

    Want to add multiple new dfs folder target path. below script is working for adding a single target and disable path.

    New-DfsnFolderTarget -Path "$($bb.dft)" -TargetPath "$($bb.tp)" -ReferralPriorityClass SiteCostNormal -State Offline

    Need help for adding multiple target path in one go using .csv file.

    Please let me know how I can complete task. Am not expert in scripting.

    IF I set first line as $C = Import-Csv -Path "C:\temp\DFS_create3.csv" than what I need to pu in front of -Path and -TargetPath (i.e. Value)

    0 comments No comments