VMware Cloud Community
BrianDGeorge
Enthusiast
Enthusiast
Jump to solution

Get-View VM Report for Backup Team

I have gone horribly wrong somewhere in my report.  I am now only getting an output with the following type information:

CapabilityConfigLayout
VMware.Vim.VirtualMachineCapabilityVMware.Vim.VirtualMachineConfigInfoVMware.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

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Looks like you might have forgotten the pipeline symbol at the end of the Get-View line

$report = @()

$report += Get-View -ViewType VirtualMachine -Filter @{'Runtime.PowerState'='poweredOn'} |

Select Name,

---------------------------------------------------------------------------------------------------------

Was it helpful? Let us know by completing this short survey here.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

Where do you see that? In the grid, the CSV file, the HTML file?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
BrianDGeorge
Enthusiast
Enthusiast
Jump to solution

I see it in the grid and csv.  The HTML never generates.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Looks like you might have forgotten the pipeline symbol at the end of the Get-View line

$report = @()

$report += Get-View -ViewType VirtualMachine -Filter @{'Runtime.PowerState'='poweredOn'} |

Select Name,

---------------------------------------------------------------------------------------------------------

Was it helpful? Let us know by completing this short survey here.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
BrianDGeorge
Enthusiast
Enthusiast
Jump to solution

So that fixed it, it all came down to a pipe.  Thank you for your keen eyes.  You wouldn't know how to fit the portion of the script referring to the created by, and adding the "This event is not in vCenter events database" for missing data into the script would you?

$snapshot=get-snapshot *

foreach ($snap in $snapshot){

$snapevent = Get-VIEvent -Entity $snap.VM -Types Info -Finish (( get-date $snap.Created).addhours(1)) | Where-Object {$_.FullFormattedMessage -imatch ‘Task: Create virtual machine snapshot’} | sort createdtime -Descending | select -First 1

if ($snapevent -ne $null){

Write-Host ( “VM: “+ $snap.VM + “. Snapshot ‘” + $snap + “‘ created on ” + $snap.Created + ” by ” + $snapevent.UserName +”.” ) -foregroundcolor GREEN

}else {

Write-Host (“VM: “+ $snap.VM + “. Snapshot ‘” + $snap + “‘ created on ” + $snap.Created + “. This event is not in vCenter events database”) -foregroundcolor RED

}

}

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Something like this?

@{N = 'SnapShot Created'; E = {

        if ($script:snaps) {

            ($script:snaps | sort-Object -property Name).CreateTime -join '|'

        }

        else {

            "This event is not in vCenter events database"

        } } },


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
BrianDGeorge
Enthusiast
Enthusiast
Jump to solution

Looks like what you provided me was for the date which I have, I am actually looking for the vievent with the created by in it.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That should be the same logic.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
BrianDGeorge
Enthusiast
Enthusiast
Jump to solution

I am only able to get the date, I am looking for correlation in the event logs.  I am working on something like the following but am coming up blank.

   @{N="SnapShot Created By";E={

        $snapevent = Get-VIEvent -Entity $snap.VM -Types Info | Where-Object {$_.FullFormattedMessage -imatch ‘Task: Create virtual machine snapshot’} | sort createdtime -Descending | select -First 1

        if ($snapeventUserName +”.” ){

        }else {

       ". This event is not in vCenter events database”

       }

       }

       }

0 Kudos