Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #assumes that the files exists
- $WorkbookPath = "c:\path\to\file\test.xlsx"
- #patterns that you are trying to pull out of the txt doc
- $Patterns = "User","Computer","Risk Name"
- $VirusAlert = get-content "c:\path\to\file\virusalert.txt"
- #the name of the sheet inside of the workbook
- $sheetName = "Sheet1"
- #open excel file and select sheet
- $objExcel = New-Object -ComObject Excel.Application
- $WorkBook = $objExcel.Workbooks.Open($WorkbookPath)
- $WorkSheet = $WorkBook.sheets.item($sheetName)
- #grabs the used range from the workbook
- $usedRange = $WorkSheet.usedRange
- #grabs the last cell in the used range
- $lastCell = $usedRange.SpecialCells(11)
- #new row is the next down
- $newRow = $lastCell.row + 1
- #Loop through all data in txt file you care about
- foreach ($Pattern in $Patterns){
- #pull the line you want from the txt file
- $AlertValue = $VirusAlert | Select-string -pattern $Pattern -list -quiet
- #split off the labels, keep only the data you wanted
- #this will break easily if the format isn't consistent
- #or if you were to have an extra space for ex. computer name
- #would only caputer "name"
- #"User: Me" --> "Me"
- $AlertValue = ($AlertValue -split " ")[-1]
- #Find the column with that header
- #also breaks easily as this must match exactly and has to be unique
- $col = ($Worksheet.Columns.Find($pattern))
- #insert data into the new row in the col number we just found
- $WorkSheet.cells.item($newRow, $col.Column).value = $AlertValue
- }#foreach pattern
- #save and quit
- $WorkBook.Save()
- $objExcel.Quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement