I had to create a script to automate vulnerability reporting. Thanks to my colleague Grzegorz from http://psvmware.wordpress.com who helped me a lot with a script. Script generates report in CSV format and send email out with attachment.
Requirements:
- Base PowerCLI installed
- VUM PowerCLI snap-in installed
[button color=”red”] NOTE – VUM PowerCLI snap-in has to have the same build version as vCenter server.[/button]
Add-PSSnapin VMware.VimAutomation.Core Add-PSSnapin VMware.VumAutomation #variables $VC='vcenter-server-IP-or-FQDN' $expfile='D:\tools\scripts\VPM-report.csv' $date=get-date -Format dd/MM/yyyy #import credentials $pwd = Get-Content D:\tools\scripts\ap-vcs-credentials | ConvertTo-SecureString $credentials = New-Object System.Management.Automation.PsCredential โusernameโ, $pwd #connect to vCS server Connect-VIServer -Server $VC $baseline=Get-PatchBaseline -Name 'ESX-*' #remove last report from tmp location Remove-Item $expfile $hosts=get-vmhost #scanning $hosts | Scan-Inventory $ComplianceResult=$hosts |Get-Compliance -Baseline $baseline -Detailed $ComplianceResult | select @{n='Esx Host';e={$_.entity.name}}, @{n='esx Version';e={$_.entity.version}}, Status, @{n='CompliantPatches';e={$_.CompliantPatches.count}}, @{n='NotCompliantPatches';e={$_.NotCompliantPatches.count}}, @{n='UnknownPaches';e={$_.UnknownPaches.count}}, @{n='NotApplicablePatches';e={$_.NotApplicablePatches.count}}, @{n='Baseline name';e={$_.Baseline.Name}}, @{n='generated on';e={get-date -Format dd/MM/yyyy}} |export-csv $expfile -NoTypeInfor
Send email with attached CSV report.
#email variables $smtpserver='FQDN-for-SMTP-server' $emailfrom="sender email" $emailto="recipient email " $emailcc="who's in CC" $emaildate=get-date -Format MMMM/yyyy #send email with attachment Send-mailmessage -to $emailto -cc $emailcc -from $emailfrom -subject "Report Virtual infrastructure $emaildate" -Attachments $expfile -SmtpServer $smtpserver Disconnect-VIServer -Confirm:$false
[button link=”http://www.vmwaremine.com/2013/04/19/schedule-task-with-powercli-script/”] Learn how to store credentials in a file and how to schedule a task with PowerCLI script in Windows 2008 R2[/button]
Not bad ๐