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 ??
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
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
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
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
hi Luc
Trying to round off my numbers but getting an error
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
do you mean like this?
$result.harddiskcapacitygb = [math]::round (($vmharddisk.capacitygb),0)
still getting the error
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
yes it does
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