VMware Cloud Community
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

Comparing VM RAM and DataStore FreeSpace

Hi All,

I am trying to find out in which VM is having more RAM when compared to datastore free space.

I have written below script for the same and getting error. More over is there any way that I can print that VM Name and Datastore Name after comparing the values. Please suggest.

$vm = get-vm

$vm | select Name,MemoryMB,@{N='FreeSpaceMB';E={(Get-DataStore | Where {$_.Name -eq ($vm.ExtensionData.Config.Files.VmPathName).Split('[]')[1]}).FreeSpaceMB}} | where{$vm.memorymb -ge $_.FreespaceMB}

PowerCLI C:\> $vm | select Name,MemoryMB,@{N='FreeSpaceMB';E={(Get-DataStore | Where {$_.Name -eq ($vm.ExtensionData.Config.Files.VmPathName).Split('[]')[1]}).FreeSpaceMB}} | where{$vm.memorymb -ge $_.FreespaceMB}

Could not compare "4096" to "1621381 1621333". Error: "Cannot convert the

"System.Object[]" value of type "System.Object[]" to type "System.Decimal"."

At line:1 char:168

+ ... ceMB}} | where{$vm.memorymb -ge $_.FreespaceMB}

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

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

    + FullyQualifiedErrorId : ComparisonFailure

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this

Get-VM |

Select Name,MemoryMB,

    @{N='FreeSpaceMB';E={(Get-Datastore -Name ($_.ExtensionData.Config.Files.VmPathName).Split('[]')[1] | Select -First 1).FreeSpaceMB}} |

where{$vm.memorymb -ge $_.FreespaceMB} 


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

View solution in original post

0 Kudos
7 Replies
LucD
Leadership
Leadership
Jump to solution

It looks like the FreeSpaceMB property returns an array (in this case with 2 values).

And PowerShell doesn't know how to compare a single value (MemoryMB) with an array holding 2 values.

You'll have to find out why the following apparently returns 2 values in your environment

Get-DataStore | Where {$_.Name -eq ($vm.ExtensionData.Config.Files.VmPathName).Split('[]')[1]}


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

0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

I have found some information regarding this.

We have 6+ data stores in one vCenter.

When I am executing the below command

Get-DataStore | Where {$_.Name -eq ($vm.ExtensionData.Config.Files.VmPathName).Split('[]')[1]}

this where command is getting the Datastore name where VM Located and passing to Get-DataStore cmdlet

Where {$_.Name -eq ($vm.ExtensionData.Config.Files.VmPathName).Split('[]')[1]}

The Same Datastore is mapped with 2 different Clusters in 2 Different DataCenters in same vCenter

This could be the reason Lucd?

Is there any way to accomplish my task?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

Get-VM |

Select Name,MemoryMB,

    @{N='FreeSpaceMB';E={(Get-Datastore -Name ($_.ExtensionData.Config.Files.VmPathName).Split('[]')[1] | Select -First 1).FreeSpaceMB}} |

where{$vm.memorymb -ge $_.FreespaceMB} 


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

0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

Thanks Lucd Script Works Fine.

0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

Is It Possible to get Hostname in which the VM is running if VM Mem is greater than Host DataStore FreeSpace.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

Get-VM |

Select Name,@{N='VMHost';E={$_.VMhost.Name}},MemoryMB,

    @{N='FreeSpaceMB';E={(Get-Datastore -Name ($_.ExtensionData.Config.Files.VmPathName).Split('[]')[1] | Select -First 1).FreeSpaceMB}} |

where{$vm.memorymb -ge $_.FreespaceMB}


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

0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

Thanks Lucd

0 Kudos