0

使用执行此脚本时Start-Job,没有问题。文件从源复制到目标,一次一个目录,但是花费的时间太长。所以我试图通过Start-Job.

该脚本执行没有错误,并且各个作业显示为已完成,但文件未复制到源。我怀疑这与$null传递给工作的变量有关。

我已经尝试了几个小时来让它工作,切换脚本,调整脚本块,标志等,但没有任何效果。

Param(
    [string]$env = 'test',
    [string]$directory = 'F:\Backup_Job'
)

# Set job variables
CD $directory
$servers = Import-Csv .\servers.csv
$query = Get-Content .\Get_Server.txt -Raw
$excludedFiles = Import-Csv .\excludedFiles.csv

$jobs = foreach ($server in $servers) {
    if ($server.env.Trim() -eq $env) {
        Write-Host "Starting copy job for $($server.servername)"
        Start-Job -Name "Copyjob $($server.servername)" -ScriptBlock {
            Param($server, $excludedFiles, $directory, $query)
            # Server and bak variables
            $sqlServer = $server.servername.Trim()
            $filepath = $server.baksource.Trim()

            # Job variables
            $bakserver = Invoke-Sqlcmd -Query $query -ServerInstance $sqlServer -Database 'master' -QueryTimeout 100
            $baksource = "\\$($bakserver.server)\$($filepath)"
            $bakdest = $server.bakdest.Trim()

            $continue = Test-Path -Path $baksource

            if ($continue) {
                robocopy $baksource $bakdest "*.bak" /XF $excludedFiles.filename /XO /NP /ETA /W:1 /B /XX /R:3
            }
        } -ArgumentList $server, $excludedFiles, $directory, $query
    }
}
4

0 回答 0