PowerShell 스크립트에서 Ping 로그를 차트 데이터로 변환하려고합니다.Powershell에서 느린 배열 작업
스크립트는 정상적으로 작동하지만 배열 작업으로 인해 매우 느리게 실행됩니다.
스크립트가 10k 회선 파일에서 실행되면 약 7 초가 소요됩니다. 배열 작업이 제거되면 완료하는 데 걸리는 시간이 그보다 짧습니다.
임시 배열을 사용하지 않고 호출자 함수로 데이터를 반환 할 대안을 찾고 있습니다. 입력 로그의
예 :
02.01.2017-14:53:54> Reply from 8.8.8.8: bytes=32 time=184ms TTL=57
02.01.2017-14:53:54> Reply from 8.8.8.8: bytes=32 time=18ms TTL=57
02.01.2017-14:53:59> Request timed out.
02.01.2017-14:54:01> Reply from 192.168.2.186: Destination host unreachable.
02.01.2017-14:54:05> Request timed out.
02.01.2017-14:54:07> Reply from 192.168.2.186: Destination host unreachable.
스크립트 : 사실
function Convert-V4PingLog2ChartData
{
param($V4PingLogFile, $AvarageRespondTime, $ChartCounter)
$ConvertedData=""
$var=Get-Content $V4PingLogFile
$varArray=$var.split("`n")
$varArray=$varArray | Select-Object -Skip 2
$CommandExecuteTime=Measure-Command{
$pattern = "^([0-9]{2})\.([0-9]{2})\.([0-9]{4})-([0-9]{2}):([0-9]{2}):([0-9]{2})> Reply from [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}: bytes=32 time=([0-9]{1,4})ms TTL=[0-9]{1,3}$";
$pattern2="^([0-9]{2})\.([0-9]{2})\.([0-9]{4})-([0-9]{2}):([0-9]{2}):([0-9]{2})> (Request timed out.|Reply from [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}: Destination host unreachable.)$"
foreach($nextLine in $varArray)
{
if($nextLine -like "* time=*")
{
$ConvertedData+=$nextLine -replace $pattern, "data$ChartCounter.addRow([new Date(`$3, `$2, `$1, `$4, `$5, `$6, 00), `$7, $AvarageRespondTime]);"
}
else
{
$ConvertedData+=$nextLine -replace $pattern2, "data$ChartCounter.addRow([new Date(`$3, `$2, `$1, `$4, `$5, `$6, 00), 0, $AvarageRespondTime]);"
}
}
}
Write-Host $CommandExecuteTime
return $ConvertedData
}
고마워요, 완벽하게 작동합니다. – Krisz
좋은 답변입니다. Ansgar :) –