I have a script which runs to scan all hosts and check compliance and then emails the details to me but i am trying to format the email so that it looks nice but the spacing doesn't want to work
add-pssnapin VMware.VimAutomation.Core
connect-viserver "XXXX"
get-vmhost | scan-inventory
$report = get-vmhost | get-compliance -detailed | sort-object -Property Entity | format-table -autosize | out-string -width 5000
$emailFrom = "vxxx@xx.com"
$emailTo = "xx@xx.com"
$subject = "VM Host Patch Compliance Status "
$body = $report | ft -AutoSize | Out-String
$smtpServer = "email server"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)
the output looks like this
Entity Baseline Status CompliantPatches NotCompliantPatches UnknownPaches NotApplicablePatches
------ -------- ------ ---------------- ------------------- ------------- --------------------
xxxxxnvm001.xxxxx.co.uk ESX 4.1 Compliant 13 0 0 0
xxxxxxvm001.xxxxx.co.uk ESX 4.1 Compliant 13 0 0 0
xxxxxxvm001.xxxxx.co.uk ESX 4.1 Compliant 12 0 0 0
xxxxxxvm001.xxxxx.co.uk ESX 4.1 Compliant 12 0 0 0
xxxxxxvm002.xxxxx.co.uk ESX 4.1 Compliant 12 0 0 0
First thing is it seems to be listing all the server twice
and secondly I want to be able to space it out - i have tried filtering the columns out and it just get different deatils i.e. it lists all the patches and other random things
any help would be appreciated
Hello, cjwirweazel-
First thing is it seems to be listing all the server twice
Get-PSSnapin VMware.VumAutomation | ft name,version
But, to just work with the detailed info for each host (and, so, only get one result for each host), you could pipe the Get-Compliance output to Where-Object as such:
Get-VMHost | Get-Compliance | ?{$_ -is [VMware.VumAutomation.Types.PatchBaselineCompliance]}
Of course, that is something to be careful with, because that typename is not necessarily static (it may change in future releases from VMware, if any).
and secondly I want to be able to space it out - i have tried filtering the columns out and it just get different deatils i.e. it lists all the patches and other random things
As for filtering the columns, the Get-Compliance cmdlet is formatting the info in its default output. That is, the default output from the cmdlet is not directly displaying the info returned, but is doing some formatting of the data before returning the table.
You could filter the info and display the remaining info in the same manner by using a new calculated property (if not familiar with the calculated properties, see the full help on Format-Table).
So, the meat of the script would be:
## array of BaselineCompliance objects $arrPatchBaselineComplianceOnly = Get-VMHost | Get-Compliance | ?{$_ -is [VMware.VumAutomation.Types.PatchBaselineCompliance]} ## make the calculated property hashtables $hshBaselineDisplay = @{n="Baseline"; e={$_.Baseline.Name}; a="left"} $hshCompliantPatchesCount = @{n="NumCompliant"; e={@($_.CompliantPatches).Count}; a="left"} $hshNotCompliantPatchesCount = @{n="NumNotCompliant"; e={@($_.NotCompliantPatches).Count}; a="left"} ## put the formatted info into a variable to be put into the email $strEmailBody = $arrPatchBaselineComplianceOnly | ft entity,$hshBaselineDisplay,$hshCompliantPatchesCount,$hshNotCompliantPatchesCount -a ##...send email here...
How does that do for you?
the reason i got two sets of results was two connections to the VI Server - silly me connected up and then ran the script which connected again.
I will give that script a go and let you know - looks good though
cheers
chris