Add Custom Attributes for Notes Annotation
………………
A request to add custom attributes for Virtual Machines when using the fat client. (Web client in 5.1 and 5.5 requires a plugin, see “vsphere-web-client-plugin-for-custom”) 6.0 doesn’t see the attributes in the Web Client, 6.5 does, see the 6.5 KB.
Fields required : Applications, Company Name, Owner, Role, VM Cost
Code Below
Connect-VIServer VC6.test.domain
New-CustomAttribute -Name “Company Name” -TargetType VirtualMachine
New-CustomAttribute -Name “VM Cost” -TargetType VirtualMachine
New-CustomAttribute -Name “Role” -TargetType VirtualMachine
New-CustomAttribute -Name “Owner” -TargetType VirtualMachine
New-CustomAttribute -Name “Applications” -TargetType VirtualMachine
disconnect-VIServer VC6.test.domain -Confirm:$false
………………..
Add the details required
………………..
Add additional code code to add annotation in to the bulk script
$companyname = $item.companyname
$applications = $item.applications
$owner = $item.owner
$role = $item.role
$cost = $item.cost
#Get the Specification and set the Nic Mapping
New-OSCustomizationNicMapping -Spec $custspec -IpMode UseStaticIp –Position 1 -IpAddress $ipaddr -SubnetMask $subnet -DefaultGateway $gateway -Dns $pdns,$sdns
#Create VM using Template with the adjusted Customization Specification
New-VM -Name $vmname -Template $template -Datastore $datastore -VMHost $vmhost -ResourcePool $resourcepool | Set-VM -OSCustomizationSpec $custspec -Confirm:$false
#Set the Network Name
Get-VM -Name $vmname | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $vlan -Confirm:$false
#Set the CPU and Memory
Get-VM -Name $vmname | Set-VM -MemoryGB $ram -NumCPU $cpu -Confirm:$false
#Set some custom attribute fieds
#New-CustomAttribute -Name “VM Cost” -TargetType VirtualMachine
#New-CustomAttribute -Name “Role” -TargetType VirtualMachine
#New-CustomAttribute -Name “Owner” -TargetType VirtualMachine
#New-CustomAttribute -Name “Applications” -TargetType VirtualMachine
#Set annotation value for custom attributes
Set-Annotation -Entity $vmname -CustomAttribute “CompanyName” -Value “$companyname”
Set-Annotation -Entity $vmname -CustomAttribute “Applications” -Value “$applications”
Set-Annotation -Entity $vmname -CustomAttribute “Owner” -Value “$owner”
Set-Annotation -Entity $vmname -CustomAttribute “Role” -Value “$role”
Set-Annotation -Entity $vmname -CustomAttribute “VM Cost” -Value “$cost”
Reports
RV Tools can be used to produce an MS Excel file to output a list of virtual machines and custom annotations RV Tools download
………………
Alternative Report function used
https://psvmware.wordpress.com/tag/get-vm-annotation/
Function Code Below
(greg-get-annotations tested successfully in our lab)……………….
function greg-get-annotations {
<# .DESCRIPTION Greg-get-annotations function stores information about annotation fields for vms in given cluster or in all clusters in VC. It stores the result in an arraylist $vms, you can either create a csv report from this object or display it on screen greg-get-annotations |export-csv -NoTypeInformation c:\file1.csv will export it to csv file etc… greg-get-annotations |format-table VMname,Cluster,CreatedOn,Notes will just display on screen a table with annotations that include : vm name, its cluster and field “CreatedOn” and Notes .PARAMETER clustername Specifies the clustername against wchi report will be built .EXAMPLE greg-get-annotations -clustername ‘cluster01’|Export-Csv c:\annotation-report.csv Will procude report on vms that resides in ‘cluster01’ and store it in csv file .EXAMPLE greg-get-annotations -clustername ‘cluster01’|ft * Will procude report on vms that resides in ‘cluster01’ output it to screen .EXAMPLE greg-get-annotations |Export-Csv c:\annotation-report.csv Will procude report on vms that resides in all clusters and output it to screen .EXAMPLE greg-get-annotations Without specified -clustername switch, it will do report regarding all clusters in VC .NOTES AUTHOR: Grzegorz Kulikowski LASTEDIT: 05/30/2011 #>
param ([string]$clustername)
if(!($clustername)){$clusters=Get-Cluster}else{$clusters=Get-Cluster $clustername}
$VMs=New-Object Collections.ArrayList
foreach ($cluster in $clusters) {
foreach ($vmview in (get-view -ViewType VirtualMachine -SearchRoot $cluster.id)) {
$vm=New-Object PsObject
Add-Member -InputObject $vm -MemberType NoteProperty -Name VMname -Value $vmview.Name
Add-Member -InputObject $vm -MemberType NoteProperty -Name Notes -Value $vmview.Config.Annotation
Add-Member -InputObject $vm -MemberType NoteProperty -Name Cluster -Value $cluster.Name
foreach ($CustomAttribute in $vmview.AvailableField){
Add-Member -InputObject $vm -MemberType NoteProperty -Name $CustomAttribute.Name -Value ($vmview.Summary.CustomValue | ? {$_.Key -eq $CustomAttribute.Key}).value
}
$VMs.add($vm)|Out-Null
}
}
return $VMs
}
greg-get-annotations |Export-Csv c:\annotation-report.csv
……………………………………..
CSV Out Put