VMware Cloud Community
virtualdive
VMware Employee
VMware Employee
Jump to solution

Task/Events for a ESX or VM: PowerCLI script

Hello All,

Please can someone suggest a script that copies all the Task/Events entries of a VM or ESX to html or excel sheet and mail it. It should be particular server not all servers in vCenter.

Thanks,

Regards,

'V'
thevshish.blogspot.in
vExpert-2014-2021
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try this

$fileName = $env:Temp + '\report.csv' 
$entity
= Get-VM -Name MyVM Get-VIEvent -Entity $entity -MaxSamples 1000 | Select CreatedTime,UserName,FullFormattedMessage | Export-Csv -Path $fileName -NoTypeInformation -UseCulture
Send-MailMessage
-From "report@lucd.info" -To "lucd@lucd.info" `
    -Subject "Events" -Attachments $fileName -SmtpServer "mail.lucd.info"


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

View solution in original post

Reply
0 Kudos
24 Replies
LucD
Leadership
Leadership
Jump to solution

Sure, the basis of such a script would be the Get-VIEvent cmdlet.

In my Events: a great source of information – Part 1 post, you'll find 2 techniques to fetch events.

Are there any specific properties you want to store in the CSV file or in the email body ?

There are quite a few properties available.

The following sample will make this clear

A basic script like this will send you the last 1000 events for a VM

$entity = Get-VM -Name MyV 
$events = Get-VIEvent -Entity $entity -MaxSamples 1000
Send-MailMessage
-From "report@lucd.info" -To "lucd@lucd.info" `
    -Subject "Events" -Body ($events | Out-String) -SmtpServer "mailserver.lucd.info"

If you do a Get-VMHost to fill the $entity variable, you will get the host related events.

But notice that some of the properties are objects themselves, so they will not display nicely in the mail body.

There are solutions for that, but it is handier when you could indicate which properties you want to see.


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

virtualdive
VMware Employee
VMware Employee
Jump to solution

thanks for the reply Luc,

I want both the options actually.

1. With all the the properties.

2. With specific properties.

Cheers!

Regards,

'V'
thevshish.blogspot.in
vExpert-2014-2021
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

For specific properties you add a Select-Object cmdlet.

Something like this

$entity = Get-VM -Name MyVM 
$events = Get-VIEvent -Entity $entity -MaxSamples 100 |
Select CreatedTime,UserName,FullFormattedMessage
Send-MailMessage -From "report@lucd.info" -To "lucd@lucd.info" `
    -Subject "Events" -Body ($events | Out-String) -SmtpServer "mail.lucd.info"

For all the properties it will be a bit more complex.

Some events have information in the Info property that in itself is a complex object.

So a simple 'Select-Object *' will not work for the Info property.

I'm afraid you will have to do these with a Select-Object and specific calculated properties for each type of event.


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

virtualdive
VMware Employee
VMware Employee
Jump to solution

Thanks Luc,

This one is better. Can this be done in the Excel sheet with three different columns and send over email.

Thanks,

Regards,

'V'
thevshish.blogspot.in
vExpert-2014-2021
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You mean export the data to a CSV file and then send the CSV file as an attachment in the email ?


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

Reply
0 Kudos
virtualdive
VMware Employee
VMware Employee
Jump to solution

Yes please Luc,

Regards,

'V'
thevshish.blogspot.in
vExpert-2014-2021
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try this

$fileName = $env:Temp + '\report.csv' 
$entity
= Get-VM -Name MyVM Get-VIEvent -Entity $entity -MaxSamples 1000 | Select CreatedTime,UserName,FullFormattedMessage | Export-Csv -Path $fileName -NoTypeInformation -UseCulture
Send-MailMessage
-From "report@lucd.info" -To "lucd@lucd.info" `
    -Subject "Events" -Attachments $fileName -SmtpServer "mail.lucd.info"


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

Reply
0 Kudos
AmolPatil
Enthusiast
Enthusiast
Jump to solution

