Advertisement
Guest User

Powershell - Parsing times from windowsupdate log

a guest
Oct 21st, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. param(
  3.     $pathbefore = 'C:\$Windows.~BT\Sources\Panther\setupact.log',
  4.     $pathafter = "C:\WINDOWS\Panther\setupact.log",
  5.     $dl = '[/PreDownload /Package /Quiet',
  6.     $dlcomplete = "/install",
  7.     $inst = '[/install',
  8.     $reboot = 'Action Progress: [100%]',
  9.     $finished = 'SetupPlatform: Phase "OOBEBoot" completed successfully.'
  10.     )
  11. $header = "Date","x","Messagetype","Modul","Message"
  12.  
  13. #DL startet = ok
  14. #DL finish = no pattern found except installation is starting
  15. #IS startet = ok
  16. #first reboot =
  17. #inst finish = ok
  18.  
  19. ### LOGFILE NACH UPDATE ABFRAGEN
  20. #DOWNLOAD STARTED
  21. #TEST IF WE ARE BERFORE OR AFTER THE UPDATE AND PARSE CORRECT LOGFILE
  22. if (Test-Path $pathbefore){
  23.     #Parse Logfile from $pathbefore, replace the first 3x spaces with comma and look for pattern $dl, because of special character we use -SimpleMatch, we convert as csv to get separate attributes with own header and we want only the first value retunred
  24.     $time1 = (get-content $pathbefore) -replace '^(\S+?) (\S+?) (\S+?) (.*)','$1 $2,$3,$4' | select-string -pattern $dl -SimpleMatch –allmatches | Convertfrom-csv -Delimiter "," -header $header | Select-Object -First 1
  25.     #Separating the resulting line into different parts by spaces (from 1 to all spaces between) and skipping the first value containing path, filename, linenumber and date
  26.     if($time1){$time1a = Get-Date -Date $time1.date}
  27.     #Returns found time from logfile and removes the following comma
  28.     Write-Host -ForegroundColor Gray "Download was started at $time1a"
  29.     }
  30. else{
  31.     $time1 = (get-content $pathafter) -replace '^(\S+?) (\S+?) (\S+?) (.*)','$1 $2,$3,$4' | select-string -pattern $dl -SimpleMatch –allmatches | Convertfrom-csv -Delimiter "," -header $header | Select-Object -First 1
  32.     if($time1){$time1a = Get-Date -Date $time1.date}
  33.     Write-Host -ForegroundColor Gray "Download was started at $time1a"
  34.     }
  35.  
  36.  
  37.  
  38.  
  39. #DOWNLOAD FINISHED
  40. if (Test-Path $pathbefore){
  41.     $time2 = (get-content $pathbefore) -replace '^(\S+?) (\S+?) (\S+?) (.*)','$1 $2,$3,$4' | select-string -pattern $dlcomplete -SimpleMatch –allmatches | Convertfrom-csv -Delimiter "," -header $header | Select-Object -First 1
  42.     $time2a = Get-Date -Date $time2.date
  43.     Write-Host -ForegroundColor Gray "Download was completed at $time2a"
  44.     }
  45. else{
  46.     $time2 = (get-content $pathafter) -replace '^(\S+?) (\S+?) (\S+?) (.*)','$1 $2,$3,$4' | select-string -pattern $dlcomplete -SimpleMatch –allmatches | Convertfrom-csv -Delimiter "," -header $header | Select-Object -First 1
  47.     $time2a = Get-Date -Date $time2.date
  48.     Write-Host -ForegroundColor Gray "Download was completed at $time2a"
  49.     }
  50.    
  51.  
  52. #INSTALLATIONS STARTED
  53. if (Test-Path $pathbefore){
  54.     $time3 = (get-content $pathbefore) -replace '^(\S+?) (\S+?) (\S+?) (.*)','$1 $2,$3,$4' | select-string -pattern $inst -SimpleMatch –allmatches | Convertfrom-csv -Delimiter "," -header $header | Select-Object -First 1
  55.     $time3a = Get-Date -Date $time3.date
  56.     Write-Host -ForegroundColor Cyan "Installation was startet at $time3a"
  57.     }
  58. else{
  59.     $time3 = (get-content $pathafter) -replace '^(\S+?) (\S+?) (\S+?) (.*)','$1 $2,$3,$4' | select-string -pattern $inst -SimpleMatch –allmatches | Convertfrom-csv -Delimiter "," -header $header | Select-Object -First 1
  60.     $time3a = Get-Date -Date $time3.date
  61.     Write-Host -ForegroundColor Cyan "Installation was startet at $time3a"
  62.     }
  63.  
  64. #FIRST REBOOT
  65. if (Test-Path $pathbefore){
  66.     $time4 = (get-content $pathbefore) -replace '^(\S+?) (\S+?) (\S+?) (.*)','$1 $2,$3,$4' | select-string -pattern $reboot -SimpleMatch –allmatches | Convertfrom-csv -Delimiter "," -header $header | Select-Object -Last 1
  67.     $time4a = Get-Date -Date $time4.date
  68.     Write-Host -ForegroundColor Cyan "First reboot was initiated at $time4a"
  69.     }
  70. else{
  71.     $time4 = (get-content $pathafter) -replace '^(\S+?) (\S+?) (\S+?) (.*)','$1 $2,$3,$4' | select-string -pattern $reboot -SimpleMatch –allmatches | Convertfrom-csv -Delimiter "," -header $header | Select-Object -Last 1
  72.     $time4a = Get-Date -Date $time4.date
  73.     Write-Host -ForegroundColor Cyan "First reboot was initiated at $time4a"
  74.     }
  75.  
  76. #INSTALLATION FINISHED, LOGFILE LOCATION BEFORE IS NOT NEEDED ANYMORE
  77. $time5 = (get-content $pathafter) -replace '^(\S+?) (\S+?) (\S+?) (.*)','$1 $2,$3,$4' | select-string -pattern $finished -SimpleMatch –allmatches | Convertfrom-csv -Delimiter "," -header $header | Select-Object -Last 1
  78. $time5a = Get-Date -Date $time5.date
  79. Write-Host -ForegroundColor Green "Installation was finished at $time5a"
  80.  
  81. #SPACES BETWEEN TiMES AND CALCULTIONS
  82. Write-Host " "
  83. Write-Host " "
  84.  
  85.  
  86.  
  87. #CAlCULATE TIMES
  88. #CALCULATE DOWNLOADTIME
  89. # IF time1a has a value, calculate time and report to user
  90. if ($time1a){
  91.     $download = $time2a - $time1a
  92.     Write-Host -ForegroundColor Gray "Download =" $download
  93.     }
  94. else
  95.     {
  96.     Write-Host -ForegroundColor Gray "No download detected"
  97.     }
  98.  
  99. #CALCUALTE INSTALLATIONTIME
  100. $installation = $time5a - $time3a
  101. Write-Host -ForegroundColor Cyan "Installation =" $installation | select hours, minutes
  102.  
  103.  
  104.  
  105. #CALCULATE TOTALTIME IF POSSIBLE
  106. if ($time1a ){
  107.     $total = $time5a - $time1a
  108.     Write-Host -ForegroundColor Green "Total =" $total | select hours, minutes
  109.     }
  110. else{
  111.     Write-Host -ForegroundColor Green "total time can't be calculated"
  112.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement