VMware Cloud Community
andrevechiatto
Enthusiast
Enthusiast
Jump to solution

Create realtime log after poweroff vms

I create script to poweroff vms. I would record real-time in a excel or html file which were vms poweroff to follow. Is possible?

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I think you would be better examining the Events for obtaining the start and stop times, i.e. the Poweron and Poweroff events.

The following will give you a list of those events over the last 10 days

$events = Get-VIEvent -Start (Get-Date).adddays(-10) |

  where {$_ -is [VMware.Vim.VmPoweredOnEvent] -or $_ -is [VMware.Vim.VmPoweredOnEvent]} |

  Group-Object -Property {$_.Vm.Name} | %{

      $_.Group | Select @{N="VMname"; E={$_.Vm.Name}},

        @{N="CreatedTime"; E={$_.CreatedTime}},

        @{N='Action';E={if($_ -is [VMware.Vim.VmPoweredOnEvent]){'Poweron'}else{'Poweroff'}}},

        @{N="User"; E={$_.UserName}}

  }

$events | Export-Csv "C:\VM-power.csv" -NoTypeInformation -UseCulture


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

View solution in original post

6 Replies
LucD
Leadership
Leadership
Jump to solution

Of course, you can use a CSV or HTML file as log.

Have a look at the Export-Csv or ConvertTo-Html cmdlets.


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

0 Kudos
andrevechiatto
Enthusiast
Enthusiast
Jump to solution

Thanks Luc.


I would like to monitor in real time the exchange status. Example schedule beginning and end of the poweroff . The problem is Export- CSV and converto-html does not change the status after export

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Not sure I got the question.

Perhaps include the script, and indicate what you want to capture and where ?


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

0 Kudos
andrevechiatto
Enthusiast
Enthusiast
Jump to solution

Example

Stop-VM -vm $vms -RunAsync -Confirm:$false | select State,PercentComplete,StartTime,FinishTime,ObjectId,Result,Description,Id,Name | ConvertTo-HTML -head $a | Out-File Test.html

But export only StartTime, not possible capture FinishTime in realtime.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I think you would be better examining the Events for obtaining the start and stop times, i.e. the Poweron and Poweroff events.

The following will give you a list of those events over the last 10 days

$events = Get-VIEvent -Start (Get-Date).adddays(-10) |

  where {$_ -is [VMware.Vim.VmPoweredOnEvent] -or $_ -is [VMware.Vim.VmPoweredOnEvent]} |

  Group-Object -Property {$_.Vm.Name} | %{

      $_.Group | Select @{N="VMname"; E={$_.Vm.Name}},

        @{N="CreatedTime"; E={$_.CreatedTime}},

        @{N='Action';E={if($_ -is [VMware.Vim.VmPoweredOnEvent]){'Poweron'}else{'Poweroff'}}},

        @{N="User"; E={$_.UserName}}

  }

$events | Export-Csv "C:\VM-power.csv" -NoTypeInformation -UseCulture


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

andrevechiatto
Enthusiast
Enthusiast
Jump to solution

Thanks Luc.

Other option is used Excel.Application and export in realtime, but when export stop-vm actions

$excel = New-Object -ComObject Excel.Application

$excel.Visible = $true

$workbook = $excel.Workbooks.Add()

$sheet = $workbook.ActiveSheet

0 Kudos