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
You can get the the number of VM's in a datastore with:
$TotalVM = (Get-Datastore $Datastore | Get-VM | Measure-Object).Count
Regards, Robert
You can get the the number of VM's in a datastore with:
$TotalVM = (Get-Datastore $Datastore | Get-VM | Measure-Object).Count
Regards, Robert
You Rock dude , Worked awsome...
Thanks a lot.
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
-
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".
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