VMware Cloud Community
RyanMcL
Enthusiast
Enthusiast
Jump to solution

Script not working as expected

Script should auto set annotations for me, it was taken directly from "VMWare vSphere PowerCLI Reference" Chapter 5

$VM = Get-VM "test"

    Get-VIEvent -Entity $VM -Types Info |

        Where-Object { $_.Gettype().Name -match "VmBeingDeployedEvent|VmCreatedEvent|VmRegisteredEvent|VmClonedEvent"} |

        ForEach-Object {

             Set-Annotation -Entity $VM -CustomAttribute CreatedBy -Value $Event.UserName

             Set-Annotation -Entity $VM -CustomAttribute CreatedOn -Value $Event.CreatedTime

        }

This is the Error:

Set-Annotation : Cannot validate argument on parameter 'Value'. The argument is null or empt

y. Supply an argument that is not null or empty and then try the command again.

At Set Custom vars.ps1:8 char:15

+         -Value <<<<  $Event.UserName

    + CategoryInfo          : InvalidData: (:) [Set-Annotation], ParameterBindingValidation

   Exception

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.

   Cmdlets.Commands.SetAnnotation

Set-Annotation : Cannot validate argument on parameter 'Value'. The argument is null or empt

y. Supply an argument that is not null or empty and then try the command again.

At Set Custom vars.ps1:11 char:15

+         -Value <<<<  $Event.CreatedTime

    + CategoryInfo          : InvalidData: (:) [Set-Annotation], ParameterBindingValidation

   Exception

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.

   Cmdlets.Commands.SetAnnotation

However a cut down version of the script:

$VM = Get-VM "test"

Get-VIEvent -Entity $VM -Types Info |

  Where-Object { $_.Gettype().Name -match "VmBeingDeployedEvent|VmCreatedEvent|VmRegisteredEvent|VmClonedEvent"}

Returns:

Template             : False

Key                  : 1022221

ChainId              : 1022221

CreatedTime          : 17/06/2011 10:52:15

UserName             : EXAMPLE\Me

Datacenter           : VMware.Vim.DatacenterEventArgument

ComputeResource      : VMware.Vim.ComputeResourceEventArgument

Host                 : VMware.Vim.HostEventArgument

Vm                   : VMware.Vim.VmEventArgument

Ds                   :

Net                  :

Dvs                  :

FullFormattedMessage : Created virtual machine test on vm6.example.com

ChangeTag            :

DynamicType          :

DynamicProperty      :

I don't know if it makes a difference but I am running powerCLI 4.1u1 and vSphere 4.0

Anyone know why?

EDIT: running dir variable: dosent seem to show $Event as existing, $VM is there

Reply
0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

You should replace $Event with $_ because you are getting the value from the pipeline. Like this:

$VM = Get-VM "test"
Get-VIEvent -Entity $VM -Types Info | 
  Where-Object { $_.Gettype().Name -match "VmBeingDeployedEvent|VmCreatedEvent|VmRegisteredEvent|VmClonedEvent"} |
  ForEach-Object {
    Set-Annotation -Entity $VM -CustomAttribute CreatedBy -Value $_.UserName
    Set-Annotation -Entity $VM -CustomAttribute CreatedOn -Value $_.CreatedTime
}

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

Reply
0 Kudos
2 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

You should replace $Event with $_ because you are getting the value from the pipeline. Like this:

$VM = Get-VM "test"
Get-VIEvent -Entity $VM -Types Info | 
  Where-Object { $_.Gettype().Name -match "VmBeingDeployedEvent|VmCreatedEvent|VmRegisteredEvent|VmClonedEvent"} |
  ForEach-Object {
    Set-Annotation -Entity $VM -CustomAttribute CreatedBy -Value $_.UserName
    Set-Annotation -Entity $VM -CustomAttribute CreatedOn -Value $_.CreatedTime
}

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
RyanMcL
Enthusiast
Enthusiast
Jump to solution

You are correct. I am sure I had tried that and got an error.. Maybe it was  connection problem that time and I didn't read the error properly assuming it was the same one.

Thanks.

Ryan

Reply
0 Kudos