question

sembi avatar image
1 Vote"
sembi asked KiranAyare-9836 answered

Powershell - creating DFS

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
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered sembi edited

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello @RichMatheisen-8856 ,

thank you, i didn't see double $.
I thought this forum is for sharing experience and knowledge, but ok :) search engine is also answer.

Anyway, thank you for your time.

Sembi

0 Votes 0 ·

It is, but it doesn't mean that you get someone to do all the work for you! If you have a problem go ahead an post your code (or the part of the code that someone can try and reproduce your problem). But I see no reason to provide you with something you can easily find for yourself.

Also, in your code you don't limit the number of jobs you start. If you think that you'll be able to start 1300+ jobs and have them run concurrently you really have another think coming! You'll find ways of managing the concurrency with simple searches like "powershell start -job throttle limit". There's no reason for someone to find them for you and post a half dozen lines of code here.

1 Vote 1 ·
sembi avatar image sembi RichMatheisen-8856 ·

Sorry, I post my question because I was not able to find problem...

Anyway, thank you for your comment and directions.

0 Votes 0 ·
RichMatheisen-8856 avatar image
1 Vote"
RichMatheisen-8856 answered RichMatheisen-8856 commented

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.

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

hi @RichMatheisen-8856

you mean

foreach (){
start-job -scriptblock{

xxxxx
xxxx

}}

i think i already tried it, and it was not working, but i should try it again if syntax is correct.


1 Vote 1 ·

Yes. You'll have to pass in the appropriate parameters to the script block., of course. And don't forget to limit the number of running jobs to something your system and network can manage.

1 Vote 1 ·
sembi avatar image sembi RichMatheisen-8856 ·

Looks something is not good,

check this:
Id Name PSJobTypeName State HasMoreData Location Command


53 Job53 BackgroundJob Running True localhost ...
55 Job55 BackgroundJob Running True localhost ...

PS C:\Windows\system32> Get-Job

Id Name PSJobTypeName State HasMoreData Location Command


53 Job53 BackgroundJob Completed True
55 Job55 BackgroundJob Completed True

its completed after few seconds, but nothing happened.

1 Vote 1 ·
Show more comments
sembi avatar image
0 Votes"
sembi answered

hello @RichMatheisen-8856
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
         }
     }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

KiranAyare-9836 avatar image
0 Votes"
KiranAyare-9836 answered

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)

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.