VMware Cloud Community
Hetfield84
Enthusiast
Enthusiast

Select only one result out of many - Get-VIEvent

Hi,

I have a query that looks like this:

$cls = Get-Cluster "clusterName" | Get-VM | Get-VIEvent -Type "Info" | Where "UserName" -eq "username" | Select @{Name='Name';E={$_.vm.name}}, CreatedTime

I'm wondering how to have it only return one event per VM that is created by the user "UserName". If I run the command above it returns several events per VM.

Another question, any idea on how to clear all variables in the ISE? Sometimes i have to close and reopen the ISE to clear some variables as it hangs on to them in memory.

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership

You could do something like this

$userName = 'domain\user'

Get-Cluster "clustername" |

Get-VM |

Get-VIEvent -Type "Info" |

Where {$_.UserName -eq $username} |

Sort-Object -Property CreatedTime -Descending |

Group-Object -Property {$_.VM.Name} |

ForEach-Object -Process {

   $_.Group | Select -First 1 |

  Select @{Name='Name';E={$_.vm.name}}, CreatedTime, UserName

}


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

Reply
0 Kudos
LucD
Leadership
Leadership

Variables that you create in a script should be removed after the script completes.
But not so in ISE I'm afraid.

The 'proper' way to do this is that your script cleans up at the end what it created.

And you can always do

Get-Variable -Name MyVar | Remove-Variable -Confirm:$false


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

Reply
0 Kudos