Hello,

Can we colleact Task details (Not Events) which has performed on that perticuler ESX host.

Regards, Amol Patil VCP 5
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The UserName property of the TaskEvent contains that user who started a task.


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

Reply
0 Kudos
AmolPatil
Enthusiast
Enthusiast
Jump to solution

Hi,

But not sure from which main syntax need to use. Task related command or property not available.

For example - I need to check one host - when its gone in maintenance mode and by whom.

Is it possible ?

Regards, Amol Patil VCP 5
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The following will show who placed an ESXi server in maintenance mode during the last 2 days.

$start = (Get-Date).AddDays(-2)

Get-VIEvent -Start $start -MaxSamples ([int]::MaxValue) | 
where
{$_ -is [VMware.Vim.TaskEvent] -and $_.Info.DescriptionId -eq "HostSystem.enterMaintenanceMode"} |
Select
CreatedTime,@{N="Host";E={$_.Host.Name}},UserName


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

Reply
0 Kudos
AmolPatil
Enthusiast
Enthusiast
Jump to solution

Hey LucD,

Thanks a lot, Yes its working and same, what we want.

Need assistance - How we can get whatever you used in where {$_ -is [VMware.Vim.TaskEvent] -and $_.Info.DescriptionId

-eq "HostSystem.enterMaintenanceMode"}.

I am beginer in script so here only I stucked that what next we can use or what are possible options available.

Do we have any tool ?? Can you suggest ??

Thanks for your quick support and script.

Regards, Amol Patil VCP 5
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

In the SDK Reference you can find all the types of events that can exist, and the TaskEvent is one of them.

The Get-VIEvent cmdlet returns all events, so we have to filter them with a Where-clause so we only keep the ones we want.

The -is operator allows you to verify the type of an object.

In PowerCLI the framework has defined all the objects, the are in the VMware.Vim namespace.

The TaskEvent is a general event, it will be created for all tasks that are fired. In the Info.descriptionId property we can see the type of task. So we add that to the Where-clause to only get the specific tasks we want.

Afaik there is not really a document descrbing all of this, it's experience I'm afraid.

Looking at other people's script, writing your own scripts and of course raising questions in this community.

So no tool Smiley Sad


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

Reply
0 Kudos
AmolPatil
Enthusiast
Enthusiast
Jump to solution

Sure, It is really helpful.

and yes I will contact you if required any assistance further.

I gone through your sites and books overtheir, which are really helpful.

Thanks a lot.

Regards, Amol Patil VCP 5
Reply
0 Kudos
NagaSGiddaluru
Enthusiast
Enthusiast
Jump to solution

Hello All,

How to get the Task section information rows using PowerCLI,

I am looking to pull the tasks completed / failed for a particular VM / Group of VM's

All, i am seeing Events with start time. I don't see how to pull the Task, Status, Start , End Time , Any issues ?

Any help is highly appreciated.

Thanks,

Naga

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you include the script you are using?

Perhaps it would be more useful to create a new thread with your question.


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

Reply
0 Kudos
NagaSGiddaluru
Enthusiast
Enthusiast
Jump to solution

Get-VM <VM Name> | Get-VIEvent | Where {$_.username -eq "username" -and $_.Info.DescriptionId -eq "task type" -and $_.CreatedTime -gt (Get-Date).AddHours(-24) }

This is to get a particular task type for last 24 hrs.  I am looking how to get the task / event completed time, task status using PowerCLI

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I replied to the thread you created for this question.


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

Reply
0 Kudos
veera1987
Contributor
Contributor
Jump to solution

Hi LuCD,

Request your help in below query, Is there any way to schedule a script so that if any ESXi host made into maintenance mode. An email to be sent automatically to listed users with the Vi event along with user name initiated the maintenance mode for the esxi host.

Thanks in Advance.

Thanks & Regards

S.Veerappan

Reply
0 Kudos