SaPu
Contributor
Contributor

Get disk on a datastore

Jump to solution

Hello,

I have the following script

Get-Datastore "*sata)" | Get-VM | Get-VMGuest |  Where { $_.Disks } | select -ExpandProperty Disks VMName


the output is

VmNamePathCapacityFreeSpace
VM1C:\128807526404642414592
VM1D:\322183864324818952192
VM2C:\128807526404642414592

What I'm looking for is an output like

VmNamePathDatastore
VM1C:\store1 (sata)
VM1D:\store3 (sata)
VM2C:\store1 (sata)

Can anyone help me with this?

Regards.

SaPu

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

That is not so straightforward.

The drive letter is linked to the partition you defined inside the guest OS.

You can for example create 2 or more partitions on 1 virtual disk.

There is an excellent script that does this, that also appeared in our book, and you can find it here.

The function is called Get-VMDiskMapping.


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

View solution in original post

0 Kudos
8 Replies
marc045
Contributor
Contributor

Hi, try this:

$report = @()

$myVMs = @(get-datastore | get-vm)

foreach($vm in $myVMs){
$myDisks = @($vm | get-harddisk)
$i=1
foreach($disk in $myDisks){
  $row = "" | Select VMname,Path,Datastore
  $row.VMname = $vm.name
  $row.Path = "Disk $i"
  $tmp = get-view -id $disk.ExtensionData.backing.datastore
  $tmp = $tmp | get-viobjectbyviview
  $row.Datastore = $tmp.name
  $i++
  $report += $row
}
}
echo $report

Regards,

marc0

AlbertWT
Virtuoso
Virtuoso

Hi marc,

how come I got multiple error message here:

Get-VIObjectByVIView : 13/05/2011 10:00:16 AM    Get-VIObjectByVIView        Object reference not set to an instance of an object.   
At C:\Temp\ad39c208-757f-4f8d-8f71-048d56235240.ps1:13 char:37
+   $tmp = $tmp | get-viobjectbyviview <<<<
    + CategoryInfo          : NotSpecified: (:) [Get-VIObjectByVIView], VimException
    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIObjectByVIViewCommand

FYI:

Iam using Get-PowerCLIVersion

PowerCLI Version

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

   VMware vSphere PowerCLI 4.1 U1 build 332441

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

Snapin Versions

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

   VMWare vSphere PowerCLI 4.1 U1 build 332441

/* Any kind of comment or input would be greatly appreciated */
0 Kudos
marc045
Contributor
Contributor

Hi Albert,

Unsure, the code works fine at my end.  I'm using 32-bit powershell, if you're using 64-bit possibly that is the culprit but I have no idea really.

Regards

marc0

0 Kudos
AlbertWT
Virtuoso
Virtuoso

no man,

I am using 32 bit WIndows XP with PS 2.0, but anyway thanks for sharing your script here 🙂

/* Any kind of comment or input would be greatly appreciated */
0 Kudos
LucD
Leadership
Leadership

You can avoid the calls to the Get-View and Get-VIObjectByVIView cmdlets by doing it this way.

$report = @()

$myVMs = @(get-datastore | get-vm)

foreach($vm in $myVMs){
    $myDisks = @($vm | get-harddisk)
    $i = 1
   
foreach($disk in $myDisks){         $row = "" | Select VMname,Path,Datastore         $row.VMname = $vm.name         $row.Path = "Disk $i"
        $row.Datastore = $disk.Filename.Split(']')[0].TrimStart('[')         $i++
       
$report += $row
    } }
$report


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

AlbertWT
Virtuoso
Virtuoso

yes it works now, many-many thanks Luc !

/* Any kind of comment or input would be greatly appreciated */
0 Kudos
SaPu
Contributor
Contributor

Hello LucD

Thanks, this works great. What should I do to get also the drive letter, e.g. Path=Disk1 Letter=C:\?

regards.

SaPu

0 Kudos
LucD
Leadership
Leadership

That is not so straightforward.

The drive letter is linked to the partition you defined inside the guest OS.

You can for example create 2 or more partitions on 1 virtual disk.

There is an excellent script that does this, that also appeared in our book, and you can find it here.

The function is called Get-VMDiskMapping.


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

View solution in original post

0 Kudos