Skip to content

Instantly share code, notes, and snippets.

@felmoltor
Created August 3, 2017 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felmoltor/921fad0713a44023057de8ca1b18b29a to your computer and use it in GitHub Desktop.
Save felmoltor/921fad0713a44023057de8ca1b18b29a to your computer and use it in GitHub Desktop.
Powershell - Parse-MBSA
# Author: Felipe Molina (@felmoltor)
# Date: 2017/08
# Summary: These functions are used to print colored messages on powershell.
Function Print-Ok (){
param(
[Parameter(Mandatory=$true,Position=0)][string]$msg
)
Write-Host -NoNewline "[ "
Write-Host -NoNewline -ForegroundColor Green "OK"
Write-Host -NoNewline " ]`t"
Write-Host $msg
}
Function Print-Nok (){
param(
[Parameter(Mandatory=$true,Position=0)][string]$msg
)
Write-Host -NoNewline "[ "
Write-Host -NoNewline -ForegroundColor Red "NOK"
Write-Host -NoNewline " ]`t"
Write-Host $msg
}
Function Print-Fail (){
param(
[Parameter(Mandatory=$true,Position=0)][string]$msg
)
Write-Host -NoNewline "[ "
Write-Host -NoNewline -ForegroundColor Red "FAIL"
Write-Host -NoNewline " ]`t"
Write-Host $msg
}
Function Print-Warning (){
param(
[Parameter(Mandatory=$true,Position=0)][string]$msg
)
Write-Host -NoNewline "[ "
Write-Host -NoNewline -ForegroundColor Yellow "WARN"
Write-Host -NoNewline " ]`t"
Write-Host $msg
}
Function Print-Info (){
param(
[Parameter(Mandatory=$true,Position=0)][string]$msg
)
Write-Host -NoNewline "[ "
Write-Host -NoNewline -ForegroundColor Gray "INF"
Write-Host -NoNewline " ]`t"
Write-Host $msg
}
Function Print-Plus (){
param(
[Parameter(Mandatory=$true,Position=0)][string]$msg
)
Write-Host -NoNewline "["
Write-Host -NoNewline -ForegroundColor Green "+"
Write-Host -NoNewline "]`t"
Write-Host $msg
}
Function Print-Minus (){
param(
[Parameter(Mandatory=$true,Position=0)][string]$msg
)
Write-Host -NoNewline "["
Write-Host -NoNewline -ForegroundColor Red "-"
Write-Host -NoNewline "]`t"
Write-Host $msg
}
Function Print-Wildcard (){
param(
[Parameter(Mandatory=$true,Position=0)][string]$msg
)
Write-Host -NoNewline "["
Write-Host -NoNewline -ForegroundColor Yellow "*"
Write-Host -NoNewline "]`t"
Write-Host $msg
}
Function Print-CapitalI(){
param(
[Parameter(Mandatory=$true,Position=0)][string]$msg
)
Write-Host -NoNewline "["
Write-Host -NoNewline -ForegroundColor Gray "I"
Write-Host -NoNewline "]`t"
Write-Host $msg
}
Function Print-QuestionMark (){
param(
[Parameter(Mandatory=$true,Position=0)][string]$msg
)
Write-Host -NoNewline "["
Write-Host -NoNewline -ForegroundColor Gray "?"
Write-Host -NoNewline "]`t"
Write-Host $msg
}
# Author: Felipe Molina (@felmoltor)
# Date: 2017/08
# Summary: The function Parse-MBSA is used to transform "mbsacli.exe /xmlout" output XML to a CSV file.
############
# INCLUDES #
############
# . .\ColoredMessages.ps1
###############
# CONFIG VARS #
###############
# $TODAY=$(Get-Date -UFormat "%Y%M%d_%H%m%S")
#############
# FUNCTIONS #
#############
Function Parse-XML(){
param(
[Parameter(Mandatory=$true,Position=0)][string]$in,
[Parameter(Mandatory=$true,Position=1)][string]$out
)
$xmlfile=[xml]$(Get-Content $in)
# Iterate through the executed checks
$xmlfile.XMLOut.Check | ForEach-Object {
$check = $_
$gn=$check.GroupName
$name=$check.Name
$advice=$check.Advice
# For each check iterate for every item on the detailed data
$check.Detail.UpdateData | ForEach-Object {
$UpdateData = $_
$udid=$UpdateData.ID
$udbid=$UpdateData.BulletinID
$udkbid=$UpdateData.KBID
$udii=$UpdateData.IsInstalled
$udsev=$UpdateData.Severity
$udrr=$UpdateData.RestartRequired
$udt=$UpdateData.Title
$udburl=$UpdateData.References.BulletinURL
$udiurl=$UpdateData.References.InformationURL
$uddurl=$UpdateData.References.DownloadURL
Out-File -Append -FilePath $out -InputObject "$gn`t$name`t$advice`t$udid`t$udbid`t$udkbid`t$udii`t$udsev`t$udrr`t$udt`t$udburl`t$udiurl`t$uddurl"
}
}
}
#############
Function Create-Output(){
param(
[Parameter(Mandatory=$true,Position=0)][string]$outputfile
)
$overwrite="N"
$outdirinfo=[System.IO.DirectoryInfo]$outputfile
if (Test-path $outputfile){
$overwrite =$(Read-Host -Prompt "The file '$outputfile' already exists. Do you want to overwrite? [y/N]").ToUpper()
if (($overwrite -eq "Y") -or ($overwrite -eq "YES")) {
New-Item -ItemType File -Force -Path $outputfile
}
else {
While (Test-Path $outputfile) {
$outputfile = $(read-host -Prompt "Please specify a new output file path not already existent")
}
if (-not (Test-Path $outdirinfo.Parent.FullName)){
New-Item -ItemType Directory -Path $($outdirinfo.Parent.FullName)
}
New-Item -ItemType File -Path $outputfile
}
}
# Insert CSV headers
Out-File -FilePath $outputfile -InputObject "GroupName`tName`tAdvice`tID`tBulletinID`tKBID`tIsInstalled`tSeverity`tRestartRequired`tTitle`tBulletinURL`tInformationURL`tDownloadURL"
return $outputfile
}
########
# MAIN #
########
Function Parse-MBSA(){
param(
[Parameter(Mandatory=$true,Position=0)][string]$mbsaXML=$null,
[Parameter(Mandatory=$true,Position=1)][string]$outfile=$null
)
# Check input parameters
if (-not (Test-Path $mbsaXML)) {
Print-Minus "Input file $mbsaXML does not exist"
exit
}
# Create the output CVS file
$definitiveoutput=Create-Output $outfile
Write-Host "The output will be in $definitiveoutput"
# Analyze the XML input file
Print-CapitalI "Input file is being parsed. Please wait a minute..."
Parse-XML -in $mbsaXML -out $definitiveoutput
Print-CapitalI "File has been parsed. Output CSV can be found in '$definitiveoutput'"
& explorer $definitiveoutput
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment