Transfer the Directory to Multiple server using BITS transfer

Hi All,

Below script transfer the data one after another complete the Job. i need to run the job parallels on multiple server
how to run the jobs for multiple servers parallely?

param (
    #[string]$SourceFolderPath,     # Source folder path (e.g., C:\path\to\folder)
    [string[]]$DestinationServers  # Array of destination server names (e.g., "ServerB", "ServerC")
)

function transfer{

param (
    #[string]$SourceFolderPath,     # Source folder path (e.g., C:\path\to\folder)
    [string]$DestinationServers  # Array of destination server names (e.g., "ServerB", "ServerC")
     )
    Import-Module BitsTransfer
    $Source="F:\Infrastructure\ARGONInfra\SCCM2012\WS2019\Packages\HyperV2019 Installer 2212\Binaries"
    $Destination="\\$DestinationServers\p$\HyperV2019_Installer_2212"
        if ( -Not (Test-Path $Destination))
            {
                $null = New-Item -Path $Destination -ItemType Directory
            }
        $folders = Get-ChildItem -Name -Path $source -Directory -Recurse
            foreach ($i in $folders)
            {
               $exists = Test-Path $Destination\$i
                if ($exists -eq $false)
                 {
                 New-Item $Destination\$i -ItemType Directory
                 }
                    $Job = Start-BitsTransfer -DisplayName $DestinationServers -Source $Source\$i\*.* -Destination $Destination\$i -asynchronous -Priority low
                    return $Job
                   
            }
}

# Create an array to store the BITS transfer jobs
$BitsJobs = @()

# Loop through each destination server and initiate folder transfer as a job
foreach ($server in $DestinationServers) {
    $bitsJob = transfer -DestinationServer $server
    $BitsJobs += $bitsJob
}

# Wait for all BITS transfer jobs to complete
Write-Host "Waiting for BITS transfer jobs to complete... $server"
$BitsJobs | ForEach-Object {
    do {
        Write-Host "Job State for $($_.JobDisplayName): $($_.JobState)"
        Start-Sleep -Seconds 5
        $_ = Get-BitsTransfer -JobId $BitsJobs.JobId
    } while ($_.JobState -eq "Transferring" -or $_.JobState -eq "Connecting")
}

Write-Host "All BITS transfer jobs completed."

Thanks
Manikandan.K

You forgot to ask a question! :wink:

Already asked the question

You shared some code and you told what you need. That’s not a question. What’s not working with the code you have?

I’ve looked this over closely and I can’t find a question mark. Please clarify the question.

Need to be modify the script to run on multiple computers at the same time

Do you expect us to modify it for you?

Doesn’t this work?

You have to tell us what’s wrong with the code or what’s not working as you expect it.

Using the array parameter, script was executing and file transfer happening on one another one server. File transfer job need to be run multiple server at the same time.

I tried to reproduce the behaviour you showed and I can confirm it’s the same on my test setup. The transfers are done in sequence. But it seems like this is a limitation of BITS - not PowerShell.

If you need to copy the files at the same time to different destinations BITS may not be the right tool for the job. :man_shrugging:t3: