Maelito77
Contributor
Contributor

How to list the contents of a VM folder with PowerCLI

Jump to solution

This is a simple one and I cannot find the answer online.

How can I list the VM files with PowerCLI?

I would like to see these files listed:

Screenshot 2022-11-14 123943.png

 

 

Thanks! 

 

 

Maelito
Tags (1)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

You can use the SearchDatastoreSubFolders method

$vmName = 'MyVM'

$flags = New-Object VMware.Vim.FileQueryFlags
$flags.FileOwner = $true
$flags.FileSize = $true
$flags.FileType = $true
$flags.Modification = $true

$qFloppy = New-Object VMware.Vim.FloppyImageFileQuery
$qFolder = New-Object VMware.Vim.FolderFileQuery
$qISO = New-Object VMware.Vim.IsoImageFileQuery
$qConfig = New-Object VMware.Vim.VmConfigFileQuery
$qConfig.Details = New-Object VMware.Vim.VmConfigFileQueryFlags
$qConfig.Details.ConfigVersion = $true
$qTemplate = New-Object VMware.Vim.TemplateConfigFileQuery
$qTemplate.Details = New-Object VMware.Vim.VmConfigFileQueryFlags
$qTemplate.Details.ConfigVersion = $true
$qDisk = New-Object VMware.Vim.VmDiskFileQuery
$qDisk.Details = New-Object VMware.Vim.VmDiskFileQueryFlags
$qDisk.Details.CapacityKB = $true
$qDisk.Details.DiskExtents = $true
$qDisk.Details.DiskType = $true
$qDisk.Details.HardwareVersion = $true
$qDisk.Details.Thin = $true
$qLog = New-Object VMware.Vim.VmLogFileQuery
$qRAM = New-Object VMware.Vim.VmNvramFileQuery
$qSnap = New-Object VMware.Vim.VmSnapshotFileQuery

$searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
$searchSpec.details = $flags
$searchSpec.Query = $qFloppy, $qFolder, $qISO, $qConfig, $qTemplate, $qDisk, $qLog, $qRAM, $qSnap
$searchSpec.sortFoldersFirst = $true

$vm = Get-VM -Name $vmName
Get-Datastore -RelatedObject $vm |
ForEach-Object -Process {
    $dsBrowser = Get-View -Id $_.ExtensionData.browser
    $rootPath = "[$($_.Name)] $($vm.Name)"
    $dsBrowser.SearchDatastoreSubFolders($rootPath, $searchSpec) |
    ForEach-Object -Process {
        $folder = $_
        $folder.File |
        Select @{N='Folder';E={$folder.FolderPath}},
            @{N='Type';E={$_.GetType().Name.Replace('Info','')}},
            Path,FileSize,Modification,Owner
    }
}

 Or use the PSProvider (which is slower and gives less information)

$vmName = 'MyVM'

$vm = Get-VM -Name $vmName
Get-Datastore -RelatedObject $vm |
ForEach-Object -Process {
    New-PSDrive -Location $_ -Name DS -PSProvider VimDatastore -Root '\' | Out-Null
    Get-ChildItem -Path "DS:\$($vm.Name)" |
    Select DatastoreFullPath,ItemType,LastWriteTime,Length | Format-List
    Remove-PSDrive -Name DS -Confirm:$false
}


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

View solution in original post

2 Replies
LucD
Leadership
Leadership

You can use the SearchDatastoreSubFolders method

$vmName = 'MyVM'

$flags = New-Object VMware.Vim.FileQueryFlags
$flags.FileOwner = $true
$flags.FileSize = $true
$flags.FileType = $true
$flags.Modification = $true

$qFloppy = New-Object VMware.Vim.FloppyImageFileQuery
$qFolder = New-Object VMware.Vim.FolderFileQuery
$qISO = New-Object VMware.Vim.IsoImageFileQuery
$qConfig = New-Object VMware.Vim.VmConfigFileQuery
$qConfig.Details = New-Object VMware.Vim.VmConfigFileQueryFlags
$qConfig.Details.ConfigVersion = $true
$qTemplate = New-Object VMware.Vim.TemplateConfigFileQuery
$qTemplate.Details = New-Object VMware.Vim.VmConfigFileQueryFlags
$qTemplate.Details.ConfigVersion = $true
$qDisk = New-Object VMware.Vim.VmDiskFileQuery
$qDisk.Details = New-Object VMware.Vim.VmDiskFileQueryFlags
$qDisk.Details.CapacityKB = $true
$qDisk.Details.DiskExtents = $true
$qDisk.Details.DiskType = $true
$qDisk.Details.HardwareVersion = $true
$qDisk.Details.Thin = $true
$qLog = New-Object VMware.Vim.VmLogFileQuery
$qRAM = New-Object VMware.Vim.VmNvramFileQuery
$qSnap = New-Object VMware.Vim.VmSnapshotFileQuery

$searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
$searchSpec.details = $flags
$searchSpec.Query = $qFloppy, $qFolder, $qISO, $qConfig, $qTemplate, $qDisk, $qLog, $qRAM, $qSnap
$searchSpec.sortFoldersFirst = $true

$vm = Get-VM -Name $vmName
Get-Datastore -RelatedObject $vm |
ForEach-Object -Process {
    $dsBrowser = Get-View -Id $_.ExtensionData.browser
    $rootPath = "[$($_.Name)] $($vm.Name)"
    $dsBrowser.SearchDatastoreSubFolders($rootPath, $searchSpec) |
    ForEach-Object -Process {
        $folder = $_
        $folder.File |
        Select @{N='Folder';E={$folder.FolderPath}},
            @{N='Type';E={$_.GetType().Name.Replace('Info','')}},
            Path,FileSize,Modification,Owner
    }
}

 Or use the PSProvider (which is slower and gives less information)

$vmName = 'MyVM'

$vm = Get-VM -Name $vmName
Get-Datastore -RelatedObject $vm |
ForEach-Object -Process {
    New-PSDrive -Location $_ -Name DS -PSProvider VimDatastore -Root '\' | Out-Null
    Get-ChildItem -Path "DS:\$($vm.Name)" |
    Select DatastoreFullPath,ItemType,LastWriteTime,Length | Format-List
    Remove-PSDrive -Name DS -Confirm:$false
}


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

Maelito77
Contributor
Contributor

Thanks @LucD - trully an honour! 

I wonder why VMware  don't have a one liner for something so simple?

Thanks for assisting me.

Maelito
0 Kudos