VMware Cloud Community
lp1504
Contributor
Contributor
Jump to solution

Get VM annotations in a spreadsheet

Hi All,

Just need a quick view on how to get the annotations in a spreadsheet from their respective VMs. We know how to get the annotation but are looking for a way to get a cleaner output

Get-Datastore -Name test*| Where {$_.Name -notmatch "test1"} | Get-VM | Get-Annotation

test2   Environment          QA

test2   Function             Some description

test2   License Host         NA

test2   Project             Some description

test2   Requestor            Some name

We are interested in only getting the environment, function, project and requestor in the spreadsheet.

Thx,

L

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You are overwriting the $report variable inside the loop, you should add the object for each datastore.

Try like this

$attrNames = 'Environment','Function','Project','Requestor'

$ca = Get-CustomAttribute -Name $attrNames

$dsN = Get-Content -Path "C:\DatastoreList.txt"

$report = @()

ForEach ($ds in $dsN)

{

$report += Get-VM -Datastore $ds | Get-Annotation -CustomAttribute $ca |

    Sort-Object -Property AnnotatedEntity,Name |

    Group-Object -Property AnnotatedEntity | %{

        $props = New-Object PSObject -Property @{

            VM = $_.Name

        }

        $_.Group | %{

            $props | Add-member -Name $_.Name -Value $_.Value -MemberType NoteProperty

        }

        $props

    }

}

$report | Export-Csv -Path "C:\VMAnnotationReport.csv" -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
25 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this

$attrNames = 'Environment','Function','Project','Requestor'

$ca = Get-CustomAttribute -TargetType VirtualMachine -Name $attrNames

$ds = Get-Datastore -Name MyDS

$report = Get-VM -Datastore $ds | Get-Annotation -CustomAttribute $ca |

Sort-Object -Property AnnotatedEntity,Name |

Group-Object -Property AnnotatedEntity | %{

    $props = [ordered]@{

        VM = $_.Name

    }

    $_.Group | %{

        $props.Add($_.Name,$_.Value)

    }

    New-Object PSObject -Property $props

}

$report | Export-Csv report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
lp1504
Contributor
Contributor
Jump to solution

Thanks Luc. I was going through one of your other post when I saw your reply Smiley Happy I modified my command to include

select -property @{N="Name";E="Value"}

but was not getting the desired output. Will execute your script and udpate the results.

0 Kudos
lp1504
Contributor
Contributor
Jump to solution

Hi Luc,

I tweaked your script to include only the list of datastores but getting error during it execution

Script:

$attrNames = 'Environment','Function','Project','Requestor'

$ca = Get-CustomAttribute -TargetType VirtualMachine -Name $attrNames

$datastores = Get-Content -Path "C:\DatastoreList.txt"

#$ds = Get-Datastore -Name MyDS

ForEach ($dsName in $datastores)

{

$ds = Get-Datastore -Name $dsName

$report = Get-VM -Datastore $ds | Get-Annotation -CustomAttribute $ca |

Sort-Object -Property AnnotatedEntity,Name |

Group-Object -Property AnnotatedEntity | %{

    $props = [ordered]@{

        VM = $_.Name

    }

    $_.Group | %{

        $props.Add($_.Name,$_.Value)

    }

    New-Object PSObject -Property $props

}

}

$report | Export-Csv -Path "C:\Annotationreport.csv" -NoTypeInformation -UseCulture

Error:

Get-CustomAttribute : 11/23/2015 3:59:12 PM    Get-CustomAttribute        CustomAttribute with name 'Environment' was not found using the specified filter(s).

At C:\Get_VMAnnotation.ps1:2 char:26

+ $ca = Get-CustomAttribute <<<<  -TargetType VirtualMachine -Name $attrNames

    + CategoryInfo          : ObjectNotFound: (:) [Get-CustomAttribute], VimException

    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetCustomAttribute

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Looks like there is no custom attribute with the name Environment.

Does the output from the following line contain an entry with the name Environment ?

Get-CustomAttribute -TargetType VirtualMachine


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
lp1504
Contributor
Contributor
Jump to solution

Yes was checking the same but it appears the value do not corelate

PowerCLI C:> Get-CustomAttribute -TargetType VirtualMachine

Key   Name                 TargetType

---   ----                 ----------

202   com.vmware.vdr.is... VirtualMachine

201   com.vmware.vdr.pr... VirtualMachine

Get-Datastore -Name test*| Where {$_.Name -notmatch "test1"} | Get-VM | Get-Annotation

AnnotatedEntity Name         Value

--------------- ----         -----

test2   Environment           QA

test2   Function             Some description

test2   License Host         NA

test2   Project               Some description

test2   Requestor             Some name

test2   com.vmware.vdr.is...

test2   com.vmware.vdr.pr...

0 Kudos
lp1504
Contributor
Contributor
Jump to solution

Just to add these are custom annotations and their type set is "Global"

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you do a Get-CustomAttribute without any parameters.

I think the custom attribute might not be assigned to the VirtualMachine type


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
lp1504
Contributor
Contributor
Jump to solution

I believe it isn't

PowerCLI C:> Get-CustomAttribute

Key   Name                 TargetType

---   ----                 ----------

3     Environment

1     Function

5     License Host

4     Project

2     Requestor

202   com.vmware.vdr.is... VirtualMachine

207   com.vmware.vdr.jo...

