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
$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】
EventTime | UserName | VM | Applicant | Application | ComputeResource | Host | Dvs | Description |
---|---|---|---|---|---|---|---|---|
2020/6/21 23:49 | vsphere.local\xx | VM1 | Cluster1 | host1 | DVS1 | Task: 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 :
EventTime | UserName | VM | Applicant | Application | ComputeResource | Host | Dvs | Description |
---|---|---|---|---|---|---|---|---|
2020/6/21 23:49 | vsphere.local\xx | VM1 | YYY | apache1 | Cluster1 | host1 | DVS1 | Task: Set virtual machine custom value |
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
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
}
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
$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
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:
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
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:
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
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
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
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