가격 및 제품 세부 정보를 수집하기 위해 사이트를 구문 분석하려고합니다. 스크립트는 루프에서 작동하지만 속도는 매우 느립니다. 그래서 다중 스레드 powershell 스크립트를 작업으로 실행하려고합니다.Powershell - foreach가 배열로 작업 (로컬/멀티 스레드)
나는 많은 추천을 시도했지만 나는 마지막 10을 선택하고있어
(웹 요청 화면가 점멸) 나는 그것의 작업을 볼 수 있지만 밖으로 결과를 얻기 위해 사투를 벌인거야 그러나 나는 나중에 조절판에 넣을 것이다. 그냥 출력 할 수 없습니다. 본질적으로 모든 결과가 $ arr로 다시 흐르고 싶습니다.
#Import Danmurphy Sitelist
[xml] $XmlDocument = (New-Object System.Net.WebClient).DownloadString("http://www.example.com/sites.xml")
#get websites listed
$ImportedProducts = $XmlDocument.DocumentElement.url | select -Last 10
"Killing existing jobs . . ."
Get-Job | Remove-Job -Force
"Done."
#loop through the products
#Create Array
$arr = @()
#$argumentlist
#ScriptBlock
$ScriptBlock = {
Param($product,$arr)
if ($product.loc -like "http://www.example.com/product/*"){
$uri = $product.loc
$WebResponse = Invoke-WebRequest -Uri $uri -SessionVariable WS
#mainpricetest
$mainprice = $WebResponse.AllElements | ? { $_.Class -eq 'price-main' } | select innerText
$MainPriceArray = $mainprice.innerText.Split(' ')
$MainUnitArry = $MainPriceArray[1..10]
$MainDollar = $MainPriceArray[0]
$MainUnit = $MainUnitArry -join ' '
$item = New-Object PSObject
$item | Add-Member -type NoteProperty -Name 'Product Site' -Value $($product.loc)
$item | Add-Member -type NoteProperty -Name 'Main Price' -Value $($MainDollar)
$item | Add-Member -type NoteProperty -Name 'Main Unit' -Value $($MainUnit)
$arr += $item
}
}
foreach ($product in $ImportedProducts){
Start-Job -InputObject $ImportedProducts -ScriptBlock $ScriptBlock -ArgumentList $product,$arr
}
$data = Get-Job * | Receive-Job
#Show Array
$arr
왜 '$ arr + = $ item'을 스크립트 블록에서 제거하고'$ data'를 사용하여 출력을 캡처합니까? – Matt