Share via


powershell script to automate BPA for Hyper-V

Another little powershell script to automate some bits that i come across

this script will

1. test if you have the hyper-v bpa installed

2. download the hyper-v and install it

3. run it and list errors and warnings

4. output the whole thing to a CSV file

 

this is on the start of the script i have a bit more the add so will be posting more at a later date

#script begin

set-executionpolicy unrestricted

$testbpaexit = $null
$testbpaexist = get-wmiobject win32_quickfixengineering |where {$_.hotfixid -eq "KB977238"}

#if bpa doesnt exist download and install

if ($testbpaexist -eq $null)
{
 write-host "Hyper-V BPA Not Found" -foregroundcolor red -backgroundcolor black
 write-host
 write-host
 write-host "Downloading BPA...." -foregroundcolor Blue -backgroundcolor black

 $rootpath = $env:systemroot + "\System32\wusa.exe"
 $file = $env:Userprofile + "\Downloads\hypervbpa.msu"
 $url = "https://www.microsoft.com/downloads/info.aspx?na=41&srcfamilyid=89d80c15-0082-4fef-a4fc-fefa463bed08&srcdisplaylang=en&u=http%3a%2f%2fdownload.microsoft.com%2fdownload%2f1%2fA%2f1%2f1A148F55-7C2C-41F5-912A-995476A49511%2fWindows6.1-KB977238-x64.msu"
 $downloadobject = new-object System.Net.WebClient
 $downloadobject.DownloadFile($url,$file)
 
 write-host "Installing BPA...." -foregroundcolor Blue -backgroundcolor black
 $parameters = $file + " /quiet /norestart"
 $installbpa = [System.Diagnostics.Process]::Start( "wusa",$parameters )
 $installbpa.WaitForExit()
 write-host "Installation Done." -foregroundcolor Green -backgroundcolor black
}

#start bpa
cls
write-host "Starting BPA Scan...." -foregroundcolor Yellow -backgroundcolor black

$bparesults = invoke-bpamodel Microsoft/windows/hyper-v
write-host "Done..." -foregroundcolor Yellow -backgroundcolor black
write-host
write-host

$bpaerrors = $null
$bpaerrors = get-bparesult Microsoft/windows/hyper-v |where {$_.Severity -eq "Error"}

if ($bpaerrors -ne $null)
{
 write-host "Displaying Errors...." -foregroundcolor Red -backgroundcolor black
 write-host "There are " $bpaerrors.count " Errors Detected"
 foreach ($b in $bpaerrors)
 {
  write-host "Title:" -foregroundcolor Blue -backgroundcolor white
  write-host $b.Title -foregroundcolor red -backgroundcolor black
  write-host "Problem:" -foregroundcolor Blue -backgroundcolor white
  write-host $b.problem -foregroundcolor red -backgroundcolor black
  write-host "Impact:" -foregroundcolor Blue -backgroundcolor white
  write-host $b.impact -foregroundcolor red -backgroundcolor black
  write-host "Resolution:" -foregroundcolor Blue -backgroundcolor white
  write-host $b.resolution -foregroundcolor red -backgroundcolor black
  write-host "Help:" -foregroundcolor Blue -backgroundcolor white
  write-host $b.help -foregroundcolor red -backgroundcolor black
  write-host
 }

 
}

$bpawarnings = $null
$bpawarnings =  get-bparesult Microsoft/windows/hyper-v| where {$_.Severity -eq "Warning"}

if ($bpawarnings -ne $null)
{
 write-host "Displaying Warnings..." -foregroundcolor Yellow -backgroundcolor black
 write-host "There are " $bpawarnings.count " Warnings Detected"
 foreach ($b2 in $bpawarnings)
 {
  
  write-host "Title:" -foregroundcolor Blue -backgroundcolor white
  write-host $b2.Title -foregroundcolor Yellow -backgroundcolor black
  write-host "Problem:" -foregroundcolor Blue -backgroundcolor white
  write-host $b2.problem -foregroundcolor Yellow -backgroundcolor black
  write-host "Impact:" -foregroundcolor Blue -backgroundcolor white
  write-host $b2.impact -foregroundcolor Yellow -backgroundcolor black
  write-host "Resolution:" -foregroundcolor Blue -backgroundcolor white
  write-host $b2.resolution -foregroundcolor Yellow -backgroundcolor black
  write-host "Help:" -foregroundcolor Blue -backgroundcolor white
  write-host $b2.help -foregroundcolor Yellow -backgroundcolor black
  write-host
  write-host
  write-host

 }

}

# Output to a CSV file
write-host "Outputting to file...."
$Outpath = $env:Userprofie + “\Desktop\BPAHyperVReport.csv
get-bparesult Microsoft/windows/hyper-v | select resultnumber,@{name=”Server Name”; Expression={hostname}},ModelID,RuleID,ResultID,Severity,NeutralSeverity,Category,Title,Problem,Impact,Resolution,Compliance,Help,Excluded | Export-CSV $Outpath