Automation

 View Only
  • 1.  VM Datastore Location

    Posted Aug 04, 2011 01:58 PM

    Hi Guy's

    I have been trying to write a script that will output the name of a vm and the datastore its located on. This is what i have written so far:

    $cred = Get-Credential
    Connect-VIServer xxx.domain.com -Credential $cred
    $datastore = Get-Datastore | select Name
    foreach ($objitem in $datastore)
    {
    Get-VM | select Name
    Write-Host "This is located on" $objitem.Name
    }
    Disconnect-VIServer -Confirm:$false

    This script appears to be nearly there but im not getting the names of the datastore. Ideally i'd expect something like:

    Name: VM1    Datasore: Lun01

    Name: VM2    Datasore: Lun02

    Name: VM3    Datasore: Lun03

    Can anybody advise?

    Regards

    Mr G



  • 2.  RE: VM Datastore Location
    Best Answer

    Posted Aug 04, 2011 02:02 PM

    Does this work for you ?

    Get-VM | Select Name,@{N="DS";E={(Get-Datastore -VM $_ | Select -First 1).Name}}


  • 3.  RE: VM Datastore Location

    Posted Aug 04, 2011 02:36 PM

    Thanks Luc,

    This worked a charm!



  • 4.  RE: VM Datastore Location

    Posted Aug 04, 2011 03:18 PM

    Note that if you have a VM that has virtual disks on other datastores, that you will not see these.

    The line only shows the first datastore, most of the time the datastore where the .vmx file is located.



  • 5.  RE: VM Datastore Location

    Posted Aug 04, 2011 03:26 PM

    Thanks i just noticed that. We put our o/s page files on a different datastores so a lot of the time this was coming up. I changed your recommendation a bit to:

    Get-VM | Select Name,@{N="DS";E={(Get-Datastore -VM $_ | Select -Last 1).Name}}

    That seems to have given me the info i need.