VMware Cloud Community
aravinds3107
Virtuoso
Virtuoso

Count of VM's in Swapfile Datastore

I am using the below one liner to find the VM count in the datastore which is configured for swapfile

@{Name ="No of Vm's in Swap Datasore";Expression={$_.VMSwapfileDatastore.ExtensionData.Vm.Count}}


I am looking to modify this to check if the datastore is configured for swapfile datastore and if "Yes" provide the VM count and if "No" just display as NA


If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful |Blog: http://aravindsivaraman.com/ | Twitter : ss_aravind
0 Kudos
6 Replies
LucD
Leadership
Leadership

Are you producing this calculated property when doing a Get-Datastore, Get-Cluster, Get-VMHost or Get-VM ?

In other words which object is in the pipeline when you reach this calculated property ?


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

0 Kudos
aravinds3107
Virtuoso
Virtuoso

I am using Get-VMHost

If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful |Blog: http://aravindsivaraman.com/ | Twitter : ss_aravind
0 Kudos
juriskrumins
Enthusiast
Enthusiast

The first part can be solved by this funcion, so you can based your code execution upon results.

function Test-SwapDatastore

{

    [CmdletBinding()]

    [OutputType([int])]

    Param

    (

        [Parameter(Mandatory=$true,

                   ValueFromPipelineByPropertyName=$true,

                   Position=0)]

        [ValidateNotNullOrEmpty()]

        [ValidateScript({Get-Datastore $_})]

        [string]$Datastore

    )

    Process

    {

        (Get-Datastore $Datastore).ExtensionData.Host | ForEach-Object {

            $VMHost=Get-VMHost -id $_.Key

            $Parent=$VmHost.ExtensionData.Parent

            if ( (Get-cluster -Id "$($Parent.Type)-$($Parent.Value)").VMSwapfilePolicy -like "InHostDatastore") {

                if ($VMHost.VMSwapfileDatastore -like "$Datastore") {

                    "" | Select-Object @{N="VMHost";E={"$($VMHost.Name)"}},@{N="Datastore";E={"$Datastore"}},@{N="isSwapDatastore";E={$true}}

                } else {

                    "" | Select-Object @{N="VMHost";E={"$($VMHost.Name)"}},@{N="Datastore";E={"$Datastore"}},@{N="isSwapDatastore";E={$false}}

                }

            } else {

                "" | Select-Object @{N="VMHost";E={"$($VMHost.Name)"}},@{N="Datastore";E={"$Datastore"}},@{N="isSwapDatastore";E={$false}}

            }

        }

    }

}

Get-Datastore | Select-Object -Property @{N="Datastore";E={$_.Name}} | Test-SwapDatastore

0 Kudos
LucD
Leadership
Leadership

But what do you do after the Get-VMHost, run through all the datastores connected to that ESXi host ?


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

0 Kudos
aravinds3107
Virtuoso
Virtuoso

I am getting the below results from the get-vmhost as of now

Get-VMHost |

Select-Object -Property @{Name="ESXi Host Name";Expression={$_.Name}},

      @{Name="Disconnected?";Expression={if($_.ConnectionState -eq "Disconnected"){"Yes"}else{"No"}}},

      @{Name="Maintenance Mode?";Expression={if($_.ExtensionData.Runtime.InMaintenanceMode -eq "True"){"Yes"}else{"No"}}},

      @{Name ="BIOS Version";Expression={$_.ExtensionData.Hardware.BiosInfo.BiosVersion}}, #Generation of BIOS version depends on the hardware

      @{Name ="# of VM's in Swap Volumes";Expression={if$_.VMSwapfileDatastore.ExtensionData.Vm.count}},

      @{Name="Uptime above $UptimeDays days?";E={if((New-TimeSpan -Start $_.Extensiondata.Runtime.BootTime.ToLocalTime() -End $Date).Days -gt $UptimeDays){"Yes"}else{"No"}}} |

If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful |Blog: http://aravindsivaraman.com/ | Twitter : ss_aravind
0 Kudos
LucD
Leadership
Leadership

Try like this

Get-VMHost |
Select-Object -Property @{Name="ESXi Host Name";Expression={$_.Name}},
     
@{Name="Disconnected?";Expression={if($_.ConnectionState -eq "Disconnected"){"Yes"}else{"No"}}},
     
@{Name="Maintenance Mode?";Expression={if($_.ExtensionData.Runtime.InMaintenanceMode -eq "True"){"Yes"}else{"No"}}},
     
@{Name ="BIOS Version";Expression={$_.ExtensionData.Hardware.BiosInfo.BiosVersion}}, #Generation of BIOS version depends on the hardware
      @{Name ="# of VM's in Swap Volumes";Expression={
       
if($_.VMSwapfileDatastore){
          (
Get-View $_.VMSwapfileDatastoreId).Vm.Count
        }
else{
         
"No"
        }}}
,
     
@{Name="Uptime above $UptimeDays days?";E={if((New-TimeSpan -Start $_.Extensiondata.Runtime.BootTime.ToLocalTime() -End $Date).Days -gt $UptimeDays){"Yes"}else{"No"}}}
     
     


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

0 Kudos