PowerCLI Owner Tag Report

Got a request from management to create a report that would break out vm’s per owner. They also wanted to separate Prod vs nonprod. This is what I came up with. Feel free to use it. I’m a below average programmer at best, so I thought I’d see if you guys see any glaring improvements. I want to make it a function so I can do one user at a time, but this is as far as I’ve gotten.

One thing I can’t figure out is this spits out two tables, prod and non prod. Prod has no border, but nonprod has a border. Am I missing something simple?

 $a = "<style>" $a = $a + "BODY{background-color:AZURE;}" $a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}" $a = $a + "TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:LIGHTSKYBLUE}" $a = $a + "TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:WHITESMOKE}" $a = $a + "</style>" $message = "INSERT MESSAGE HERE" #Connect-VIserver -server MYVCEN01 -SaveCredentials $date = Get-Date -Format "MM_dd_yyyy" New-Item "C:PowershellVMware Owner Reports $date" -ItemType Directory $ownerslist = @() $from = "NoReply@MYCOMPANY.com" #Populate a list of owners $owners = Get-Tag -Category Owner | select Name -ExpandProperty Name | where {$_.Name -notlike "$null"} | sort Name -Unique #Cycle through all servers for each owner foreach ($owner in $owners) { $vms = get-vm -Tag $owner foreach ($vm in $vms) { $object = [pscustomobject]@{ Owner = $owner Name = $vm.name #PowerState = $vm.powerstate NumCpu = $vm.numcpu RamGB = [int]$vm.memorygb DiskSpaceGB = [int]$vm.ProvisionedSpaceGB Environment = (Get-TagAssignment -Entity $vm -Category ServerEnvironment).Tag.Name Backup = (Get-TagAssignment -Entity $vm -Category Backup).Tag.Name Replication = (Get-TagAssignment -Entity $vm -Category Replication).Tag.Name BusinessLine = (Get-TagAssignment -Entity $vm -Category BusinessLine).Tag.Name #Datastore = (Get-Datastore -Id $vm.DatastoreIdList).Name -join ',' } #add object to variable $ownerslist += $object } #get prod servers from specific owner $prodservs = $ownerslist | where {$_.Owner -like $owner -and $_.Environment -like "PROD"} | Sort Name if ($prodservs) { $prodservs += [pscustomobject]@{ #Owner = "" Name = "Total" NumCpu = $prodservs | Measure-Object -Property NumCpu -Sum | Select -ExpandProperty Sum RamGB = $prodservs | Measure-Object -Property RamGb -Sum | Select -ExpandProperty Sum DiskSpaceGB = $prodservs | Measure-Object -Property DiskSpaceGB -Sum | Select -ExpandProperty Sum Environment = "" Backup = "" Replication = "" BusinessLine = "" } $prodservs | ConvertTo-Html -Body $a | ft -AutoSize | Out-File "C:PowershellVMware Owner Reports $date$owner Production.html" } #get dev servers from specific owner $nonprodservers = $ownerslist | where {$_.Owner -like $owner -and $_.Environment -notlike "PROD"} | Sort Environment, Name if ($nonprodservers) { $nonprodservers += [pscustomobject]@{ #Owner = "" Name = "Total" NumCpu = $nonprodservers | Measure-Object -Property NumCpu -Sum | Select -ExpandProperty Sum RamGB = $nonprodservers | Measure-Object -Property RamGb -Sum | Select -ExpandProperty Sum DiskSpaceGB = $nonprodservers | Measure-Object -Property DiskSpaceGB -Sum | Select -ExpandProperty Sum Environment = "" Backup = "" Replication = "" BusinessLine = "" } $nonprodservers | ConvertTo-Html -Body $a | ft -AutoSize | Out-File "C:PowershellVMware Owner Reports $date$owner NonProduction.html" } #create body of email (Pulls it from the html file i create) $body = @() $body = "<br><br><b>Production</b> servers account for the following resources: <br>" $prodemail = Get-Content "C:PowershellVMware Owner Reports $date$owner Production.html" $body = $body + "$prodemail <br>" $body += "<b>Non Production</b> servers account for the following resources: <br>" $nonprodemail = Get-Content "C:PowershellVMware Owner Reports $date$owner NonProduction.html" $body = $body + "$nonprodemail <br>" #Get Email $email = Get-Tag -Category Owner -Name $owner | select Description -ExpandProperty Description | sort Description -Unique #Send email with attachments Send-MailMessage -from $from -to $email -subject "$owner Report" -BodyAsHtml -body "$message $body" -smtpServer 1.2.3.4 } 

submitted by /u/sphinxpup
[link] [comments]

Leave a Reply