2016-11-15 8 views
1

サーバーから設定ファイルを取得してデータを解析し、デバイスにpingを実行してオンラインかオフラインかを確認するPowerShellスクリプトがあります。それは100%期待どおりに動作しますが、実行には8時間以上かかります。私は、これが起こる原因となっている何かが私の論理の中にあるかどうか疑問に思っています。私はそれが他の問題に関連する可能性があることを認識していますが、私はこれを排除したかったのです。誰かがロジックに長い時間がかかりそうなフォールトを見ることができますか?つまり、ある種のループなどに詰まっている場合です。ここでPowerShellスクリプトは完璧に動作しますが、実行するには8時間以上かかります

が私のコードです:

$logfile = "D:\Logs\MPOSPrinterPingLog.txt" 
$offlineprinters = "D:\Reports\MPOS\MPOSOfflinePrinters.txt" 
if (Test-Path $logfile) {Remove-Item $logfile} 
if (Test-Path $offlineprinters) {Remove-Item $offlineprinters} 

Add-Content $logfile "Setting local path" 
$localPath = "C:\Temp\MPOS" 

Add-Content $logfile "Gathering server list" 
$serverList = (Get-ADComputer -Filter "Name -like 'Q0*P30' -or Name -like 'Q0*P32'" -SearchBase "OU=Print,OU=Prod,OU=POS,DC=COMPANY,DC=NET").name | Sort-Object | Out-File C:\Temp\MPOS\MPOSPrintServers.txt 
$serverListPath = "C:\Temp\MPOS\MPOSPrintServers.txt" 

#Retrieve a list of MPOS Print servers from text file and set to $serverNames 
Add-Content $logfile "Compiling text file" 
$serverNames = Get-Content -Path $serverListPath 

#Iterate through each of the server names 
foreach ($serverName in $serverNames) { 
    Add-Content $logfile "Processing $serverName" 

    #Check if the server is online before doing the remote command 
    if (Test-Connection -ComputerName $serverName -Quiet -count 1) { 
     Add-Content $logfile "$serverName is online" 

     #copy config file from MPOS print to local server for processing 
     $timestamp1 = (Get-Date -Format g) 
     Add-Content $logfile "$timestamp1 - Copying xml file from server to local path" 
     $configPath = "\\$($serverName)\C$\ProgramData\Microsoft\Point Of Service\Configuration\Configuration.xml" 
     Copy-Item $configPath $localPath 

     #process xml file to parse IP addresses for ping 
     $timestamp2 = (Get-Date -Format g) 
     Add-Content $logfile "$timestamp2 - Processing xml file from $serverName to parse data to csv" 
     $xml = [xml](Get-Content C:\Temp\MPOS\Configuration.xml) 
     $PrinterNames = $xml.selectNodes('//PointOfServiceConfig/ServiceObject/Device') | foreach {New-Object -TypeName psobject -Property @{LogicalName=$_.LogicalName.Name}} 
     $PrinterIPs = $xml.selectNodes('//PointOfServiceConfig/ServiceObject/Device') | foreach {New-Object -TypeName psobject -Property @{HardwarePath=$_.HardwarePath}} 

     foreach ($PrinterIP in $PrinterIPs) { 
      $pingableIP = $PrinterIP.HardwarePath 

      if (Test-Connection $pingableIP -Quiet -Count 1) { 
       $timestamp3 = (Get-Date -Format g) 
       Add-Content $logfile "$timestamp3 - $serverName, $pingableIP is online and pingable" 
      } else { 
       $timestamp3 = (Get-Date -Format g) 
       Add-Content $offlineprinters "$timestamp3 - $serverName, $pingableIP is offline!" 
      } 
     } 
    } else { 
     Add-Content $logfile "$serverName is offline!" 
    } 
} 
+0

最も長く取っている部分は何ですか?広告クエリまたはping? – 4c74356b41

+2

いくつのデバイスがありますか? 'Test-Connection'は同期的で、タイムアウトを制御することはできません。 – briantist

+0

問題を引き起こしているかもしれない場所にエコーを置くことをお勧めします。ループを繰り返し何回繰り返すかを見てください。 – briansrls

答えて

3

だから私はあなたが原因pingに問題に直面していることを信じて、私はあなたが何をすべきかので、ADのクエリは、ほとんどの時間がかかるとは思わない - 平行であり、 ping。 PoshRSJobは始めるのに最適な場所です。

foreach ($serverName in $serverNames) { 
    start-rsjob -Name $_ -ScriptBlock { 
     # your code in the loop goes here # 
     } 
}    
Get-RSjob | Receive-RSJob 

またはあなたは自分自身の実行空間のラッパーを書くことができ;) 私はパラレル実行空間を実現した後、あなたが興味を持っている場合には、私は、それが完了するまでに6~7分のように取っていた、テキストファイルを解析するスクリプトを持っていたがそれは5ミリ秒で完了しました。どのように狂っている? xD

+2

私はこれも問題だと思うし、PoshRSJobは素晴らしいモジュールです。 – briantist

+0

私はこれを試します、あなたの援助とあなたの親切のために非常に感謝します! – LilithGoddess

+0

あなたは大歓迎です。 – 4c74356b41

関連する問題