ReportCardPS.psm1
Write-Verbose 'Importing from [C:\projects\reportcardps\ReportCardPS\private]' # .\ReportCardPS\private\Get-vCenterAlarmSet.ps1 function Get-vCenterAlarmSet { <# .DESCRIPTION Builds HTML Reports using VMware's ClarityUI library. .PARAMETER tbd01 working on the details .PARAMETER tbd02 working on the details .EXAMPLE Get-vCenterAlarmSet .NOTES No notes at this time. #> [CmdletBinding( SupportsShouldProcess = $true, ConfirmImpact = "Low" )] [OutputType([String])] [OutputType([boolean])] param( [Parameter()][String]$tbd01, [Parameter()][String]$tbd02 ) if ($pscmdlet.ShouldProcess("Starting Get-vCenterAlarmSet function.")) { try { #Add Function details # Get Alarm Information $ServiceInstance = Get-View ServiceInstance $AlarmManager = Get-View -Id $ServiceInstance.Content.AlarmManager $vCenterInventory = Get-Inventory # Loop through each inventory Item to determine the number of active alarms $vCenterAlarmCollection = New-Object System.Collections.ArrayList foreach ($Item in $vCenterInventory) { $thisItem = New-Object System.Object $objectName = $Item.Name $thisItemAlarmsCount = 0 $thisItemWarningsCount = 0 $thisItemAlarmsCount = ($AlarmManager.GetAlarmState($Item.ExtensionData.MoRef) | Where-Object { [VMware.Vim.ManagedEntityStatus]::yellow -contains $_.OverallStatus } | Measure-Object).count $thisItemWarningsCount = ($AlarmManager.GetAlarmState($Item.ExtensionData.MoRef) | Where-Object { [VMware.Vim.ManagedEntityStatus]::red -contains $_.OverallStatus } | Measure-Object).count if (($thisItemAlarmsCount -ne 0) -or ($thisItemWarningsCount -ne 0)) { # Add the item to the collection $thisItem | Add-Member -MemberType NoteProperty -Name "ObjectName" -Value "$objectName" $thisItem | Add-Member -MemberType NoteProperty -Name "AlarmCount" -Value "$thisItemAlarmsCount" $thisItem | Add-Member -MemberType NoteProperty -Name "WarningsCount" -Value "$thisItemWarningsCount" $vCenterAlarmCollection.Add($thisItem) | Out-Null } } $vCenterTop5Alarms = $vCenterAlarmCollection | Sort-Object -Property WarningsCount -Descending | Select-Object -First 5 | ConvertTo-Html -Fragment # Build the HTML Card $AlarmsCard = New-ClarityCard -Title Alarms -Icon info-standard -IconSize 24 $AlarmsCardBody = Add-ClarityCardBody -CardText "$vCenterTop5Alarms" $AlarmsCardBody += Close-ClarityCardBody $AlarmsCard += $AlarmsCardBody $AlarmsCard += Close-ClarityCard -Title "Close Alarm Card" $AlarmsCard } catch { $ErrorMessage = $_.Exception.Message $FailedItem = $_.Exception.ItemName Throw "Get-vCenterAlarmSet: $ErrorMessage $FailedItem" } } else { # -WhatIf was used. return $false } } # .\ReportCardPS\private\Get-vCenterCPU.ps1 function Get-vCenterCPU { <# .DESCRIPTION Gathers vCenter overall CPU information. .PARAMETER Include Provide a String to use when including specific VMHosts based on Name .PARAMETER Exclude Provide a String to use when excluding specific VMHosts based on Name .EXAMPLE Get-vCenterCPU .NOTES No notes at this time. #> [CmdletBinding( SupportsShouldProcess = $true, ConfirmImpact = "Low" )] [OutputType([String])] [OutputType([boolean])] param( [Parameter()][String]$Include, [Parameter()][String]$Exclude ) if ($pscmdlet.ShouldProcess("Starting Get-vCenterCPU function.")) { try { # Get the VMHosts if ($Include) { $VMHosts = Get-VMHost | Where-Object { $_.Name -like "*$Include*" } } elseif ($Exclude) { $VMHosts = Get-VMHost | Where-Object { $_.Name -notlike "*$Exclude*" } } else { $VMHosts = Get-VMHost } # Setup some empty Variables to store things $vCenterCpuTotalMhz = $null $vCenterCpuUsageMhz = $null # Loop Through the hosts to get the and CPU stats foreach ($VMHost in $VMHosts) { #$VMHostName = $VMHost.Name #Write-Output "Collecting Stats for $VMHostName" $thisVMhostCpuTotalMhz = $VMHost.CpuTotalMhz $thisVMhostCpuUsageMhz = $VMHost.CpuUsageMhz # Add the Items to the total $vCenterCpuTotalMhz += $thisVMhostCpuTotalMhz $vCenterCpuUsageMhz += $thisVMhostCpuUsageMhz } #CPU $vCenterCpuUsagePercent = ([math]::Round(($vCenterCpuUsageMhz / $vCenterCpuTotalMhz), 2)) * 100 $vCenterCPUFreeMhz = ([math]::Round(($vCenterCpuTotalMhz - $vCenterCpuUsageMhz), 2)) $vCenterCPUFreeGhz = $vCenterCPUFreeMhz / 1000 $vCenterCpuTotalGhz = $vCenterCpuTotalMhz / 1000 # Build the HTML Card $CPUCard = New-ClarityCard -Title CPU -Icon cpu -IconSize 24 $CPUCardBody = Add-ClarityCardBody -CardText "$vCenterCPUFreeGhz GHz free" $CPUCardBody += Add-ProgressBlock -value $vCenterCPUUsagePercent -max 100 $CPUCardBody += Add-CardText -CardText "$vCenterCpuTotalMhz MHz used | $vCenterCpuTotalGhz GHz total" $CPUCardBody += Close-ClarityCardBody $CPUCard += $CPUCardBody $CPUCard += Close-ClarityCard -Title "CPUCard" $CPUCard } catch { $ErrorMessage = $_.Exception.Message $FailedItem = $_.Exception.ItemName Throw "Get-vCenterCPU: $ErrorMessage $FailedItem" } } else { # -WhatIf was used. return $false } } # .\ReportCardPS\private\Get-vCenterHostSet.ps1 function Get-vCenterHostSet { <# .DESCRIPTION Gathers VMHost State information. .PARAMETER Include Provide a String to use when including specific VMHosts based on Name .PARAMETER Exclude Provide a String to use when excluding specific VMHosts based on Name .EXAMPLE Get-vCenterHostSet .NOTES No notes at this time. #> [CmdletBinding( SupportsShouldProcess = $true, ConfirmImpact = "Low" )] [OutputType([String])] [OutputType([boolean])] param( [Parameter()][String]$Include, [Parameter()][String]$Exclude ) if ($pscmdlet.ShouldProcess("Starting Get-vCenterHostSet function.")) { try { #Add Function details # Get the VMHosts if ($Include) { $VMHosts = Get-VMHost | Where-Object { $_.Name -like "*$Include*" } } elseif ($Exclude) { $VMHosts = Get-VMHost | Where-Object { $_.Name -notlike "*$Exclude*" } } else { $VMHosts = Get-VMHost } $ConnectedVMHosts = ($VMHosts | Where-Object { $_.ConnectionState -eq "Connected" } | Measure-Object).count $DisConnectedVMHosts = ($VMHosts | Where-Object { $_.ConnectionState -eq "DisConnected" } | Measure-Object).count $MaintenanceVMHosts = ($VMHosts | Where-Object { $_.ConnectionState -eq "Maintenance" } | Measure-Object).count # Make a Custom object with the information $vmHostObject = New-Object System.Object $vmHostObject | Add-Member -MemberType NoteProperty -Name "Connected" -Value "$ConnectedVMHosts" $vmHostObject | Add-Member -MemberType NoteProperty -Name " | Disconnected | " -Value "$DisConnectedVMHosts" $vmHostObject | Add-Member -MemberType NoteProperty -Name "Maintenance" -Value "$MaintenanceVMHosts" $vmHostHtml = $vmHostObject | ConvertTo-Html -Fragment # Manipulate the HTML to get the card we want. $Part1 = $vmHostHtml[0] $Part2 = $vmHostHtml[3] -replace ("<td>", "<th><h2>") $Part2 = $Part2 -replace ("</td>", "</h2></th>") $Part3 = $vmHostHtml[2] -replace ("th", "td") $Part4 = $vmHostHtml[4] $FinalvmHostHtml = $Part1 + $Part2 + $Part3 + $Part4 # Build the HTML Card $vmHostCard = New-ClarityCard -Title VMHosts -Icon host -IconSize 24 $vmHostCardBody += Add-ClarityCardBody -CardText "$FinalvmHostHtml" $vmHostCardBody += Close-ClarityCardBody $vmHostCard += $vmHostCardBody $vmHostCard += Close-ClarityCard -Title "Close vmHost Card" $vmHostCard } catch { $ErrorMessage = $_.Exception.Message $FailedItem = $_.Exception.ItemName Throw "Get-vCenterHostSet: $ErrorMessage $FailedItem" } } else { # -WhatIf was used. return $false } } # .\ReportCardPS\private\Get-vCenterMemory.ps1 function Get-vCenterMemory { <# .DESCRIPTION Gathers VMHost Memory Data. .PARAMETER Include Provide a String to use when including specific VMHosts .PARAMETER Exclude Provide a String to use when excluding specific VMHosts .EXAMPLE Get-vCenterMemory .NOTES No notes at this time. #> [CmdletBinding( SupportsShouldProcess = $true, ConfirmImpact = "Low" )] [OutputType([String])] [OutputType([boolean])] param( [Parameter()][String]$Include, [Parameter()][String]$Exclude ) if ($pscmdlet.ShouldProcess("Starting Get-vCenterMemory function.")) { try { # Get the VMHosts if ($Include) { $VMHosts = Get-VMHost | Where-Object { $_.Name -like "*$Include*" } } elseif ($Exclude) { $VMHosts = Get-VMHost | Where-Object { $_.Name -notlike "*$Exclude*" } } else { $VMHosts = Get-VMHost } # Setup some empty Variables to store things $vCenterMemoryTotalGB = $null $vCenterMemoryUsageGB = $null # Loop Through the hosts to get the Memory and CPU stats #Write-Output "Looping through the VMHosts to gather Memory and CPU data." foreach ($VMHost in $VMHosts) { #$VMHostName = $VMHost.Name #Write-Output "Collecting Stats for $VMHostName" $thisVMhostMemoryTotalGB = $VMHost.MemoryTotalGB $thisVMhostMemoryUsageGB = $VMHost.MemoryUsageGB # Add the Items to the total $vCenterMemoryTotalGB += $thisVMhostMemoryTotalGB $vCenterMemoryUsageGB += $thisVMhostMemoryUsageGB } #Mem $vCenterMemoryTotalGB = [math]::Round($vCenterMemoryTotalGB, 2) $vCenterMemoryUsageGB = [math]::Round($vCenterMemoryUsageGB, 2) $vCenterMemoryUsagePercent = ([math]::Round(($vCenterMemoryUsageGB / $vCenterMemoryTotalGB), 2)) * 100 $vCenterMemoryUsageFree = $vCenterMemoryTotalGB - $vCenterMemoryUsageGB # Build the HTML Card $MemoryCard = New-ClarityCard -Title Memory -Icon memory -IconSize 24 $MemoryCardBody = Add-ClarityCardBody -CardText "$vCenterMemoryUsageFree GB free" $MemoryCardBody += Add-ProgressBlock -value $vCenterMemoryUsagePercent -max 100 $MemoryCardBody += Add-CardText -CardText "$vCenterMemoryUsageGB GB used | $vCenterMemoryTotalGB GB total" $MemoryCardBody += Close-ClarityCardBody $MemoryCard += $MemoryCardBody $MemoryCard += Close-ClarityCard -Title "Close Memory Card" $MemoryCard } catch { $ErrorMessage = $_.Exception.Message $FailedItem = $_.Exception.ItemName Throw "Get-vCenterMemory: $ErrorMessage $FailedItem" } } else { # -WhatIf was used. return $false } } # .\ReportCardPS\private\Get-vCenterPluginSet.ps1 function Get-vCenterPluginSet { <# .DESCRIPTION Builds HTML Card with vCenter Plugin data using VMware's ClarityUI library. .PARAMETER Filter Optional - Provide a String to use as a filter. example: vSphere .EXAMPLE Get-vCenterPluginSet .EXAMPLE Get-vCenterPluginSet -Filter vSphere .NOTES No notes at this time. #> [CmdletBinding( SupportsShouldProcess = $true, ConfirmImpact = "Low" )] [OutputType([String])] [OutputType([boolean])] param( [Parameter()][String]$Filter ) if ($pscmdlet.ShouldProcess("Starting Get-vCenterPluginSet function.")) { try { # Grab the Extention Manager Object $ExtensionManager = Get-View ExtensionManager # Get the list of vCenter plugins that match the filter $InstalledPlugins = $ExtensionManager.ExtensionList | Where-Object { $_.Company -notlike "" } | Select-Object @{N = 'Name'; E = { $_.Description.Label } }, Version, Company | Where-Object { $_.Name -like "*$Filter*" } # Convert the List to HTML table $InstalledPluginsList = $InstalledPlugins | ConvertTo-Html -Fragment # Build the HTML Card $PluginCard = New-ClarityCard -Title "vCenter Plugins" -Icon plugin -IconSize 24 $PluginCardBody = Add-ClarityCardBody -CardText "$InstalledPluginsList" $PluginCardBody += Close-ClarityCardBody $PluginCard += $PluginCardBody $PluginCard += Close-ClarityCard -Title "Close Plugin Card" $PluginCard } catch { $ErrorMessage = $_.Exception.Message $FailedItem = $_.Exception.ItemName Throw "Get-vCenterPluginSet: $ErrorMessage $FailedItem" } } else { # -WhatIf was used. return $false } } # .\ReportCardPS\private\Get-vCenterStorage.ps1 function Get-vCenterStorage { <# .DESCRIPTION Builds HTML Reports using VMware's ClarityUI library. .PARAMETER Include Provide a String to use when including specific DataStores based on Name .PARAMETER Exclude Provide a String to use when excluding specific DataStores based on Name .EXAMPLE Get-vCenterStorage .NOTES No notes at this time. #> [CmdletBinding( SupportsShouldProcess = $true, ConfirmImpact = "Low" )] [OutputType([String])] [OutputType([boolean])] param( [Parameter()][String]$Include, [Parameter()][String]$Exclude ) if ($pscmdlet.ShouldProcess("Starting Get-vCenterStorage function.")) { try { # Get the DataStores # Write-Output "Gathering Storage Data." if ($Include) { $DataStores = Get-DataStore | Where-Object { $_.Name -like "*$Include*" } } elseif ($Exclude) { $DataStores = Get-Datastore | Where-Object { $_.Name -notlike "*$Exclude*" } } else { $DataStores = Get-Datastore } # Start some Empty Variables to store things $vCenterFreeSpaceGB = $null $vCenterCapacityGB = $null # Loop Through each to determine the Used space. foreach ($DataStore in $DataStores) { $thisDSCapacityGB = $Datastore.CapacityGB $thisDSFreeSpaceGB = $DataStore.FreeSpaceGB $vCenterFreeSpaceGB += $thisDSFreeSpaceGB $vCenterCapacityGB += $thisDSCapacityGB } $vCenterFreeSpaceGB = ([math]::Round($vCenterFreeSpaceGB, 2)) $vCenterCapacityGB = ([math]::Round($vCenterCapacityGB, 2)) $vCenterUsedSpaceGB = ([math]::Round(($vCenterCapacityGB - $vCenterFreeSpaceGB), 2)) $vCenterUsedSpaceGBPercent = ([math]::Round(($vCenterUsedSpaceGB / $vCenterCapacityGB), 2)) * 100 # Build the HTML Card $StorageCard = New-ClarityCard -Title Storage -Icon storage -IconSize 24 $StorageCardBody = Add-ClarityCardBody -CardText "$vCenterFreeSpaceGB GB free" $StorageCardBody += Add-ProgressBlock -value $vCenterUsedSpaceGBPercent -max 100 $StorageCardBody += Add-CardText -CardText "$vCenterUsedSpaceGB GB used | $vCenterCapacityGB GB total" $StorageCardBody += Close-ClarityCardBody $StorageCard += $StorageCardBody $StorageCard += Close-ClarityCard -Title "Close Storage Card" $StorageCard } catch { $ErrorMessage = $_.Exception.Message $FailedItem = $_.Exception.ItemName Throw "Get-vCenterStorage: $ErrorMessage $FailedItem" } } else { # -WhatIf was used. return $false } } # .\ReportCardPS\private\Get-vCenterVMSet.ps1 function Get-vCenterVMSet { <# .DESCRIPTION Gathers VM data for PowerState report. .PARAMETER Include Provide a String to use when including specific VMs based on Name .PARAMETER Exclude Provide a String to use when excluding specific VMs based on Name .EXAMPLE Get-vCenterVMSet .NOTES No notes at this time. #> [CmdletBinding( SupportsShouldProcess = $true, ConfirmImpact = "Low" )] [OutputType([String])] [OutputType([boolean])] param( [Parameter()][String]$Include, [Parameter()][String]$Exclude ) if ($pscmdlet.ShouldProcess("Starting Get-vCenterVMSet function.")) { try { if ($Include) { $vmList = Get-VM | Where-Object { $_.Name -like "*$Include*" } | Select-Object PowerState } elseif ($Exclude) { $vmList = Get-VM | Where-Object { $_.Name -notlike "*$Exclude*" } | Select-Object PowerState } else { $vmList = Get-VM | Select-Object PowerState } $PoweredOnVMsCount = ($vmList | Where-Object { $_.PowerState -eq "PoweredOn" } | Measure-Object).count $PoweredOffVMsCount = ($vmList | Where-Object { $_.PowerState -eq "PoweredOff" } | Measure-Object).count $SuspendedVMsCount = ($vmList | Where-Object { $_.PowerState -eq "Suspended" } | Measure-Object).count # Make a Custom object with the information $vmObject = New-Object System.Object $vmObject | Add-Member -MemberType NoteProperty -Name "Powered On" -Value "$PoweredOnVMsCount" $vmObject | Add-Member -MemberType NoteProperty -Name " | Powered Off | " -Value "$PoweredOffVMsCount" $vmObject | Add-Member -MemberType NoteProperty -Name "Suspended" -Value "$SuspendedVMsCount" $CardText = $vmObject | ConvertTo-Html -Fragment # Manipulate the HTML to get desired card layout. $Part1 = $CardText[0] $Part2 = $CardText[3] -replace ("<td>", "<th><h2>") $Part2 = $Part2 -replace ("</td>", "</h2></th>") $Part3 = $CardText[2] -replace ("th", "td") $Part4 = $CardText[4] $FinalCardTextHtml = $Part1 + $Part2 + $Part3 + $Part4 # Build the HTML Card $VMCard = New-ClarityCard -Title VM -Icon vm -IconSize 24 $VMCardBody += Add-ClarityCardBody -CardText "$FinalCardTextHtml" $VMCardBody += Close-ClarityCardBody $VMCard += $VMCardBody $VMCard += Close-ClarityCard -Title "Close VM card" $VMCard } catch { $ErrorMessage = $_.Exception.Message $FailedItem = $_.Exception.ItemName Throw "Get-vCenterVMSet: $ErrorMessage $FailedItem" } } else { # -WhatIf was used. return $false } } Write-Verbose 'Importing from [C:\projects\reportcardps\ReportCardPS\public]' # .\ReportCardPS\public\Get-ReportCardTemplateSet.ps1 function Get-ReportCardTemplateSet { <# .DESCRIPTION Builds HTML Reports using VMware's ClarityUI library. .PARAMETER Path Path to a directory with JSON templates. .EXAMPLE Get-ReportCardTemplateSet .NOTES No notes at this time. #> [CmdletBinding( SupportsShouldProcess = $true, ConfirmImpact = "Low" )] [OutputType([String])] [OutputType([Boolean])] param( [Parameter()][String]$Path ) if ($pscmdlet.ShouldProcess("Starting Get-ReportCardTemplateSet function.")) { try { # If the Path is empty, search the module Root Lib directory. if (!($Path)) { $Path = (Split-Path -Path (Get-Module -ListAvailable ReportCardPS | Sort-Object -Property Version -Descending | Select-Object -First 1).path) $Path = $Path + "\lib" } $TemplateFiles = Get-ChildItem -Path $Path -Recurse -File | Select-Object FullName $TemplateFiles } catch { $ErrorMessage = $_.Exception.Message $FailedItem = $_.Exception.ItemName Throw "Get-ReportCardTemplateSet: $ErrorMessage $FailedItem" } } else { # -WhatIf was used. return $false } } # .\ReportCardPS\public\New-ReportCard.ps1 function New-ReportCard { <# .DESCRIPTION Builds HTML Reports using VMware's ClarityUI library. .PARAMETER Title Title for the document. .PARAMETER JsonFilePath Full Path to the JsonFile. .EXAMPLE New-ReportCard .NOTES No notes at this time. #> [CmdletBinding( SupportsShouldProcess = $true, ConfirmImpact = "Low" )] [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingInvokeExpression", '')] [OutputType([String])] [OutputType([Boolean])] param( [Parameter()][String]$Title, [Parameter()][String]$JsonFilePath ) if ($pscmdlet.ShouldProcess("Starting New-ReportCard function.")) { try { # Validate the Template Json Path if (Test-Path -Path $JsonFilePath) { # Get the New-ClarityDocument HTML $ClarityDocument = New-ClarityDocument -Title "$Title" # Loop through the Json to create the HTML file. $JsonContent = Get-Content -Path $JsonFilePath | ConvertFrom-Json $JsonContent = $JsonContent | Sort-Object -Property Order foreach ($JsonObject in $JsonContent) { # Execute each specified function $CardFunction = $JsonObject.CardFunction $CardArguments = $JsonObject.CardArguments $CardHtml = Invoke-Expression -Command "$CardFunction $CardArguments" $ClarityDocument += $CardHtml } # Close the Document $ClarityDocument += Close-ClarityDocument # Output the data (Allow passthru?) $ClarityDocument } else { Throw "New-ReportCard: Json File not found. $JsonFilePath" } } catch { $ErrorMessage = $_.Exception.Message $FailedItem = $_.Exception.ItemName Throw "New-ReportCard: $ErrorMessage $FailedItem" } } else { # -WhatIf was used. return $false } } Write-Verbose 'Importing from [C:\projects\reportcardps\ReportCardPS\classes]' |