201   com.vmware.vdr.pr... VirtualMachine

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, in that case, leave out the TargetType parameter on the Get-CustomAttribute cmdlet.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
lp1504
Contributor
Contributor
Jump to solution

Yes, just did that and it seems to be working..will udpate in a minute

0 Kudos
lp1504
Contributor
Contributor
Jump to solution

Unfortunately it doesn't, the screen is red with errors!

New-Object : Cannot validate argument on parameter 'Property'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.

At C:Get_VMAnnotation.ps1:17 char:34

+     New-Object PSObject -Property <<<<  $props

    + CategoryInfo          : InvalidData: (:) [New-Object], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.NewObjectCommand

Unable to find type [ordered]: make sure that the assembly containing this type is loaded.

At C:Get_VMAnnotation.ps1:11 char:23

+     $props = [ordered] <<<< @{

    + CategoryInfo          : InvalidOperation: (ordered:String) [], RuntimeException

    + FullyQualifiedErrorId : TypeNotFound

You cannot call a method on a null-valued expression.

At C:\Get\Get_VMAnnotation.ps1:15 char:19

0 Kudos
LucD
Leadership
Leadership
Jump to solution

It seems you are not using PowerShell v3, or higher.

The [ordered] cast was introduced in PowerShell v3.

Can you check by displaying the content of $PSVersionTable ?

And can you upgrade your PowerShell version ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
lp1504
Contributor
Contributor
Jump to solution

Indeed we are running on PS 2 and it would be really difficult to get PS 3 until early next year (all process, compliance and things) Smiley Sad Is there a dirty way to get in out of P2?

Thx,

L

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

$attrNames = 'Environment','Function','Project','Requestor'

$ca = Get-CustomAttribute -Name $attrNames

$ds = Get-Datastore -Name MyDS

$report = Get-VM -Datastore $ds | Get-Annotation -CustomAttribute $ca |

Sort-Object -Property AnnotatedEntity,Name |

Group-Object -Property AnnotatedEntity | %{

    $props = New-Object PSObject -Property @{

        VM = $_.Name

    }

    $_.Group | %{

        $props.Add($_.Name,$_.Value)

    }

    $props

}

$report | Export-Csv report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
lp1504
Contributor
Contributor
Jump to solution

Still  the same

Method invocation failed because [System.Management.Automation.PSCustomObject] doesn't contain a method named 'Add'.

At C:Get_VMAnnotationMod.ps1:15 char:19

+         $props.Add <<<< ($_.Name,$_.Value)

    + CategoryInfo          : InvalidOperation: (Add:String) [], RuntimeException

    + FullyQualifiedErrorId : MethodNotFound

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Last option, try like this

$attrNames = 'Environment','Function','Project','Requestor'

$ca = Get-CustomAttribute -TargetType VirtualMachine -Name $attrNames

$ds = Get-Datastore -Name MyDS

$report = Get-VM -Datastore $ds | Get-Annotation -CustomAttribute $ca |

Sort-Object -Property AnnotatedEntity,Name |

Group-Object -Property AnnotatedEntity | %{

    $props = New-Object PSObject -Property @{

        VM = $_.Name

    }

    $_.Group | %{

        $props | Add-member -Name $_.Name -Value $_.Value -MemberType NoteProperty

    }

    $props

}

$report | Export-Csv report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
lp1504
Contributor
Contributor
Jump to solution

Hi Luc,

Thanks a ton , it works! Tweaked it a little bit. But is now only providing output from the last datastore in the list. We have almost 6 datastores but the CSV file generated contains VM only from the last DS. Something incorrent with the placement of $reports in the code

Script:

$attrNames = 'Environment','Function','Project','Requestor'

$ca = Get-CustomAttribute -Name $attrNames

$dsN = Get-Content -Path "C:\DatastoreList.txt"

ForEach ($ds in $dsN)

{

$report = Get-VM -Datastore $ds | Get-Annotation -CustomAttribute $ca |

Sort-Object -Property AnnotatedEntity,Name |

Group-Object -Property AnnotatedEntity | %{

    $props = New-Object PSObject -Property @{

        VM = $_.Name

    }

    $_.Group | %{

        $props | Add-member -Name $_.Name -Value $_.Value -MemberType NoteProperty

    }

    $props

}

}

$report | Export-Csv -Path "C:\VMAnnotationReport.csv" -NoTypeInformation -UseCulture

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You are overwriting the $report variable inside the loop, you should add the object for each datastore.

Try like this

$attrNames = 'Environment','Function','Project','Requestor'

$ca = Get-CustomAttribute -Name $attrNames

$dsN = Get-Content -Path "C:\DatastoreList.txt"

$report = @()

ForEach ($ds in $dsN)

{

$report += Get-VM -Datastore $ds | Get-Annotation -CustomAttribute $ca |

    Sort-Object -Property AnnotatedEntity,Name |

    Group-Object -Property AnnotatedEntity | %{

        $props = New-Object PSObject -Property @{

            VM = $_.Name

        }

        $_.Group | %{

            $props | Add-member -Name $_.Name -Value $_.Value -MemberType NoteProperty

        }

        $props

    }

}

$report | Export-Csv -Path "C:\VMAnnotationReport.csv" -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
lp1504
Contributor
Contributor
Jump to solution

Thanks much Luc, totally missed it. Works as expected now! Can we also include the comments from "Notes" section in here?

0 Kudos