到目前为止,我的代码一个接一个地工作,但我需要它们并行运行:
这是我的代码。
$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
}
如何并行编写代码
有几种方法可以完成"多线程/并行"工作。
- 工作
- 工作流程
- 运行空间
使用 PowerShell 进行并行处理
Start-Job -Name EventJob01 -ScriptBlock {Get-EventLog -Log system} -Credential domain01user01
Workflow Some-TasksToRun
{
<Commands>
}
$rp = [runspacefactory]::CreateRunspace(1,5)
$ps = [powershell]::Create()
$ps.RunspacePool = $rp
...
"blogs.technet.microsoft.com/uktechnet/2016/06/20/parallel-processing-with-powershell">
另请参阅有关运行空间的长篇讨论:
"serverfault.com/questions/626711/how-do-i-run-my-powershell-scripts-in-parallel-without-using-jobs">