Hello,
I have the following script
Get-Datastore "*sata)" | Get-VM | Get-VMGuest | Where { $_.Disks } | select -ExpandProperty Disks VMName
the output is
VmName | Path | Capacity | FreeSpace |
---|---|---|---|
VM1 | C:\ | 12880752640 | 4642414592 |
VM1 | D:\ | 32218386432 | 4818952192 |
VM2 | C:\ | 12880752640 | 4642414592 |
What I'm looking for is an output like
VmName | Path | Datastore |
---|---|---|
VM1 | C:\ | store1 (sata) |
VM1 | D:\ | store3 (sata) |
VM2 | C:\ | store1 (sata) |
Can anyone help me with this?
Regards.
SaPu
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
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
Hi marc,
how come I got multiple error message here:
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
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
no man,
I am using 32 bit WIndows XP with PS 2.0, but anyway thanks for sharing your script here 🙂
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
yes it works now, many-many thanks Luc !
Hello LucD
Thanks, this works great. What should I do to get also the drive letter, e.g. Path=Disk1 Letter=C:\?
regards.
SaPu
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