Avatar of Mike
Mike
Flag for United States of America asked on

Unable to Parse MBSA Log using PowerShell Script

Hello,
I am having trouble getting the following script to parse List of MBSA logs located on my local hard drive. Every time I start the script I get the following error message below.  I have checked and verified my credentials have full access to that file directory  and the script is running as “administrator” when I start it.  The error message pointing to an error on Line 30 char: 38 as follows “[XML]$ScanResult = Get-Content <<<<  "C:\SecurityScans".. Can somebody take look at and see what I may be missing.  I have attached copy of the script along with this question.

Error Message
Get-Content : Access to the path 'C:\SecurityScans' is denied.
At C:\Users\admin\Desktop\MBSA Folder\MBSA Itemzed list\MBSA List of Missing Patches.ps1:30 char:38
+        [XML]$ScanResult = Get-Content <<<<  "C:\SecurityScans"
    + CategoryInfo          : PermissionDenied: (C:\SecurityScans:String) [Get-Content], UnauthorizedAccessException
    + FullyQualifiedErrorId : GetContentReaderUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetContentCommand

# Get Excel ready    
    $Excel = New-Object -comobject Excel.Application    
    $Excel.visible = $True     
    $Workbook = $Excel.Workbooks.Add()    
    $Info = $Workbook.Worksheets.Item(1)    
       
    # Create our column headers    
    $Info.Cells.Item(1,1) = "Computer Name"   
    $Info.Cells.Item(1,2) = "SDK Components Security Updates"   
    $Info.Cells.Item(1,3) = "SQL Server Security Updates"   
    $Info.Cells.Item(1,4) = "Windows Security Updates"   
    $Info.Cells.Item(1,5) = "BizTalk Server Security Updates"   
    $Info.Cells.Item(1,6) = "Exchange Security Updates"   
    $Info.Cells.Item(1,7) = "Office Security Updates"   
     
    # Add a little formatting    
    $Style = $Info.UsedRange    
    $Style.Interior.ColorIndex = 19    
    $Style.Font.ColorIndex = 11    
    $Style.Font.Bold = $True   
     
    $intRow = 2    
     
    # iterate over each .mbsa file    
    foreach ($file in $file)    
    {    
       [XML]$ScanResult = Get-Content "C:\SecurityScans"
       $Scanned = $ScanResult.SecScan.Check | select Name, Advice    
       $Server = $ScanResult.SecScan.Machine    
       foreach($Scan in $Scanned)    
       {    
          # if Advice doesn't start with a numeric value then set it equal to 0    
          if( $Scan.Advice -match '^(?<Cnt>[0-9]*)'){$Advice=$matches.cnt}    else{$Advice=0}    
                  
          $Style.Cells.Item($intRow, 1) = $Server   
                 
          switch ($Scan.Name)     
          {    
             "SDK Components Security Updates"   {$Style.Cells.Item($intRow, 2) = $Advice;break}    
             "SQL Server Security Updates"       {$Style.Cells.Item($intRow, 3) = $Advice;break}    
             "Windows Security Updates"          {$Style.Cells.Item($intRow, 4) = $Advice;break}    
             "BizTalk Server Security Updates"   {$Style.Cells.Item($intRow, 5) = $Advice;break}    
             "Exchange Security Updates"         {$Style.Cells.Item($intRow, 6) = $Advice;break}    
             "Office Security Updates"           {$Style.Cells.Item($intRow, 7) = $Advice;break}    
          }    
         
       }    
       $intRow = $intRow + 1    
    }

Open in new window

Microsoft Server AppsMicrosoft DevelopmentPowershell

Avatar of undefined
Last Comment
Mike

8/22/2022 - Mon
Qlemo

C:\SecurityScans is a folder, and you can only read files with Get-Content. That line (27 in your code snippet) should sound:
 [XML]$ScanResult = Get-Content "C:\SecurityScans\$file"

Open in new window

However, there is another typo in the foreach line (25) - you are using the same var as foreach var and files array. I reckon that line should be:
  foreach ($file in $files)

Open in new window

Mike

ASKER
Qlemo,

     Thanks for your help...... I went back and looked at the code I posted and missed part of it my bad.... Here is the complete code....   i went back and corrected some spelling and got the following error . I have attached the output file I trying to import over into excel file (need to change the file ext from .txt to .mbsa to read it..



Error Message

Get-Content : Cannot find path 'C:\Users\admin\HI - HVXJMN1 (8-11-2011 10-14 AM).mbsa' because it does not exist.
At C:\Users\admin\Desktop\MBSA Folder\MBSA Itemzed list\MBSA List of Missing Patches.ps1:30 char:38
+        [XML]$ScanResult = Get-Content <<<<  $Files
    + CategoryInfo          : ObjectNotFound: (C:\Users\admin\... 10-14 AM).mbsa:String) [Get-Content], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
$files = Get-ChildItem -path 'C:\users\admin\SecurityScans'     
     
    # Get Excel ready    
    $Excel = New-Object -comobject Excel.Application    
    $Excel.visible = $True     
    $Workbook = $Excel.Workbooks.Add()    
    $Info = $Workbook.Worksheets.Item(1)    
       
    # Create our column headers    
    $Info.Cells.Item(1,1) = "Computer Name"   
    $Info.Cells.Item(1,2) = "SDK Components Security Updates"   
    $Info.Cells.Item(1,3) = "SQL Server Security Updates"   
    $Info.Cells.Item(1,4) = "Windows Security Updates"   
    $Info.Cells.Item(1,5) = "BizTalk Server Security Updates"   
    $Info.Cells.Item(1,6) = "Exchange Security Updates"   
    $Info.Cells.Item(1,7) = "Office Security Updates"   
     
    # Add a little formatting    
    $Style = $Info.UsedRange    
    $Style.Interior.ColorIndex = 19    
    $Style.Font.ColorIndex = 11    
    $Style.Font.Bold = $True   
     
    $intRow = 2    
     
    # iterate over each .mbsa file    
    foreach ($file in $files)    
    {    
       [XML]$ScanResult = Get-Content $Files
       $Scanned = $ScanResult.SecScan.Check | select Name, Advice    
       $Server = $ScanResult.SecScan.Machine    
       foreach($Scan in $Scanned)    
       {    
          # if Advice doesn't start with a numeric value then set it equal to 0    
          if( $Scan.Advice -match '^(?<Cnt>[0-9]*)'){$Advice=$matches.cnt}    else{$Advice=0}    
                  
          $Style.Cells.Item($intRow, 1) = $Server   
                 
          switch ($Scan.Name)     
          {    
             "SDK Components Security Updates"   {$Style.Cells.Item($intRow, 2) = $Advice;break}    
             "SQL Server Security Updates"       {$Style.Cells.Item($intRow, 3) = $Advice;break}    
             "Windows Security Updates"          {$Style.Cells.Item($intRow, 4) = $Advice;break}    
             "BizTalk Server Security Updates"   {$Style.Cells.Item($intRow, 5) = $Advice;break}    
             "Exchange Security Updates"         {$Style.Cells.Item($intRow, 6) = $Advice;break}    
             "Office Security Updates"           {$Style.Cells.Item($intRow, 7) = $Advice;break}    
          }    
         
       }    
       $intRow = $intRow + 1    
    }

Open in new window

Domain-Name---Device-Name---8-10.xml
ASKER CERTIFIED SOLUTION
Qlemo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Mike

ASKER
Making the Change to  "Get-Content $Files.Fullname " string done the trick and I was able to get a nice clear listing on to a excel spreadsheet... thanks for the hlep
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Mike

ASKER
The change you suggest done the trick.... thanks for your help...