0
我有权访问单核处理器的单处理器虚拟机,并为其执行日志记录。我有以下代码:单处理器系统上的多线程powershell脚本
$sb = {
Param($_)
if($_.CONTROLLER -ne ".xx"){
$posIP = "10." + $_.IP + $_.CONTROLLER
if (Test-Connection -ComputerName $posIP -Count 1 -Quiet) {
$mapPath = "\\" + $posIP + "\c$"
net use $mapPath $password /user:$userName | Out-Null
if(Test-Path $mapPath$dataFile) {
[xml]$periods = Get-Content $mapPath$dataFile
$endDate = $periods.IndataDbf.ingredient.PeriodDetail.PeriodEndDate | select -last 1
$output = "$($_.STORE);$endDate" }
else {
$outPut = $_.STORE + ';' + "$dataFile Not Found" }
net use $mapPath /de | Out-Null
}
else {
$outPut = $_.STORE + ';' + "Map FAILED" }
Write-Output $OutPut
}
}
Import-Csv $inFile | ForEach-Object {
while ((Get-Job -State Running).Count -ge 100) {
Start-Sleep -Seconds 5;
}
Write-Output $_.STORE
Start-Job -Scriptblock $sb -ArgumentList $_ | Write-Verbose
Get-Job -State Completed -HasMoreData 1 | Receive-Job | Out-File -Append -FilePath $outLog
}
Get-Job | Wait-Job | Receive-Job | Out-File -Append -FilePath $outLog
其中运行良好,但花费的时间是相同数量的运行没有Start-Job
相同的代码,只是一个循环。但是,之前的日志记录命令使用了BATCH文件,并自动打开了几十个子命令窗口来处理数据,然后返回,并在一半的时间内运行。使用的代码是相同的,所以我不明白为什么添加更多线程不会使脚本运行得更快。任何人都可以告诉我为什么一个BATCH文件程序与几十个子窗口运行速度很快,可以说是相同的代码?为什么Start-Job
命令根本不能提高速度?我认为它会尝试同时执行多个线程。
我必须在家看看,在某些时候。我的雇主阻止了Box,所以我无法下载代码来查看它是如何实现的,而且帖子没有完整列出代码。 –