Automation

 View Only
  • 1.  Powershell code to list all VM per Datastore

    Posted Nov 22, 2010 04:44 AM

    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



  • 2.  RE: Powershell code to list all VM per Datastore
    Best Answer

    Posted Nov 22, 2010 06:22 AM

    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



  • 3.  RE: Powershell code to list all VM per Datastore

    Posted Nov 22, 2010 10:35 PM

    ah yes,

    you're right let me try this first.



  • 4.  RE: Powershell code to list all VM per Datastore

    Posted Nov 24, 2010 03:03 AM






    Yes it works man, thanks for the help !

    Kind Regards,

    AWT



  • 5.  RE: Powershell code to list all VM per Datastore

    Posted May 09, 2012 01:31 AM

    Hi LucD,

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

    Thanks in advance.



  • 6.  RE: Powershell code to list all VM per Datastore

    Posted May 09, 2012 04:54 AM

    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