2016-11-15 67 views
1

我有一個PowerShell腳本,它會從服務器獲取配置文件並解析一些數據,然後ping設備以查看它們是聯機還是脫機。它按預期工作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

花費時間最長的部分是什麼? AD查詢還是Ping? – 4c74356b41

+2

有多少個設備? 'Test-Connection'是同步的,不能讓你控制超時。 – briantist

+0

我建議你在可能導致問題的地方放一些回聲,例如看它循環迭代的次數。 – briansrls

回答

3

所以我相信你是由於坪面臨的問題,我不認爲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

相關問題