VMware Cloud Community
Yyy_wanna
Contributor
Contributor
Jump to solution

Get-Annotation if vm is not null

Hi,

     Being puzzled by "get-annotation", I don't know how to use this function correctly.

     Now, I'm  trying to write scripts that I wanna get users events , and  get the custom attribute of vm if have. But "get-annotation" can not return anything

This is my unfinished  scripts  Smiley Sad

$events = Get-VIEvent -MaxSamples 200 -Start 6/19/2020 -Finish 6/22/2020

$eventCollection =@()

foreach($event in $events){

    #exclude "null" user

    if( $event.UserName -match "vsphere*"){

    $csvline = "" | Select EventTime,UserName,VM,Applicant,Application,ComputeResource,Host,Dvs,Description

    $csvline.EventTime = $event.CreatedTime

    $csvline.UserName = $event.UserName

    $csvline.VM = $event.VM.Name

     ####  confusion ### This !!!   #####

     if($csvline.VM.Name -ne $null ){

     $csvline.Applicant  = Get-Vm $event.VM.Name | get-Annotation -CustomAttribute "Applicant" |select -ExpandProperty value

     $csvline.Application= Get-Vm $event.VM.Name | get-Annotation -CustomAttribute "Application" |select -ExpandProperty value

     }

     #####

    $csvline.ComputeResource = $event.ComputeResource.Name

    $csvline.Host = $event.Host.Name

    $csvline.Dvs = $event.Dvs.Name

    $csvline.Description = $event.FullFormattedMessage

    $eventCollection += $csvline

    }

}

$eventCollection  | Export-Csv E:\test.csv -NoTypeInformation -Encoding UTF8

Now :   【Applicant and Application  are null   --------how to apply it】 

EventTimeUserNameVMApplicantApplicationComputeResourceHostDvsDescription
2020/6/21 23:49vsphere.local\xxVM1Cluster1host1
DVS1Task: Set virtual machine custom value

【get-vm | get-Annotation -CustomAttribute "Application" |select -ExpandProperty value】

:

flannel-test_01

flannel-test_03

flannel-test_02

Expect input :

EventTimeUserNameVMApplicantApplicationComputeResourceHostDvsDescription
2020/6/21 23:49vsphere.local\xxVM1YYYapache1Cluster1host1
DVS1Task: Set virtual machine custom value

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

That was my reply, but I'm afraid that

if($csvline.VM = $event.VM.Name){

should be

if($csvline.VM -eq $event.VM.Name){


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

View solution in original post

0 Kudos
9 Replies
Yyy_wanna
Contributor
Contributor
Jump to solution

Parameter validation cannot be performed on parameter 'Name'.The argument is Null or Null.Provide a parameter that is not Null or empty, and then try the command again.

Location row:12 string: 36

+      $csvline.Applicant  = Get-Vm  $csvline.VM  | get-Annotation -Cus ...

+                                    ~~~~~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Get-VM],ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

I find that's some errors in the  judgment statement, how could I fix the problem

if($csvline.VM.Name -ne $null ){

     $csvline.Applicant  = Get-Vm $event.VM.Name | get-Annotation -CustomAttribute "Applicant" |select -ExpandProperty value

     $csvline.Application= Get-Vm $event.VM.Name | get-Annotation -CustomAttribute "Application" |select -ExpandProperty value

     }

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The object $csvline that you are building is a flat object, in other words, there are no nested objects in there, only scalars in the properties.
The $event object on the other hand does have nested objects.

Try with

if($csvline.VM -ne $null ){

    $csvline.Applicant  = Get-Vm $event.VM.Name | get-Annotation -CustomAttribute "Applicant" |select -ExpandProperty value

    $csvline.Application= Get-Vm $event.VM.Name | get-Annotation -CustomAttribute "Application" |select -ExpandProperty value

}


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

0 Kudos
Yyy_wanna
Contributor
Contributor
Jump to solution

hhhhhh    I succeed:)

Finnally:

if($csvline.VM = $event.VM.Name){

     $csvline.Applicant  = Get-Vm $event.VM.Name | get-Annotation -CustomAttribute "Applicant" |select -ExpandProperty value

     $csvline.Application= Get-Vm $event.VM.Name | get-Annotation -CustomAttribute "Application" |select -ExpandProperty value

     }

But it's too slow ,this is an another question:smileyangry:

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That was my reply, but I'm afraid that

if($csvline.VM = $event.VM.Name){

should be

if($csvline.VM -eq $event.VM.Name){


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

0 Kudos
Yyy_wanna
Contributor
Contributor
Jump to solution

Thanks LucD. It's working.

But I find that if the amount of events is increased, such as one day events, in other words,  I remove  "-MaxSamples 200"  parameter,

$events = Get-VIEvent  -Start 6/21/2020 -Finish 6/22/2020

it's too slow .And I have to try another method:smileyconfused:

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Unfortunately, Get-VIEvent is very slow, but that is a vCenter issue, not a PowerCLI issue.


Btw, I'm not sure why you are using events in this script.
You seem to take any event, and then take the involved VM (if any) to get further info (like Annotation) from that VM.

What is the purpose of your script?


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

0 Kudos
Yyy_wanna
Contributor
Contributor
Jump to solution

My leader wanna get a record of the administrator's daily operations ,especially vms, so he can develop the control and change process. In fact, i don't know why he wants the vms information Smiley Sad

0 Kudos
Yyy_wanna
Contributor
Contributor
Jump to solution

if($csvline.VM = $event.VM.Name){

It's two point.

Firstly, it runs as $csvline.VM = $event.VM.Name, so  I can get its csvline.VM value;

Secondly,the result of  it  is decided whether excutes the following statement

     $csvline.Applicant  = Get-Vm $event.VM.Name | get-Annotation -CustomAttribute "Applicant" |select -ExpandProperty value

     $csvline.Application= Get-Vm $event.VM.Name | get-Annotation -CustomAttribute "Application" |select -ExpandProperty value

or not

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can't use the assignment operator (=) in a condition.

That should be the operator -eq (like I said earlier).

The assignments inside the if-block are correct


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

0 Kudos