keshavmaster
Contributor
Contributor

how to get poweredoff details of vm's which are located in a resourcepool

how to get poweredoff details of vm's which are located in a resourcepool

i got a script in which will show for full vc, i need deatils for sepecefic resource pool.

function Get-VMLastPoweredOffDate {
  param([Parameter(Mandatory=$true,ValueFromPipeline=$true)]
        [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl] $vm)
  process {
    $Report = "" | Select-Object -Property Name,LastPoweredOffDate,UserName
    $Report.Name = $vm.Name
    $Event = Get-VIEvent -Entity $vm -MaxSamples 10000 | `
      Where-Object { $_.GetType().Name -eq "VmPoweredOffEvent" } | `
      Sort-Object -Property CreatedTime -Descending | `
      Select-Object -First 1
    $Report.LastPoweredOffDate = $Event.CreatedTime
    $Report.UserName = $Event.UserName
    $Report
  }
}

function Get-VMLastPoweredOnDate {
  param([Parameter(Mandatory=$true,ValueFromPipeline=$true)]
        [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl] $vm)

  process {
    $Report = "" | Select-Object -Property Name,LastPoweredOnDate,UserName
    $Report.Name = $vm.Name
    $Event = Get-VIEvent -Entity $vm -MaxSamples 10000 | `
      Where-Object { $_.GetType().Name -eq "VmPoweredOnEvent" -or $_.GetType().Name -eq "DrsVmPoweredOnEvent"} | `
      Sort-Object -Property CreatedTime -Descending |`
      Select-Object -First 1
    $Report.LastPoweredOnDate = $Event.CreatedTime
    $Report.UserName = $Event.UserName
    $Report
  }
}

New-VIProperty -Name LastPoweredOffDate -ObjectType VirtualMachine -Value {(Get-VMLastPoweredOffDate -vm $Args[0]).LastPoweredOffDate} -Force
New-VIProperty -Name LastPoweredOffUserName -ObjectType VirtualMachine -Value {(Get-VMLastPoweredOffDate -vm $Args[0]).UserName} -Force
New-VIProperty -Name LastPoweredOnDate -ObjectType VirtualMachine -Value {(Get-VMLastPoweredOnDate -vm $Args[0]).LastPoweredOnDate} -Force
New-VIProperty -Name LastPoweredOnUserName -ObjectType VirtualMachine -Value {(Get-VMLastPoweredOnDate -vm $Args[0]).UserName} -Force

Get-VM | Select-Object -property Name,LastPoweredOnDate,LastPoweredOnUserName,LastPoweredOffDate,LastPoweredOffUserName

Can you please help to edit this to get deatils only for specific resource pool.

Got the Answer

we can change last line Get-ResourcePool '' | Get-VM | Select-Object -property Name,LastPoweredOnDate,LastPoweredOnUserName,LastPoweredOffDate,LastPoweredOffUserName

0 Kudos
1 Reply
Chrisxxxx
Contributor
Contributor

I think you're solution will pipe every resource pool into Get-VM.  I think the quotes after Get-ResourcePool are not needed.

The "-Location" parameter filters the results to whatever you've specified.  Our environment is fairly large so we have multiple clusters in a vCenter, and each cluster has the same resource pool names (high/normal/low).  Doing "Get-ResourcePool Normal" returns several resource pools, so I have to do a little extra work to get the single "normal" resource pool I want.

Get-VM -Location (Get-ResourcePool respoolname) | Select-Object -property Name,LastPoweredOnDate,LastPoweredOnUserName,LastPoweredOffDate,LastPoweredOffUser

------------------------------------------------------

SYNTAX

    Get-VM [[-Name] <String[]>] [-Server <VIServer[]>] [-Datastore <StorageResource[]>] [-Location <VIContainer[]>] [-Tag <Tag[]>] [-NoRecursion] [<CommonParameters>]

-Location <VIContainer[]>

    Specifies vSphere container objects you want to search for virtual machines. Supported container object types are: ResourcePool, VApp, VMHost, Folder, Cluster, Datacenter.

0 Kudos