VMware Cloud Community
chicojr
Enthusiast
Enthusiast

Find powered off vms and who powered off and for any vm pwr'd off longer than 90 days please.

I run this:

   param(

    [VMware.VimAutomation.ViCore.Impl.V1.Inventory.InventoryItemImpl[]]$Entity,

    [string[]]$EventType,

    [DateTime]$Start,

    [DateTime]$Finish,

    [switch]$Recurse,

    [switch]$FullMessage = $false

  )

  process {

    $eventnumber = 100

    $events = @()

    $eventMgr = Get-View EventManager

    $eventFilter = New-Object VMware.Vim.EventFilterSpec

    $eventFilter.disableFullMessage = ! $FullMessage

    $eventFilter.entity = New-Object VMware.Vim.EventFilterSpecByEntity

    $eventFilter.entity.recursion = &{if($Recurse){"all"}else{"self"}}

    $eventFilter.eventTypeId = $EventType

    if($Start -or $Finish){

      $eventFilter.time = New-Object VMware.Vim.EventFilterSpecByTime

      $eventFilter.time.beginTime = $Start

      $eventFilter.time.endTime = $Finish

    }

    $entity | %{

      $eventFilter.entity.entity = $_.ExtensionData.MoRef

      $eventCollector = Get-View ($eventMgr.CreateCollectorForEvents($eventFilter))

      $eventsBuffer = $eventCollector.ReadNextEvents($eventnumber)

      while($eventsBuffer){

        $events += $eventsBuffer

        $eventsBuffer = $eventCollector.ReadNextEvents($eventnumber)

      }

      $eventCollector.DestroyCollector()

    }

    $events

}

}

$oneMonth = (Get-Date).AddDays(-31)

$entity = Get-Folder -Name datacenters

$events = Get-VIEventPlus -Entity $entity -EventType VmPoweredOnEvent -Recurse -FullMessage

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

$_.Group | Sort-Object -Property CreatedTime -Descending |

Select -First 1 |

where {$_.CreatedTime -lt $oneMonth -and (Get-VM -Name $_.Vm.Name -ErrorAction SilentlyContinue).PowerState -eq "PoweredOff"} |

Select @{N="VM";E={$_.Vm.Name}},CreatedTime,UserName

}

But it errors out:

At line:38 char:1

+ }

+ ~

Unexpected token '}' in expression or statement.

    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException

    + FullyQualifiedErrorId : UnexpectedToken

I tried to clean up } but still fails, just need this to run cleanly and export to something viewable like a CSV please.

Reply
0 Kudos
5 Replies
sjesse
Leadership
Leadership

Someone may respond to fix your code, but look at http://www.virtu-al.net/vcheck-pluginsheaders/vcheck/  if you haven't. Once of the plugins "60 Powered Off VMs.ps1"  does what your looking for the most part. You just changed $PoweredOffDays to 90, the default is 7

Reply
0 Kudos
chicojr
Enthusiast
Enthusiast

sjesse, thx. Please if you could provide a direct link to plugin it has me going everywhere and just not sure what I'm looking for.

Reply
0 Kudos
LucD
Leadership
Leadership

The curly braces for the Process block are out of sync.

The corretc script is attached to Re: VM Last Poweron n use

Btw, the vCheck plugin uses my same Get-ViEventPlus function :smileygrin:


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

Reply
0 Kudos
chicojr
Enthusiast
Enthusiast

PS C:\Scripts> .\vCheck.ps1 -OutputPath C:\Temp\pwroffvms2019[/ps]

Get-vCheckSetting : The term 'Get-vCheckSetting' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is

correct and try again.

At C:\Scripts\vCheck.ps1:18 char:15

+ $IgnoredVMs = Get-vCheckSetting $Title "IgnoredVMs" $IgnoredVMs

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

    + CategoryInfo          : ObjectNotFound: (Get-vCheckSetting:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

Get-vCheckSetting : The term 'Get-vCheckSetting' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is

correct and try again.

At C:\Scripts\vCheck.ps1:19 char:18

+ $IgnoredVMpath = Get-vCheckSetting $Title "IgnoredVMpath" $IgnoredVMp ...

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

    + CategoryInfo          : ObjectNotFound: (Get-vCheckSetting:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

Get-vCheckSetting : The term 'Get-vCheckSetting' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is

correct and try again.

At C:\Scripts\vCheck.ps1:20 char:19

+ $PoweredOffDays = Get-vCheckSetting $Title "PoweredOffDays" $PoweredO ...

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

    + CategoryInfo          : ObjectNotFound: (Get-vCheckSetting:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

You cannot call a method on a null-valued expression.

At C:\Scripts\vCheck.ps1:22 char:21

+ ... ere-Object {$_.ExtensionData.Config.ManagedBy.ExtensionKey -ne 'com.v ...

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

    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

    + FullyQualifiedErrorId : InvokeMethodOnNull

Error formatting a string: Index (zero based) must be greater than or equal to zero and less than the size of the argument list..

At C:\Scripts\vCheck.ps1:30 char:1

+ $Comments = ("May want to consider deleting VMs that have been powere ...

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

    + CategoryInfo          : InvalidOperation: (May want to con... than {90} days:String) [], RuntimeException

    + FullyQualifiedErrorId : FormatError

Reply
0 Kudos