Avatar of Mike
MikeFlag 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
Avatar of Qlemo
Qlemo
Flag of Germany image

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

Avatar of Mike
Mike
Flag of United States of America image

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
Avatar of Qlemo
Qlemo
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Mike
Mike
Flag of United States of America image

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
Avatar of Mike
Mike
Flag of United States of America image

ASKER

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

Most development for the Microsoft platform is done utilizing the technologies supported by the.NET framework. Other development is done using Visual Basic for Applications (VBA) for programs like Access, Excel, Word and Outlook, with PowerShell for scripting, or with SQL for large databases.

48K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo