Hi
I am trying to import a list of VMs from CSV and retrieve some custom attributes then export results into a CSV.
If I run the below for 1 attribute, it kind of works. I get the results but in a horrible way:
AnnotatedEntityId | AnnotatedEntity | Name | Value | Uid |
VirtualMachine-vm- | my vm | VMOwner | /VIServer= |
$vmlist = Import-Csv "C:\Users\myvms.csv"
ForEach ($vm in $vmlist) {
$vmrow = Get-VM -Name $vm.name
Get-Annotation -Entity $vmrow -CustomAttribute "VMOwner" | Export-Csv C:\Users\myvmsexport.csv -NoTypeInformation -Append
}
I would like to get multiple custom attributes and have it exported in a format like this:
VM | VMOwner | OwnerEmail | ServerType |
3 custom attributes are VMOwner, OwnerEmail and ServerType
Thanks!
Try something like this
$vmlist = Import-Csv "C:\Users\myvms.csv"
$caNames = 'VMOwner', 'OwnerEmail', 'ServerType'
Get-VM -Name $vmlist.Name |
ForEach-Object -Process {
$obj = New-object -TypeName PSObject -Property (
[ordered]@{
VM = $_.Name
VMOwner = ''
OwnerEmail = ''
ServerType = ''
}
)
Get-Annotation -Entity $_ -CustomAttribute $caNames -ErrorAction SilentlyContinue |
ForEach-Object -Process {
$obj."$($_.Name)" = $_.Value
}
$obj
} |
Export-Csv C:\Users\myvmsexport.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try something like this
$vmlist = Import-Csv "C:\Users\myvms.csv"
$caNames = 'VMOwner', 'OwnerEmail', 'ServerType'
Get-VM -Name $vmlist.Name |
ForEach-Object -Process {
$obj = New-object -TypeName PSObject -Property (
[ordered]@{
VM = $_.Name
VMOwner = ''
OwnerEmail = ''
ServerType = ''
}
)
Get-Annotation -Entity $_ -CustomAttribute $caNames -ErrorAction SilentlyContinue |
ForEach-Object -Process {
$obj."$($_.Name)" = $_.Value
}
$obj
} |
Export-Csv C:\Users\myvmsexport.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Amazing...thank you!
So all 3 custom attributes need to be put into a variable first? and to get the exported format, that required something else
To be able to select the correct Custom Attributes, you do need to provide the names somewhere.
I used a Foreach-Object, since this passes the objects over the pipeline, and so it can be exported to a CSV after the Foreach-Object.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
HI
I continue from the same post....
I need a report of "VMs including the value "notes" and including some of the custom attribute.
Some of the custom attribute have an additional value that I created - for example "days" with "number".
if number higher than 30, the VMs Should be on report . If it is below 30 that will not appear in the report.
the table of the report need to be like this
VMname, custom attribute1,custom attribute2,Days,Folder name, Cluster name, Notes, vspherename
thank you