Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #assumes that the files exists
  2. $WorkbookPath = "c:\path\to\file\test.xlsx"
  3.  
  4. #patterns that you are trying to pull out of the txt doc
  5. $Patterns = "User","Computer","Risk Name"
  6. $VirusAlert = get-content "c:\path\to\file\virusalert.txt"
  7.  
  8. #the name of the sheet inside of the workbook
  9. $sheetName = "Sheet1"
  10.  
  11. #open excel file and select sheet
  12. $objExcel = New-Object -ComObject Excel.Application
  13. $WorkBook = $objExcel.Workbooks.Open($WorkbookPath)
  14. $WorkSheet = $WorkBook.sheets.item($sheetName)
  15.  
  16. #grabs the used range from the workbook
  17. $usedRange = $WorkSheet.usedRange
  18.  
  19. #grabs the last cell in the used range
  20. $lastCell = $usedRange.SpecialCells(11)
  21.  
  22. #new row is the next down
  23. $newRow = $lastCell.row + 1
  24.  
  25. #Loop through all data in txt file you care about
  26. foreach ($Pattern in $Patterns){
  27.  
  28. #pull the line you want from the txt file
  29. $AlertValue = $VirusAlert | Select-string -pattern $Pattern -list -quiet
  30.  
  31. #split off the labels, keep only the data you wanted
  32. #this will break easily if the format isn't consistent
  33. #or if you were to have an extra space for ex. computer name
  34. #would only caputer "name"
  35. #"User:  Me" --> "Me"
  36. $AlertValue = ($AlertValue -split " ")[-1]
  37.  
  38. #Find the column with that header
  39. #also breaks easily as this must match exactly and has to be unique
  40. $col = ($Worksheet.Columns.Find($pattern))
  41.  
  42. #insert data into the new row in the col number we just found
  43. $WorkSheet.cells.item($newRow, $col.Column).value = $AlertValue
  44. }#foreach pattern
  45.  
  46. #save and quit
  47. $WorkBook.Save()
  48. $objExcel.Quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement