I have gone horribly wrong somewhere in my report. I am now only getting an output with the following type information:
Capability | Config | Layout |
VMware.Vim.VirtualMachineCapability | VMware.Vim.VirtualMachineConfigInfo | VMware.Vim.VirtualMachineFileLayout |
This is my script which is probably not the best way to do this but was trying to improve performance from roughly 4 hours to hopefully minutes.
#Variables
$Date = get-date
$Datefile = ( get-date ).ToString('yyyy-MM-dd-hhmmss')
$oldverbose = $VerbosePreference
$VerbosePreference = "continue"
# Variable to change
$ErrorActionPreference = "SilentlyContinue"
$CreateCSV= "yes"
$GridView = "yes"
$HTML = "yes"
$DisplayHTMLOnScreen = "no"
$EmailHTML = "yes"
$SendEmail = "yes"
$FileHTML = New-Item -type file "C:\Report\Missing_Backups_$datefile.html"
$FileCSV = New-Item -type file "C:\Reports\Missing_Backups_$datefile.csv"
#Credential
$username = "administrator@vsphere.local"
$password = cat "C:\Scripts\admin-securestring.txt" | convertto-securestring
$creds = new-object -typename System.Management.Automation.PSCredential `
-argumentlist $username, $password
#Text to the HTML file
Function Create-HTMLTable
{
param([array]$Array)
$arrHTML = $Array | ConvertTo-Html
$arrHTML[-1] = $arrHTML[-1].ToString().Replace(‘</body></html>’,"")
Return $arrHTML[5..2000]
}
$output = @()
$output += ‘<html><head></head><body>’
$output +=
‘<style>table{border-style:solid;border-width:1px;font-size:8pt;background-color:#7ab0f9;width:100%;}th{text-align:left;}td{background-color:#fff;width:20%;border-style:so
lid;border-width:1px;}body{font-family:verdana;font-size:12pt;}h1{font-size:12pt;}h2{font-size:10pt;}</style>’
$output += ‘<H1>[2019 - Weekly Report - / Missing VM Backups Report</H1>’
$output += ‘<H2>Date and time</H2>’,$date
Write-Verbose "Connecting to vCenter Servers"
#Modules#
Import-Module VMware.PowerCLI
#Connect to vCenter
Connect-VIServer "VCENTER1" -Credential $creds -WarningAction SilentlyContinue
Write-Verbose "Gathering VM statistics"
######################################################################################
$report = @()
$report += Get-View -ViewType VirtualMachine -Filter @{'Runtime.PowerState'='poweredOn'}
Select Name,
@{N='Datacenter';E={
$parent = Get-View -Id $_.Parent -Property Name,Parent
while($parent -isnot [VMware.Vim.Datacenter] -and $parent){
$parent = Get-View -Id $parent.Parent -Property Name,Parent
}
if($parent){
$parent.Name
}}},
@{N='Cluster';E={
$parent = Get-View -Id $_.ResourcePool
while($parent -isnot [VMware.Vim.ClusterComputeResource] -and $parent){
$parent = Get-View -Id $parent.Parent -Property Name,Parent
}
if($parent){
$parent.Name}}},
@{N='OS';E={$_.Config.GuestFullName}},
@{N="Networker Policy";E={$viewThisVM = $_; ($viewThisVM.CustomValue | ?{$_.Key -eq ($viewThisVM.AvailableField | ?{$_.Name -eq "Last EMC vProxy Backup"}).Key}).Value}},
@{N='SnapShot';E={
function Get-Snap{
param([PSObject]$snap)
$snap
if($snap.ChildSnapshotList){
$snap.ChildSnapshotList | %{
Get-Snap -Snap $_
}
}
}
$script:snaps = $_.Snapshot.RootSnapshotList | %{
Get-Snap -Snap $_
}
($script:snaps | sort-Object -property Name).Name -join '|'}},
@{N='SnapShot Created';E={($script:snaps | sort-Object -property Name).CreateTime -join '|'}},
@{N="Days Old";E={ (New-TimeSpan -End (Get-Date) -Start $script:snaps.CreateTime).Days}}
#Output
if ($GridView -eq "yes") {
$report | Out-GridView }
if ($CreateCSV -eq "yes") {
$report | Export-Csv $FileCSV -NoTypeInformation -UseCulture }
if ($HTML -eq "yes") {
$output += ‘<p>’
$output += ‘<H2>Weekly Report - / Missing VM Backups Report</H2>’
$output += ‘<p>’
$output += Create-HTMLTable
$output += ‘</p>’
$output += ‘</body></html>’
$output | Out-File $FileHTML }
if ($DisplayHTMLOnScreen -eq "yes") {
ii $FileHTML}
#mail CSV
if ($SendEmail -eq "yes") {
$filename = "C:\Reports\Missing_Backups_$datefile.csv"
$smtpServer = "smtp.davisvision.com"
$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($filename)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = "vCenter@abc.com"
$msg.To.Add("SysReports@abc.com")
$msg.Subject = "Weekly Report - / Missing VM Backups Report CSV - $Date"
$msg.Body = "Enjoy your weekly vCenter Missing VM Backups Report."
$msg.Attachments.Add($att)
$smtp.Send($msg)
}
#mail HTML
if ($EmailHTML -eq "yes") {
$filename = "C:\Report\Missing_Backups_$datefile.html"
$smtpServer = "smtp.davisvision.com"
$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($filename)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = "vCenter@abc.com"
$msg.To.Add("SysReports@abc.com")
$msg.Subject = "Weekly Report - / Missing VM Backups Report HTML - $Date"
$msg.Body = "Enjoy your weekly vCenter Missing VM Backups Report."
$msg.Attachments.Add($att)
$smtp.Send($msg)
}
$VerbosePreference = $oldverbose
Disconnect-V -Server * -Force -WarningAction SilentlyContinue -Confirm:$false
Clear-Variable -Name * -Force