PowerCLI

 View Only
Expand all | Collapse all

PowerCLI to list all Powered Off VMs and Date of Event

  • 1.  PowerCLI to list all Powered Off VMs and Date of Event

    Posted Jul 21, 2016 07:57 PM

    I am trying to get this script to work but it doesn't even seem close at this point.  I am looking to run a report on Powered Off VMs and information about them.  I need to be able to clean the environment and have to wait 60 days before I permanently delete anything.  Any help would be great.

    Connect-VIServer -Server XXX-vcenter1 -User administrator@vsphere.local -Password XXXXXXXXXXXX

    Connect-VIServer -Server XXX-vcenter1 -User administrator@vsphere.local -Password XXXXXXXXXXXX

    $Report = @()

    $VMs = get-vm |Where-object {$_.powerstate -eq "poweredoff"}

    $Datastores = Get-Datastore | select Name, Id

    $VMHosts = Get-VMHost | select Name, Parent

    foreach ($vm in Get-VM){

    $view = Get-View $VMs

    Get-VIEvent -Entity $VMs -MaxSamples ([int]::MaxValue) |

    where {$_ -is [VMware.Vim.VmPoweredOffEvent]}

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

      $lastPO = $_.Group | Sort-Object -Property CreatedTime -Descending | Select -First 1

      $row = '' | select VMName,Powerstate,OS,Host,Cluster,Datastore,NumCPU,MemMb,DiskGb,PowerOFF

        $row.VMName = $VMs.Name

        $row.Powerstate = $VMs.Powerstate

        $row.OS = $VMs.Guest.OSFullName

        $row.Host = $VMs.host.name

        $row.Cluster = $VMs.host.Parent.Name

        $row.Datastore = ($Datastores | where {$_.ID -match (($vmview.Datastore | Select -First 1) | Select Value).Value} | Select Name).Name

        $row.NumCPU = $VMs.NumCPU

        $row.MemMb = (($VMs.MemoryMB),2)

        $row.DiskGb = ((($VMs.HardDisks | Measure-Object -Property CapacityKB -Sum).Sum * 1KB / 1GB),2)

        $row.PowerOFF = $lastPO.CreatedTime

      $report += $row

    }}

    $report | Sort Name | Export-Csv -Path "C:\XXXXX\Powered_Off_VMs.csv"

    disconnect-viserver * -confirm:$false

    I don't get an export to CSV and a display on screen of the following:

    Template             : False

    Key                  : 165369

    ChainId              : 165369

    CreatedTime          : 6/28/2016 10:19:35 AM

    UserName             :

    Datacenter           : VMware.Vim.DatacenterEventArgument

    ComputeResource      : VMware.Vim.ComputeResourceEventArgument

    Host                 : VMware.Vim.HostEventArgument

    Vm                   : VMware.Vim.VmEventArgument

    Ds                   :

    Net                  :

    Dvs                  :

    FullFormattedMessage : ServerName on  XXX-prodesxi-10.xxxxx.com in ClusterName is powered off

    ChangeTag            :



  • 2.  RE: PowerCLI to list all Powered Off VMs and Date of Event

    Posted Jul 22, 2016 05:03 AM

    There is a pipe symbol missing on the Where-clause line.

    And I took the liberty of fixing a few other issues

    Connect-VIServer -Server XXX-vcenter1 -User administrator@vsphere.local -Password XXXXXXXXXXXX

    $Report = @()

    $VMs = get-vm |Where-object {$_.powerstate -eq "poweredoff"}

    $Datastores = Get-Datastore | select Name, Id

    Get-VIEvent -Entity $VMs -MaxSamples ([int]::MaxValue) |

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

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

      $lastPO = $_.Group | Sort-Object -Property CreatedTime -Descending | Select -First 1

      $vm = Get-VM -Name $_.Name

      $row = '' | select VMName,Powerstate,OS,Host,Cluster,Datastore,NumCPU,MemMb,DiskGb,PowerOFF

        $row.VMName = $vm.Name

        $row.Powerstate = $vm.Powerstate

        $row.OS = $vm.Guest.OSFullName

        $row.Host = $vm.VMHost.name

        $row.Cluster = $vm.VMHost.Parent.Name

        $row.Datastore = ($Datastores | where {$_.ID -match (($vm.Datastore | Select -First 1) | Select Value).Value} | Select Name).Name

        $row.NumCPU = $vm.NumCPU

        $row.MemMb = $vm.MemoryMB

        $row.DiskGb = ((($vm.HardDisks | Measure-Object -Property CapacityKB -Sum).Sum * 1KB / 1GB),2)

        $row.PowerOFF = $lastPO.CreatedTime

        $report += $row

    }

    $report | Sort Name | Export-Csv -Path "C:\XXXXX\Powered_Off_VMs.csv" -NoTypeInformation -UseCulture

    disconnect-viserver * -confirm:$false



  • 3.  RE: PowerCLI to list all Powered Off VMs and Date of Event

    Posted Jul 22, 2016 12:48 PM

    The script is working much better but only captioning a portion of the machines and throwing the following error:

    Get-VM : 7/22/2016 7:34:07 AM    Get-VM        VM with name 'Server_poweredoff_07082016' was not found using the

    specified filter(s).

    At C:\Scripts\Powered Off Guests.ps1:26 char:9

    +   $vm = Get-VM -Name $_.Name

    +         ~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : ObjectNotFound: (:) [Get-VM], VimException

        + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

    Get-VM : 7/22/2016 7:34:08 AM    Get-VM        VM with name 'Server' was not found using the specified filter(s).

    I am thinking that vm is supposed to be renamed when powered off but it looks like the Get-VM is pulling the pre renamed name and it is causing it to choke.

    The other issue is the DataStore and Disk are only pulling back System.Object[]



  • 4.  RE: PowerCLI to list all Powered Off VMs and Date of Event

    Posted Jul 22, 2016 01:27 PM

    The event object contains the name of the VM at the time it was powered off.

    If the VM is renamed afterwards, the script would need to look at the VM's ID to identify it.

    I changed that

    For the Datastore and Disks entries I also did some updates

    Connect-VIServer -Server XXX-vcenter1 -User administrator@vsphere.local -Password XXXXXXXXXXXX

    $Report = @()

    $VMs = get-vm |Where-object {$_.powerstate -eq "poweredoff"}

    $Datastores = Get-Datastore | select Name, Id

    Get-VIEvent -Entity $VMs -MaxSamples ([int]::MaxValue) |

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

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

      $lastPO = $_.Group | Sort-Object -Property CreatedTime -Descending | Select -First 1

      $vm = Get-VIObjectByVIView -MORef $_.Group[0].VM.VM

      $row = '' | select VMName,Powerstate,OS,Host,Cluster,Datastore,NumCPU,MemMb,DiskGb,PowerOFF

        $row.VMName = $vm.Name

        $row.Powerstate = $vm.Powerstate

        $row.OS = $vm.Guest.OSFullName

        $row.Host = $vm.VMHost.name

        $row.Cluster = $vm.VMHost.Parent.Name

        $row.Datastore = $Datastores | Where{$_.Id -eq ($vm.DatastoreIdList | select -First 1)} | Select -ExpandProperty Name

        $row.NumCPU = $vm.NumCPU

        $row.MemMb = $vm.MemoryMB

        $row.DiskGb = Get-HardDisk -VM $vm | Measure-Object -Property CapacityGB -Sum | select -ExpandProperty Sum

        $row.PowerOFF = $lastPO.CreatedTime

        $report += $row

    }

    $report | Sort Name | Export-Csv -Path "C:\XXXXX\Powered_Off_VMs.csv" -NoTypeInformation -UseCulture

    disconnect-viserver * -confirm:$false 



  • 5.  RE: PowerCLI to list all Powered Off VMs and Date of Event

    Posted Jul 22, 2016 02:29 PM

    Getting so close:

    Get-VIObjectByVIView : 7/22/2016 8:36:26 AM    Get-VIObjectByVIView        The object has already been deleted or has

    not been completely created

    At C:Scripts\Powered Off Guests.ps1:26 char:9

    +   $vm = Get-VIObjectByVIView -MORef $_.Group[0].VM.VM

    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : NotSpecified: (:) [Get-VIObjectByVIView], ManagedObjectNotFound

        + FullyQualifiedErrorId : Client20_QueryServiceImpl_RetrievePropertiesEx_ViError,VMware.VimAutomation.ViCore.Cmdle

       ts.Commands.DotNetInterop.GetVIObjectByVIViewCommand

            

    VMNamePowerstateOSHostClusterDatastoreNumCPUMemMbDiskGbPowerOFF
    ServerPoweredOffSUSE Linux Enterprise 11 (64-bit)ESXi-CLUSTERLUN44096177/6/2016 11:14
    System.Object[]System.Object[]System.Object[]System.Object[]System.Object[]LUNSystem.Object[]System.Object[]777/6/2016 11:18


  • 6.  RE: PowerCLI to list all Powered Off VMs and Date of Event

    Posted Nov 17, 2017 02:16 AM

    I ran this script, but export nothing to  the Excel, anything  wrong



  • 7.  RE: PowerCLI to list all Powered Off VMs and Date of Event

    Posted Nov 17, 2017 05:34 AM

    If you do have powered off VMs, and if you keep the events for a sufficiently long time, there should be output.



  • 8.  RE: PowerCLI to list all Powered Off VMs and Date of Event

    Posted Jul 27, 2021 02:42 PM

    Script works great . need to run for mutiple vcenters,  i am giving vcname, IP from inputCSV,  i am tried to connect one vcenter and collect the poweredoff VM's and append in the output sheet but i dono how to append this output details for poweredoff . Please help and modify which can pull from multiple VC and save in a CSV format.



  • 9.  RE: PowerCLI to list all Powered Off VMs and Date of Event

    Posted Jan 05, 2018 12:26 AM

    I realise this thread is getting on a bit, but it was among the first results when I googled.

    LucD's script gave me about 90% of what I was after (thanks!), except that some of our teams have been a little naughty and left VMs powered off for long enough that the event entries have been purged, which in turn meant that those VMs were not included in the report.

    For my purposes I need these VMs to be included in the output, so to this end I've modified the script a little - hopefully it helps someone!

    $Report = @()

    $VMs = Get-VM | Where {$_.PowerState -eq "PoweredOff"}

    $Datastores = Get-Datastore | Select Name, Id

    $PowerOffEvents = Get-VIEvent -Entity $VMs -MaxSamples ([int]::MaxValue) | where {$_ -is [VMware.Vim.VmPoweredOffEvent]} | Group-Object -Property {$_.Vm.Name}

    foreach ($VM in $VMs) {

        $lastPO = ($PowerOffEvents | Where { $_.Group[0].Vm.Vm -eq $VM.Id }).Group | Sort-Object -Property CreatedTime -Descending | Select -First 1

        $row = "" | select VMName,Powerstate,OS,Host,Cluster,Datastore,NumCPU,MemMb,DiskGb,PoweredOffTime,PoweredOffBy

        $row.VMName = $vm.Name

        $row.Powerstate = $vm.Powerstate

        $row.OS = $vm.Guest.OSFullName

        $row.Host = $vm.VMHost.name

        $row.Cluster = $vm.VMHost.Parent.Name

        $row.Datastore = $Datastores | Where{$_.Id -eq ($vm.DatastoreIdList | select -First 1)} | Select -ExpandProperty Name

        $row.NumCPU = $vm.NumCPU

        $row.MemMb = $vm.MemoryMB

        $row.DiskGb = Get-HardDisk -VM $vm | Measure-Object -Property CapacityGB -Sum | select -ExpandProperty Sum

        $row.PoweredOffTime = $lastPO.CreatedTime

        $row.PoweredOffBy   = $lastPO.UserName

        $report += $row

    }

    # Output to screen

    $report | Sort Cluster, Host, VMName | Select VMName, Cluster, Host, NumCPU, MemMb, @{N='DiskGb';E={[math]::Round($_.DiskGb,2)}}, PoweredOffTime, PoweredOffBy | ft -a

    # Output to CSV - change path/filename as appropriate

    $report | Sort Cluster, Host, VMName | Export-Csv -Path "C:\XXXXX\Powered_Off_VMs.csv" -NoTypeInformation -UseCulture



  • 10.  RE: PowerCLI to list all Powered Off VMs and Date of Event

    Posted Feb 26, 2018 04:14 PM

    I'm another who has benefited from this old thread. Many thanks, especially to LucD for the examples provided!

    For my solution I've reworked LucD's example inline above as well as another of his postings for Get-VMLog which copies the VM's log to local storage.

    This modifies LucD's Get-VMLog to instead copy the logfile to local storage where it can be parsed, pull the last event and return the datestamp, log source, and logged event as objects. Then  modified LucD's example above to attempt to pull vm power off event first and fail back to the slow logfile method.

    Error checking could be improved, but this works for my quick and dirty needs and I have not found any blocking or critical errors, usually just cases where the vmware.log file doesn't exist.

    Also note the returned date object from Get-VMLogLastEvent may have issues with non-US timestamps which I understand is a normal problem with "Get-Date" conversions so be aware.

    Many thanks to LucD for sharing how to post pretty code. Shame they don't support the old BBCode markup [code][/code] blocks or anything similar.


    function Get-VMLogLastEvent{
        param(
        [
    parameter(Mandatory=$true,ValueFromPipeline=$true)][PSObject[]]$VM,
        [
    string]$Path=$env:TEMP
        )

       
    process{  
           
    $report = @()

           
    foreach($obj in $VM){
               
    if($obj.GetType().Name -eq "string"){
                   
    $obj = Get-VM -Name $obj
                }
               
    $logpath = ($obj.ExtensionData.LayoutEx.File | ?{$_.Name -like "*/vmware.log"}).Name
               
    $dsName = $logPath.Split(']')[0].Trim('[')
               
    $vmPath = $logPath.Split(']')[1].Trim(' ')
               
    $ds = Get-Datastore -Name $dsName
               
    $drvName = "MyDS" + (Get-Random)
               
    $localLog = $Path + "\" + $obj.Name + ".vmware.log"
               
    New-PSDrive -Location $ds -Name $drvName -PSProvider VimDatastore -Root '\' | Out-Null
               
    Copy-DatastoreItem -Item ($drvName + ":" + $vmPath) -Destination $localLog -Force:$true
               
    Remove-PSDrive -Name $drvName -Confirm:$false
           
               
    $lastEvent = Get-Content -Path $localLog -Tail 1
               
    Remove-Item -Path $localLog -Confirm:$false
               
               
    $row = "" | Select VM, EventType, Event, EventTime
               
    $row.VM = $obj.Name
                (
    $row.EventTime, $row.EventType, $row.Event) = $lastEvent.Split("|")
               
    $row.EventTime = $row.EventTime | Get-Date
               
    $report += $row
             }
            
    $report       
        }
    }

    $Report = @()  
    $VMs = Get-VM | Where {$_.PowerState -eq "PoweredOff"
    $Datastores = Get-Datastore | Select Name, Id 
    $PowerOffEvents = Get-VIEvent -Entity $VMs -MaxSamples ([int]::MaxValue) | where {$_ -is [VMware.Vim.VmPoweredOffEvent]} | Group-Object -Property {$_.Vm.Name
     
    foreach ($VM in $VMs) { 
       
    $lastPO = ($PowerOffEvents | Where { $_.Group[0].Vm.Vm -eq $VM.Id }).Group | Sort-Object -Property CreatedTime -Descending | Select -First 1
       
    $lastLogTime = "";
       
       
    # If no event log detail, revert to vmware.log last entry which takes more time...
        if (($lastPO.PoweredOffTime -eq "") -or ($lastPO.PoweredOffTime -eq $null)){
           
    $lastLogTime = (Get-VMLogLastEvent -VM $VM).EventTime
        }
     
       
    $row = "" | select VMName,Powerstate,OS,Host,Cluster,Datastore,NumCPU,MemMb,DiskGb,PoweredOffTime,PoweredOffBy,LastLogTime
       
    $row.VMName = $vm.Name 
       
    $row.Powerstate = $vm.Powerstate 
       
    $row.OS = $vm.Guest.OSFullName 
       
    $row.Host = $vm.VMHost.name 
       
    $row.Cluster = $vm.VMHost.Parent.Name 
       
    $row.Datastore = $Datastores | Where{$_.Id -eq ($vm.DatastoreIdList | select -First 1)} | Select -ExpandProperty Name 
       
    $row.NumCPU = $vm.NumCPU 
       
    $row.MemMb = $vm.MemoryMB 
       
    $row.DiskGb = Get-HardDisk -VM $vm | Measure-Object -Property CapacityGB -Sum | select -ExpandProperty Sum 
       
    $row.PoweredOffTime = $lastPO.CreatedTime 
       
    $row.PoweredOffBy   = $lastPO.UserName
       
    $row.LastLogTime = $lastLogTime
       
    $report += $row 

     
    # Output to screen 
    $report | Sort Cluster, Host, VMName | Select VMName, Cluster, Host, NumCPU, MemMb, @{N='DiskGb';E={[math]::Round($_.DiskGb,2)}}, PoweredOffTime, PoweredOffBy | ft -a 
     
    # Output to CSV - change path/filename as appropriate 
    $report | Sort Cluster, Host, VMName | Export-Csv -Path "output\Powered_Off_VMs.csv" -NoTypeInformation -UseCulture 


  • 11.  RE: PowerCLI to list all Powered Off VMs and Date of Event

    Posted Feb 26, 2018 05:45 PM

    Thanks for sharing that.

    And on the pretty code, have a look at Some ways to enter PowerCLI code under the new forum SW



  • 12.  RE: PowerCLI to list all Powered Off VMs and Date of Event

    Posted Aug 01, 2018 09:21 AM

    This script is working fine where events are traced. But some vcenters has overrite logs as per purge settings and for some Powered off VMs lets say powered off 2-3 years ago..... 'VmPoweredOffEvent' event doesn't exist on vCenter so how can we fetch info for those VMs.



  • 13.  RE: PowerCLI to list all Powered Off VMs and Date of Event

    Posted Aug 01, 2018 09:25 AM

    You can't.
    The only solution is to export the events that are of interest of you to an external source, before they are purged.

    Same goes for the statistical data.



  • 14.  RE: PowerCLI to list all Powered Off VMs and Date of Event

    Posted Mar 30, 2017 07:24 AM

    Get-VIEvent : 3/30/2017 2:22:44 AM    Get-VIEvent        Object reference not set to an instance of an object.   

    At line:8 char:1

    + Get-VIEvent -Entity $VMs | Where-Object {$_.FullFormattedMessage -is  ...

    + ~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : NotSpecified: (:) [Get-VIEvent], VimException

        + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetEvent

    I'm getting the above error while running the script. Could you please tell me what am i missing?



  • 15.  RE: PowerCLI to list all Powered Off VMs and Date of Event

    Posted Mar 30, 2017 08:04 AM

    Did you already try to stop/start your PowerCLI session?



  • 16.  RE: PowerCLI to list all Powered Off VMs and Date of Event

    Posted Mar 30, 2017 12:10 PM

    Yes, I did.



  • 17.  RE: PowerCLI to list all Powered Off VMs and Date of Event

    Posted Mar 30, 2017 02:07 PM

    And did you check that there is anything in $vms.

    Also, try adding the -Verbose switch on the Get-VIEvent cmdlet.

    See if that gives more information



  • 18.  RE: PowerCLI to list all Powered Off VMs and Date of Event

    Posted Mar 07, 2019 03:12 PM
    Hi#Connect-VIServer -Server XXX-vcenter1 -User administrator@vsphere.local -Password XXXXXXXXXXXX
     
    $Report = @()
     
    $VMs = get-vm |Where-object {$_.powerstate -eq "poweredoff"}
     
    $Datastores = Get-Datastore | select Name, Id
     
    Get-VIEvent -Entity $VMs -MaxSamples ([int]::MaxValue) |
     
    where {$_ -is [VMware.Vim.VmPoweredOffEvent]} |
     
    Group-Object -Property {$_.Vm.Name} | %{
     
      $lastPO = $_.Group | Sort-Object -Property CreatedTime -descending | Select -First 1
     
      $vm = Get-VIObjectByVIView -MORef $_.Group[0].VM.VM
     
      $row = '' | select VMName,Powerstate,OS,Host,Cluster,Datastore,NumCPU,MemMb,DiskGb,PowerOFF
     
        $row.VMName = $vm.Name
     
        $row.Powerstate = $vm.Powerstate
     
        $row.OS = $vm.Guest.OSFullName
     
        $row.Host = $vm.VMHost.name
     
        $row.Cluster = $vm.VMHost.Parent.Name
     
        $row.Datastore = $Datastores | Where{$_.Id -eq ($vm.DatastoreIdList | select -First 1)} | Select -ExpandProperty Name
     
        $row.NumCPU = $vm.NumCPU
     
        $row.MemMb = $vm.MemoryMB
     
        $row.DiskGb = Get-HardDisk -VM $vm | Measure-Object -Property CapacityGB -Sum | select -ExpandProperty Sum
     
        $row.PowerOFF = $lastPO.CreatedTime
     
           $report += $row
        
    }
    #Connect-VIServer -Server XXX-vcenter1 -User administrator@vsphere.local -Password XXXXXXXXXXXX
     
    $Report = @()
     
    $VMs = get-vm |Where-object {$_.powerstate -eq "poweredoff"}
     
    $Datastores = Get-Datastore | select Name, Id
     
    Get-VIEvent -Entity $VMs -MaxSamples ([int]::MaxValue) |
     
    where {$_ -is [VMware.Vim.VmPoweredOffEvent]} |
     
    Group-Object -Property {$_.Vm.Name} | %{
     
      $lastPO = $_.Group | Sort-Object -Property CreatedTime -descending | Select -First 1
     
      $vm = Get-VIObjectByVIView -MORef $_.Group[0].VM.VM
     
      $row = '' | select VMName,Powerstate,OS,Host,Cluster,Datastore,NumCPU,MemMb,DiskGb,PowerOFF
     
        $row.VMName = $vm.Name
     
        $row.Powerstate = $vm.Powerstate
     
        $row.OS = $vm.Guest.OSFullName
     
        $row.Host = $vm.VMHost.name
     
        $row.Cluster = $vm.VMHost.Parent.Name
     
        $row.Datastore = $Datastores | Where{$_.Id -eq ($vm.DatastoreIdList | select -First 1)} | Select -ExpandProperty Name
     
        $row.NumCPU = $vm.NumCPU
     
        $row.MemMb = $vm.MemoryMB
     
        $row.DiskGb = Get-HardDisk -VM $vm | Measure-Object -Property CapacityGB -Sum | select -ExpandProperty Sum
     
        $row.PowerOFF = $lastPO.CreatedTime
     
           $report += $row
        
    }
    #Connect-VIServer -Server XXX-vcenter1 -User administrator@vsphere.local -Password XXXXXXXXXXXX
     
    $Report = @()
     
    $VMs = get-vm |Where-object {$_.powerstate -eq "poweredoff"}
     
    $Datastores = Get-Datastore | select Name, Id
     
    Get-VIEvent -Entity $VMs -MaxSamples ([int]::MaxValue) |
     
    where {$_ -is [VMware.Vim.VmPoweredOffEvent]} |
     
    Group-Object -Property {$_.Vm.Name} | %{
     
      $lastPO = $_.Group | Sort-Object -Property CreatedTime -descending | Select -First 1
     
      $vm = Get-VIObjectByVIView -MORef $_.Group[0].VM.VM
     
      $row = '' | select VMName,Powerstate,OS,Host,Cluster,Datastore,NumCPU,MemMb,DiskGb,PowerOFF
     
        $row.VMName = $vm.Name
     
        $row.Powerstate = $vm.Powerstate
     
        $row.OS = $vm.Guest.OSFullName
     
        $row.Host = $vm.VMHost.name
     
        $row.Cluster = $vm.VMHost.Parent.Name
     
        $row.Datastore = $Datastores | Where{$_.Id -eq ($vm.DatastoreIdList | select -First 1)} | Select -ExpandProperty Name
     
        $row.NumCPU = $vm.NumCPU
     
        $row.MemMb = $vm.MemoryMB
     
        $row.DiskGb = Get-HardDisk -VM $vm | Measure-Object -Property CapacityGB -Sum | select -ExpandProperty Sum
     
        $row.PowerOFF = $lastPO.CreatedTime
     
           $report += $row
        
    }


  • 19.  RE: PowerCLI to list all Powered Off VMs and Date of Event

    Posted Mar 07, 2019 03:16 PM

    Is there a question in there?



  • 20.  RE: PowerCLI to list all Powered Off VMs and Date of Event

    Posted Mar 31, 2017 06:38 AM

    Would this be possible to add/modify who turned off the VM ?



  • 21.  RE: PowerCLI to list all Powered Off VMs and Date of Event

    Posted Mar 31, 2017 06:49 AM

    Try adding the UserName property to the output.



  • 22.  RE: PowerCLI to list all Powered Off VMs and Date of Event

    Posted Apr 02, 2017 11:59 PM

    Yes, you are right, I've added the below lines and it works:

    $row.UserName = $lastPO.UserName

    Thanks LucD



  • 23.  RE: PowerCLI to list all Powered Off VMs and Date of Event

    Posted Jul 10, 2017 08:47 AM

    Hi guys,

    I know this is an old post, but quick question.

    Would this also list VMs that were shutdown through the guest OS?

    Thanks

    Yan



  • 24.  RE: PowerCLI to list all Powered Off VMs and Date of Event

    Posted Jul 10, 2017 08:56 AM

    Yes, normally it should.



  • 25.  RE: PowerCLI to list all Powered Off VMs and Date of Event

    Posted Jul 10, 2017 10:08 AM

    Thanks for fast reply Luc..

    Well what could explain some VMs missing from the list of powered off VMs?  Perhaps because the power off event for those VMs isn't in the event log anymore?



  • 26.  RE: PowerCLI to list all Powered Off VMs and Date of Event

    Posted Jul 10, 2017 10:17 AM

    Yes, if the power off happened further back in time then the Event retention period, you wouldn't find any events for those.

    There could be circumstances that a VM is powered off without the vCenter creating an event.

    The hosting ESXi could be disconnected at the time, the VM can disconnect...

    It would require further investigation into such a case.



  • 27.  RE: PowerCLI to list all Powered Off VMs and Date of Event

    Posted Jul 10, 2017 10:29 AM

    Thanks man. 

    Yan