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
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
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
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