This time i wanted to build a report to check which vms are missing the notes/description section. If you have large virtual infrastructure it is very handy to have information about vm, what is its role. In case i don’t have notes/description field in VM, i would go to AD and check if the object has description. I would also like to know who to contact in case it does not have the description i will put data from ManagedBy field in AD. I would also like to know what operating system the vm has, and if it is powered on or off. At the end, i want to have a csv file with this information, so i can quickly edit it in Excel, or other spreadsheet sotware, apply some filters, conditional formatting and so on. It could be done also via powershell using the excel object for instance, and generating data directly to excel spreadsheet but it would take more time to write this at this point for me than applying filters and conditional formatting(1 minute).
[sourcecode language=”powershell”]
$(foreach ($cluster in get-cluster) { get-view -viewtype VirtualMachine -SearchRoot $cluster.id | select @{N="VM name";E={$_.Name} },@{N=
"PowerState";E={$_.Summary.Runtime.PowerState}} ,@{N="Guest OS";E={$_.Config.GuestFullName} } , @{N="Cluster";E={$cluster.name} },@{N="VM .Notes";E={$_.Summary.
Config.Annotation} }, @{N="AD Description";E={(Get-QADComputer $_.Name).Description} },@{N="AD ManagedBy";E={((Get-QADComputer $_.name).ManagedBy | Get-QADUser)
.DisplayName} } }) | export-csv -NoTypeInformation c:\report.csv
[/sourcecode]
I am pulling data cluster after cluster:
foreach ($cluster in get-cluster)
Pulling data for virtual machines in that particular cluster:
get-view -viewtype VirtualMachine -SearchRoot $cluster.id
Building custom colums :
select @{N=”VM name”;E={$_.Name} -> this should be self explanatory, at this moment i am in a loop where i am receiving vms for cluster, so $_ is the view for VM, $_.Name is the name field in the vm view
I want to have in report the powerstate information :
@{N="PowerState";E={$_.Summary.Runtime.PowerState}}
I want to have information about the operating system:
@{N="Guest OS";E={$_.Config.GuestFullName} }
Information about in which cluster vm resides :
@{N="Cluster";E={$cluster.name} }
Finally i want to see if the notes have been filled out or not:
@{N="VM .Notes";E={$_.Summary.Config.Annotation} }
In case the notes section is not filled out, i want to see what’s in AD description field:
@{N="AD Description";E={(Get-QADComputer $_.Name).Description} }
If that description would be empty i would like to know who to contact in order to get this information so:
@{N="AD ManagedBy";E={((Get-QADComputer $_.name).ManagedBy | Get-QADUser).DisplayName} }
At the end i close whole output $() in sub-expression to pipe it to export-csv
I was using here Quest cmdlets to query AD. You can download it for free at
http://www.quest.com/powershell/activeroles-server.aspx
Make sure that if you are using them in a script you will add them first by using this:
Add-PSSnapin Quest.ActiveRoles.ADManagement
Result: