VMware Cloud Community
AlbertWT
Virtuoso
Virtuoso
Jump to solution

Powershell code to list all VM per Datastore

Hi Everyone,

I got the following script to list all of the VM per datastore, but somehow it doesn't display datastore (blanks) for certain VMs ?

Get-Datacenter | Get-VM | %{ $_.Name + " - " + (($_ | Get-Datastore | select Name).Name) | sort Name } | ft -AutoSize

any kind of help would be greatly appreciated.

Kind Regards,

AWT

/* Please feel free to provide any comments or input you may have. */
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Could it be that you have some guests that have connections to 2 or more datastores ?

Try this

Get-Datacenter | Get-VM | %{
	$vm = $_
	Get-Datastore -VM $vm | %{
		$vm.Name + "-" + $_.Name
	}
} | ft -AutoSize

____________

Blog: LucD notes

Twitter: lucd22


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

View solution in original post

0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

Could it be that you have some guests that have connections to 2 or more datastores ?

Try this

Get-Datacenter | Get-VM | %{
	$vm = $_
	Get-Datastore -VM $vm | %{
		$vm.Name + "-" + $_.Name
	}
} | ft -AutoSize

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
AlbertWT
Virtuoso
Virtuoso
Jump to solution

ah yes,

you're right let me try this first.

/* Please feel free to provide any comments or input you may have. */
0 Kudos
AlbertWT
Virtuoso
Virtuoso
Jump to solution






Yes it works man, thanks for the help !

Kind Regards,

AWT

/* Please feel free to provide any comments or input you may have. */
0 Kudos
ronmanu07
Enthusiast
Enthusiast
Jump to solution

Hi LucD,

How would I get this output in to a CSV file?

Thanks in advance.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you pipe this as-is to a CSV you will just get a CSV file with the length of each string.

The solution is to use a Select-Object and pass objects to the Export-CSV cmdlet.

Something like this for example

Get-Datacenter | Get-VM | %{
  $vm = $_
 
Get-Datastore -VM $vm |
  Select @{N="Name and DS";E={$vm.Name + "-" + $_.Name}} } | Export-Csv C:\report.csv -NoTypeInformation -UseCulture


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

0 Kudos