Hi Experts,
Need assistance getting the values of annotation custom attribute below is my script. No problem gathering other informations.
listed below is the codes where im having issues. there is no output for the VM Notes and Product.
@{N='VM Notes';E={$_.Summary.Config.Annotation}},
@{N="Product";E={$key = $_.AvailableField | where {$_.Name -eq "Product"} | Select -ExpandProperty Key
$_.CustomValue | where {$_.Key -eq $key} | Select -ExpandProperty Value}},
Listed below is the whole script where the code above is integrated.
$vCenterServerName = "test.com"
Connect-VIServer $vCenterServerName -User xxx -Password xxxx
$SMTPServer = "xxxx"
$ToAddress = "xxxxx"
$FromAddress = $vCenterServerName + "@xxx.com"
$Subject = "VMPath Report for ALL VMs in vcenter-tpe.corp.kazootek.com"
$date = Get-Date -format M-d-yyyy
$reportlocation = “C:\test\vcenter-xxx-vmpath-$date.csv”
$vmFields = 'Name','Parent','Config.Template','Runtime.Host','ResourcePool','Config.Hardware','Summary.Storage','Runtime.Powerstate','Summary.Config.Annotation'
# $report = C:\test\tpe_mate1.csv -UseCulture | %{
$report = Get-View -ViewType VirtualMachine -Property $vmFields | %{
$esx = Get-View -id $_.Runtime.Host -Property Name,Hardware
$_ | Select Name,
@{N='VM Notes';E={$_.Summary.Config.Annotation}},
@{N="Product";E={$key = $_.AvailableField | where {$_.Name -eq "Product"} | Select -ExpandProperty Key
$_.CustomValue | where {$_.Key -eq $key} | Select -ExpandProperty Value}},
@{N='Template';E={$_.Config.Template}},
@{N='Datacenter';E={
$obj = Get-View -Id $_.ResourcePool -Property Name,Parent
while ($obj -isnot [VMware.Vim.Datacenter]){
$obj = Get-View $obj.Parent -Property Name,Parent
}
$obj.Name
}},
@{N='Cluster';E={
$obj = Get-View -Id $_.ResourcePool -Property Name,Parent
while ($obj -isnot [VMware.Vim.ClusterComputeResource]){
$obj = Get-View $obj.Parent -Property Name,Parent
}
$obj.Name
}},
@{N='Host';E={$esx.Name}},
@{N='NumCpu';E={$_.Config.Hardware.NumCpu}},
@{N='MemoryGB';E={[int]($_.Config.Hardware.MemoryMB/1KB)}},
@{N="ProvisionedSpaceGB"; E={($_.Summary.Storage.Committed+$_.Summary.Storage.Uncommitted)/1GB}},
@{N='Powerstate';E={$_.Runtime.PowerState}},
@{N='Path';E={
$current = Get-View -Id $_.Parent -Property Name,Parent
$path = $_.Name
do {
$parent = $current
if($parent.Name -ne "vm"){$path = $parent.Name + "\" + $path}
$current = Get-View $current.Parent -Property Name,Parent
} while ($current.Parent -ne $null)
[string]::Join('\',($path.Split('\')[0..($path.Split('\').Count-2)]))
}},
@{N='FolderId';E={$_.Parent.ToString()}},
@{N='Manufacturer'; E={$esx.Hardware.SystemInfo.Vendor}},
@{N='Model'; E={$esx.Hardware.SystemInfo.Model}},
@{N='ProcessorType'; E={$esx.Hardware.CpuPkg[0].Description}}
}
# }
$report | Export-Csv $reportlocation -NoTypeInformation -UseCulture
Write-Host "Report has exported to csv file " + $reportlocation
Send-Mailmessage -From $FromAddress -To $ToAddress -Subject $Subject -Attachments $reportlocation -SmtpServer $SMTPServer
Write-Host "Report has been sent by E-mail to " $ToAddress " from " $FromAddress
You need to add AvailableField and CustomValue to the $vmFields variable
$vmFields = 'Name','Parent','Config.Template','Runtime.Host','ResourcePool','Config.Hardware','Summary.Storage',
'Runtime.Powerstate','Summary.Config.Annotation','AvailableField','CustomValue'
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You need to add AvailableField and CustomValue to the $vmFields variable
$vmFields = 'Name','Parent','Config.Template','Runtime.Host','ResourcePool','Config.Hardware','Summary.Storage',
'Runtime.Powerstate','Summary.Config.Annotation','AvailableField','CustomValue'
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Luc.
