Hello everybody!
I need a liitle powershell script, which is doing the following things.
Get all VM's and when "CustomAttribute" is set to 1, write the Values like CPU, RAM, .... into a csv-File.
I tried it like this:
$VMExport = Get-VM | Get-Annotation -CustomAttribute Export | Where-Object {$_.Value -eq 1 }
But now I have only the Value 1 in the Variable "$VMExport" and cannot use it for further things.
Can anybody help me with that?
The CustomFields property is a hash table, so you can access the values by using the Key as index.
You will have to use a calculated property for that
$VMExport = Get-VM | where {Get-Annotation -Entity $_ -CustomAttribute Export | Where-Object {$_.Value -eq 1 }} | Select Name,ProvisionedSpaceGB,MemoryMB,NumCpu,Description,
@{N="Custom Field";E={$_.CustomFields["CustomFieldName"]}}
This retrieves the value of the Custom Field called "CustomFieldName" and shows it under the "Custom Field" property.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Use the Get-Annotation cmdlet inside the where-clause.
Like this
$VMExport = Get-VM | where {Get-Annotation -Entity $_ -CustomAttribute Export | Where-Object {$_.Value -eq 1 }} | Select Name
Update the Select cmdlet at the end to include the properties of the VM that you want
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for your help!
It was very helpful, but now I'm stuck again.
I don't know how to get all the attributes which I need to have in the csv-file
I need the following things:
You get VirtualMachineImpl objects back with my line.
Try
$VMExport = Get-VM | where {Get-Annotation -Entity $_ -CustomAttribute Export | Where-Object {$_.Value -eq 1 }} | Select Name,ProvisionedSpaceGB,MemoryMB,NumCpu,Description
It should produce most of the data you want.
If you want to see what is returned or what properties are available, do a Get-Member
Get-VM | where {Get-Annotation -Entity $_ -CustomAttribute Export | Where-Object {$_.Value -eq 1 }} | Get-Member
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, you're right.
But how do I get my CustomAttributes within this line?
The CustomFields property is a hash table, so you can access the values by using the Key as index.
You will have to use a calculated property for that
$VMExport = Get-VM | where {Get-Annotation -Entity $_ -CustomAttribute Export | Where-Object {$_.Value -eq 1 }} | Select Name,ProvisionedSpaceGB,MemoryMB,NumCpu,Description,
@{N="Custom Field";E={$_.CustomFields["CustomFieldName"]}}
This retrieves the value of the Custom Field called "CustomFieldName" and shows it under the "Custom Field" property.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference