VMware Cloud Community
ANKYIT
Contributor
Contributor
Jump to solution

PowerCli Help : Need VMs count on a datastore

Hi Guys ,

Need some assistance in writing a script where I need numbers of VMs on a datastore. this script has some more columns as well.

I am using below however I am not getting any output if VM count is less than 2

$TotalVM = (Get-VM -DataStore $datastore).count

I know there are some one liner scripts available however I am writing this script to get HTML output , and I have also tried

$TotalVM = Get-Datastore Sharepoint_Datastore2 | Select Name ,@{N=”TotalVM”;E={@($_ | Get-VM).Count}}| select totalvm

this gives me output as @{TotalVM=1}

I just need the count of VM on a particluar datastore , Any help would be appriciated.

Thanks in Advance

Reply
0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

You can get the the number of VM's in a datastore with:

$TotalVM = (Get-Datastore $Datastore | Get-VM | Measure-Object).Count

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

Reply
0 Kudos
4 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

You can get the the number of VM's in a datastore with:

$TotalVM = (Get-Datastore $Datastore | Get-VM | Measure-Object).Count

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
ANKYIT
Contributor
Contributor
Jump to solution

You Rock dude , Worked awsome...

Thanks a lot.

Reply
0 Kudos
avlieshout
VMware Employee
VMware Employee
Jump to solution

When the number of vms is less then 2, a scalar value is returned instead of an array value.

A scalar doesn't have a count property, therefore the $TotalVM variable will be empty.

You can use the array subexpression, to force the output to always be an array value:

Original subexpression:

$TotalVM = (Get-VM -DataStore $datastore).count

Using array subexpression

$TotalVM = @(Get-VM -DataStore $datastore).count

spot the difference?

Have fun Smiley Happy

-


Arnim van Lieshout

Blogging: http://www.van-lieshout.com

Twitter: http://www.twitter.com/avlieshout

If you find this information useful, please award points for "correct" or "helpful".

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
Reply
0 Kudos
AllanChristians
Enthusiast
Enthusiast
Jump to solution

Hi,

Try out the new options for SPEED

Run the command's

New-VIProperty -Name VMcount -ObjectType Datastore -ValueFromExtensionProperty 'COUNT vm' -Force
Get-datastore | select name, vmcount

and spot the speed difference from :

(get-datastore | get-vm | measure).count

/Allan

http://doitsmarter.blogspot.com

/Allan http://doitsmarter.blogspot.com
Reply
0 Kudos