VMware Cloud Community
tdubb123
Expert
Expert

get vmhosts connected to datastores

I need to get a list of all vmhost connected to specific datastores

is there a quick way to do this?

get-datastore | ?{$_.name -match "EMC"} | get-vmhost ??

0 Kudos
10 Replies
LucD
Leadership
Leadership

Try like this

Get-Datastore |

Select Name,@{N='VMHost';E={(Get-View -Id $_.ExtensionData.Host.Key -Property Name).Name -join ','}}


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

0 Kudos
tdubb123
Expert
Expert

thanks lucd

can you tell me why I am getting this under some colums here

System.Object[]

with this script

$results = @()

$vms = get-vm *

foreach ($vm in $vms){

$vmharddisk = $vm | Get-harddisk

$vmview = $vm | Get-View

$datastore = $vm | get-datastore

$result = ""| select vmNAme, harddiskname, harddiskcapacitygb, totalharddiskgb, datastore, datastoreFreespaceGB, datastoreCapacitygb, Storageformat, esxhost

$result.vmName = $vm.Name

$result.harddiskname = $vmharddisk.name

$result.harddiskcapacitygb = $vmharddisk.capacitygb

$result.totalharddiskgb = Get-HardDisk -vm $vm | Measure-Object -Property Capacitygb -Sum | select -ExpandProperty Sum

$result.datastore = $vmview.config.DatastoreUrl.name

$result.datastoreFreespaceGB = $datastore.FreeSpaceGB

$result.datastoreCapacityGB = $datastore.CapacityGB

$result.storageformat = $vmharddisk.StorageFormat

$result.esxhost = $vm.vmhost.name

$results += $result

}

$results | export-csv "C:\scripts\vms.csv"  -NoTypeInformation -UseCulture

-----------------

its only happening outputting to csv

when I do outgrid-view it works just fine

0 Kudos
tdubb123
Expert
Expert

ok i added -join "," to get it to work since i have multiple entries

$results = @()

$vms = get-vm *

foreach ($vm in $vms){

$vmharddisk = $vm | Get-harddisk

$vmview = $vm | Get-View

$datastore = $vm | get-datastore

$result = ""| select vmNAme, harddiskname, harddiskcapacitygb, totalharddiskgb, datastore, datastoreFreespaceGB, datastoreCapacitygb, Storageformat, esxhost

$result.vmName = $vm.Name

$result.harddiskname = $vmharddisk.name -join ","

$result.harddiskcapacitygb = $vmharddisk.capacitygb

$result.totalharddiskgb = Get-HardDisk -vm $vm | Measure-Object -Property Capacitygb -Sum | select -ExpandProperty Sum

$result.datastore = $vmview.config.DatastoreUrl.name

$result.datastoreFreespaceGB = $datastore.FreeSpaceGB

$result.datastoreCapacityGB = $datastore.CapacityGB

$result.storageformat = $vmharddisk.StorageFormat

$result.esxhost = $vm.vmhost.name

$results += $result

}

$results | export-csv "C:\scripts\vms.csv"  -NoTypeInformation -UseCulture

0 Kudos
LucD
Leadership
Leadership

When one of your properties is an array, the Export-Csv cmdlet doens't know how to handle that.

So it just displays the type of the property.

One way of avoiding this and seeing multiple values in one cell, is by doing a -join (see my code above).

That way the multiple values in the array are converted to one string.


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

0 Kudos
tdubb123
Expert
Expert

hi Luc

Trying to round off my numbers but getting an error

Screen Shot 2017-08-10 at 6.49.35 AM.png

0 Kudos
LucD
Leadership
Leadership

The [math]::Round function takes two parameters, the 1st is the actual number that you want rounded, and the 2nd is the number of decimal places you want,

You can put 0 for the 2nd parameter if you don't want decimals in the rounded number.


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

0 Kudos
tdubb123
Expert
Expert

do you mean like this?

$result.harddiskcapacitygb = [math]::round (($vmharddisk.capacitygb),0)

still getting the error

0 Kudos
LucD
Leadership
Leadership

Try like this.
Does this still produce the same error?

$result.harddiskcapacitygb = [math]::round($vmharddisk.capacitygb,0)


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

0 Kudos
tdubb123
Expert
Expert

yes it does

0 Kudos
LucD
Leadership
Leadership

Ok, think I see the issue.

You have more than one value in the variable, it's an array.

Probably from a VM with more than 1 harddisk


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

0 Kudos