I am using the get-datastore cmdlet to populate a field in a billing report. We charge for storage based on the type of storage, it looks like this.
$row.DatastoreName = get-datastore -vm $_ | foreach{ if($_.Name -match "sata"){echo "Tier3";}else{echo "Tier2";}}
My problem has only occured recently because i have added some microsoft clusters to the environment and the second node of each has multiple datastores listed. This makes my foreach statement blow up. Is there a way to make the get-datastore command to only return one value?
Thanks
Yes, but you would need to choose which one it would get...
you could do the first one
get-datastore |select-object -first 1
or a random
get-datastore | get-random
or one based on some type of filter
get-datastore | Where-object {$_.freespace -gt "5"}
This last one could still return multiple datastores depending on the filter you use....
How are you looking to choose just one?
Yes, but you would need to choose which one it would get...
you could do the first one
get-datastore |select-object -first 1
or a random
get-datastore | get-random
or one based on some type of filter
get-datastore | Where-object {$_.freespace -gt "5"}
This last one could still return multiple datastores depending on the filter you use....
How are you looking to choose just one?
You forgot the simplest one
@(Get-Datastore -VM $_)[0]
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
excellent i used the pipe to select-object and the data in the output all looks good now.
Thanks very much crad14
One other line i have an issue with is
$row.ProvisionedSpaceGB = "{0:N1}" -f (($_.HardDisks | %{$_.CapacityKB} | Measure-Object -Sum).Sum/1MB)
it returns the total disk space allocated to the vm including any raw luns, if i could find a way to drop off the raw luns sizes that would be great.
I have not started looking into this yet. So it might be a simple solution.
Yeah it should be pretty easy
Essentially you want to filter out that particular disk type...
$_.HardDisks | Where-Object { $_.disktype -notlike "RDM"}
Before you do the foreach portion....
I don't really use raw disks but if you look at the $vm.harddisks.disktype property you should be able to substitute my RDM for whatever powercli is calling them...
Hope that helps...