scripting: keeping 5 restore sessions to restore 5 folders at a time, and start additions if some session are completed until all folders are restored

pwshellmagic 1 Reputation point
2022-11-18T21:41:02.137+00:00

There are about hundreds of first level of folders under a drive, with large amount of data in total. I can have a file containing all hundreds of first level folders that need to be restored.

We can run maximum of 5 restore commands at any single time, and each one is to restore a single folder, for instance, "restore -options c:\folder1*", ... "restore -options c:\folder5*". Also, when any of these 5 commands are finished, we need to grab same number of finished folders from the file containing all folders, and start more such restore commands. So, the script should be also able to constantly check how many sessions/commands are running. In unix, I can do "ps -ef | grep restore | wc -l" to check if there are 5 restore commands running.

Can you please help me to script this function? Thank you!
I am from Unix background, and just starting to learn PowerShell.

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,386 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Limitless Technology 43,966 Reputation points
    2022-11-23T15:59:21.903+00:00

    Hi,

    Thank you for posting your query.

    Kindly check the details below to resolve your issue.

    I found an article that restores files and folders that DFS Replication previously preserved.

    Go to this link for your reference https://learn.microsoft.com/en-us/powershell/module/dfsr/restore-dfsrpreservedfiles

    ----------------------------------------------------------------------------------------------------------------------

    If the answer is helpful kindly click "Accept as Answer" and up vote it.

    0 comments No comments

  2. Rich Matheisen 45,096 Reputation points
    2022-11-23T22:32:27.43+00:00

    Something like this may be what you need:

    $maxConcurrentJobs = 5  
    foreach($Object in $Objects) {  
        if ((Get-Job -State 'Running').Count -lt $maxConcurrentJobs) {  
            $ScriptBlock = {  
                #Insert the code of your workload here  
            }  
        }  
        else{  
            while ( (Get-Job -State 'Running').Count -lt $maxConcurrentJobs){  
                Start-Sleep -Seconds 5  # pick a suitable time to wait based on typical job duration  
            }  
            # when a running job ends, for whatever reason, start another  
            Start-Job -ScriptBlock $ScriptBlock = {  
                #Insert the code of your workload here  
            }  
        }  
    }  
    
    0 comments No comments