Hi Guys,
I wonder if you can help. I have a custom field setup on our vm's called Owner which unsurprisingly has the name of the vms owner.
I would like to create a report that shows the following;
Virtual Machine Name Owner Cluster Datastore
TEST1 SEAN DATACENTRE1 STORE1
I want to be able to run this against a set of datastores, e.g.
get-datastore -name dc1 | $report
And ideally output it as a csv.
I've seen a couple of other scripts on the forums but nothing that just pulls a single custom field or that can format it with the other data I was after.
Anyone know how to write this?
Many thanks,
Sean.
Hi Sean,
the next script gives you the desired information for all datastores:
Get-Datastore | ForEach-Object { $Datastore = $_ $Datastore | Get-VM | ForEach-Object { $VM = $_ $Report = "" | Select-Object VirtualMachine,Owner,Cluster,Datastore $Report.VirtualMachine = $VM.Name $Report.Owner = (Get-Annotation -Entity $VM -CustomAttribute Owner).value $Report.Cluster = (Get-Cluster -VM $VM).Name $Report.Datastore = $Datastore.Name $Report } }
If you want the information for a specific datastore, you can add the name of the datastore in the first line after the Get-Datastore cmdlet.
If you want to output the information into a .csv file you can change the last line of the script into:
} | Export-Csv -Path Datastores.csv -NoTypeInformation
Regards, Robert
Message was edited by: RvdNieuwendijk
Hi Robert,
That is absolutely excellent, thanks very much for that it is a lifesaver!!
Sean.