VMware Cloud Community
haripetrov
Contributor
Contributor
Jump to solution

How to Display Information About FileName of a Disk?

I am trying to obtain information about all disks which are using "nonpersistent" mode with Get-View. Everything is working with the exception of @{N="Filename";E={$vm.Config.Hardware.Device.Backing.FileName}}

$VMs = Get-View -ViewType VirtualMachine | Where {-not $_.Config.Template}

$Result = @(ForEach ($vm in $VMs){

    $vdisk = $vm.Config.Hardware.Device | where {$_ -is [VMware.Vim.VirtualDisk] -and ($_.Backing.DiskMode -eq "nonpersistent" -or $_.Backing.DiskMode -eq "independent_nonpersistent")}

    if ($vdisk){

        $vdisk | Select @{N="VM";E={$vm.Name}},

            @{N="NonPersistentMode";E={$_.DeviceInfo.Label}},

            @{N="CapacityGB";E={$_.capacityInKB/1024/1024}},

            @{N="Filename";E={$vm.Config.Hardware.Device.Backing.FileName}}

    }

})

$Result

This script is originally inspired by this one:

Get-VM | % { Get-HardDisk -VM $_ | Where {$_.Persistence -eq "IndependentPersistent" -or $_.Persistence -eq "IndependentNonPersistent"} } | Select Parent, Name, CapacityGB, Filename, DiskType, Persistence, ScsiCanonicalName | Export-CSV VMs_Persistent_Mode.csv -NoTypeInformation -UseCulture

I can see that in https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.vm.device.VirtualDevice... we have property "FileName" but I don't know how to call it in this case.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this

$diskModes = [VMware.Vim.VirtualDiskMode]::independent_nonpersistent,[VMware.Vim.VirtualDiskMode]::nonpersistent

foreach($vm in (Get-View -ViewType VirtualMachine -Property Name,Config.Hardware.Device -Filter @{'Config.Template'='False'})){

  $vm.Config.Hardware.Device |

  where{$_ -is [VMware.Vim.VirtualDisk] -and $diskModes -contains $_.Backing.DiskMode} |

  Select  @{N="VM";E={$vm.Name}},

    @{N="Disk";E={$_.DeviceInfo.Label}},

    @{N="Mode";E={$_.Backing.DiskMode}},

    @{N="CapacityGB";E={$_.capacityInKB/1MB}},

    @{N="Filename";E={$_.Backing.FileName}}

}

To get the syntax highlighted code, I currently use the PowerShell ISE addon from Windows PowerShell V3 ISE: Copy As HTML Add-On


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

View solution in original post

0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

The Device property is an array, so you will have to find the correct entry.

Perhaps based on the Label, something like this

$hd = $vm.Config.Hardware.Device | where{$_.Info.Label -eq 'Hard Disk 1'}


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

haripetrov
Contributor
Contributor
Jump to solution

I will continue to search for this information. Smiley Happy

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Let me know if you can find it, otherwise I can give you a sample script.


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

0 Kudos
haripetrov
Contributor
Contributor
Jump to solution

I have not managed to find the information so far. I would appreciate it if you could share your sample script here.

P.S. Would it be possible for you to tell me how you format your code in this way (using color format)?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try something like this

$diskModes = [VMware.Vim.VirtualDiskMode]::independent_nonpersistent,[VMware.Vim.VirtualDiskMode]::nonpersistent

foreach($vm in (Get-View -ViewType VirtualMachine -Property Name,Config.Hardware.Device -Filter @{'Config.Template'='False'})){

  $vm.Config.Hardware.Device |

  where{$_ -is [VMware.Vim.VirtualDisk] -and $diskModes -contains $_.Backing.DiskMode} |

  Select  @{N="VM";E={$vm.Name}},

    @{N="Disk";E={$_.DeviceInfo.Label}},

    @{N="Mode";E={$_.Backing.DiskMode}},

    @{N="CapacityGB";E={$_.capacityInKB/1MB}},

    @{N="Filename";E={$_.Backing.FileName}}

}

To get the syntax highlighted code, I currently use the PowerShell ISE addon from Windows PowerShell V3 ISE: Copy As HTML Add-On


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

0 Kudos
haripetrov
Contributor
Contributor
Jump to solution

Thank you, again. The script is working as expected.

I will try this addon in question.

0 Kudos