我的代码到目前为止一个接一个地工作,但我需要它们并行运行:
这是我的代码。
$TIMESTAMP = Get-Date;
$TIMESTAMP = $TIMESTAMP.ToString("yyyy-MM-dd-07:00");
#Patterns
$DB_NAME_MIRROR = "mirroring_<X>(<Y>)";
$primarykey = $STORAGE_ACCOUNT_BACKUP_KEY;
#strdatabasebackup
$STORAGE_URI_BACKUP = "example/(<Y>)<X>.bacpac";
#Arrays
$DB_ARRAY = @("dbdom_mpinto","dbdom_penalolen","dbdom_coquimbo","dbdom_elquisco","dbdom_temuco","dbdom_imelbosque","dbdom_lareina","dbdom_buin");
#Replace timestamp
$DB_NAME_MIRROR = $DB_NAME_MIRROR.Replace('<Y>',$TIMESTAMP);
$STORAGE_URI_BACKUP = $STORAGE_URI_BACKUP.Replace('<Y>',$TIMESTAMP);
#Debugging....
#Write-Host $DB_NAME_MIRROR;
#Write-Host $STORAGE_URI_BACKUP;
#Database import
foreach ($DB in $DB_ARRAY) {
$DB_NAME_IMPORT = $DB_NAME_MIRROR.Replace('<X>',$DB);
$URL_FILE_BACKUP = $STORAGE_URI_BACKUP.Replace('<X>',$DB);
#Debugging
Write-Host $DB_NAME_IMPORT
Write-Host $URL_FILE_BACKUP
}
如何使用并行性执行代码
答案 0 :(得分:1)
有多种方法可以实现多线程/并行&#39;努力。
使用PowerShell进行并行处理
Start-Job -Name EventJob01 -ScriptBlock {Get-EventLog -Log system} -Credential domain01\user01
Workflow Some-TasksToRun
{
<Commands>
}
$rp = [runspacefactory]::CreateRunspace(1,5)
$ps = [powershell]::Create()
$ps.RunspacePool = $rp
...
&#39; blogs.technet.microsoft.com/uktechnet/2016/06/20/parallel-processing-with-powershell'
另见有关Runspaces的长篇讨论:
&#39; serverfault.com/questions/626711/how-do-i-run-my-powershell-scripts-in-parallel-without-using-jobs'