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
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
I am using Get-VMHost
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
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
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"}}} |
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