Wednesday, February 20, 2013

Custom VM report, output to CSV

Note: VC, Template and Description is for manual entry, they don't have any data :)

function Get-myReport {
#sample: get-content test.txt | %{ Get-myReport $_ } | export-csv C:\testout.csv -notypeinformation

param ($VM)
$report = @()
$row = "" | select VC, Cluster, Name, Template, ResourcePool, NumCPU, RAMSizeGB, D_DriveSizeGB, VLAN, Application, BusinessUnit, Category, Support, Description


$v = get-vm $VM
$VLAN = get-virtualportgroup -vm $VM
$app = get-vm -Name $VM | get-annotation | where-object {$_.Name -eq "Application"}
$unit = get-vm -Name $VM | get-annotation | where-object {$_.Name -eq "business unit"}
$category = get-vm -Name $VM | get-annotation | where-object {$_.Name -eq "category"}
$support = get-vm -Name $VM | get-annotation | where-object {$_.Name -eq "support"}
$row.Cluster = $v.vmhost.parent.name
$row.Name = $VM
$row.ResourcePool = $v.resourcepool.name
$row.NumCPU = $v.NumCpu
$row.RAMSizeGB = $v.MemoryMB / 1024
$row.D_DriveSizeGB = ""
$row.VLAN = $VLAN.name
$row.Application = $app.value
$row.BusinessUnit = $unit.value
$row.Category = $category.value
$row.Support = $support.value

$DriveD = Get-mVMdriveD $VM
$row.D_DriveSizeGB = $DriveD.Size

$report += $row

$report

}


function Get-mVMdriveD {
param ($VM)
gwmi -query "SELECT Caption, Size FROM win32_logicaldisk WHERE DriveType=3" -computer $VM | where-object { $_.Caption -eq "D:" } | select-object @{Name="Size"; Expression={"{0:N0}" -f ($_.Size/1GB)}}

}