Automation

 View Only
Expand all | Collapse all

how to list files in datastores?

  • 1.  how to list files in datastores?

    Broadcom Employee
    Posted Jun 25, 2008 04:09 AM

    Hi,

    Would like to find out how to list all the files and size in datastore? The objective is to help generate a spreadsheet for storage planning.

    Anyone has a sample script?

    Could not seem to get it working with get-datastore.



  • 2.  RE: how to list files in datastores?
    Best Answer

    Broadcom Employee
    Posted Jun 25, 2008 07:37 AM

    Hi

    You can use SearchDatastoreSubFolders of the HostDatastoreBrowser object to browse all files in the datastore

    $ds = Get-Datastore <datastorename> | %{Get-View $_.Id}

    $fileQueryFlags = New-Object VMware.Vim.FileQueryFlags

    $fileQueryFlags.FileSize = $true

    $fileQueryFlags.FileType = $true

    $fileQueryFlags.Modification = $true

    $searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec

    $searchSpec.details = $fileQueryFlags

    $searchSpec.sortFoldersFirst = $true

    $dsBrowser = Get-View $ds.browser

    $rootPath = "[]" -f ($ds.summary.Name)

    $searchResult = $dsBrowser.SearchDatastoreSubFolders($rootPath, $searchSpec)

    foreach ($folder in $searchResult) {

    foreach ($fileResult in $folder.File) {

    $file = "" | select Name, Size, Modified, FullPath

    $file.Name = $fileResult.Path

    $file.Size = $fileResult.Filesize

    $file.Modified = $fileResult.Modification

    $file.FullPath = $folder.FolderPath + $file.Name

    $file | out-default

    }

    }

    You can also check query property of the SearchSpec object in order to filter files by type(i.e. only VmDisk or Vmconfig files)Regards,

    Yasen



  • 3.  RE: how to list files in datastores?

    Broadcom Employee
    Posted Jun 25, 2008 08:01 AM

    Yasen,

    Is that perl or Powershell script? I would like to do with the Powershell VI Toolkit if possible.



  • 4.  RE: how to list files in datastores?

    Broadcom Employee
    Posted Jun 25, 2008 08:18 AM

    It seems the code didn't show correct on IE. I've update the script code and I hope it's looking good now.

    Yes It's VI Toolkit for Windows script



  • 5.  RE: how to list files in datastores?

    Broadcom Employee
    Posted Jun 25, 2008 09:26 AM

    Yasen,

    Perfect, it works fine except the output is on to screen with repeated header lines.

    How can it be modified to write to a CSV file? I tried the export-CSV, it seems that there is only 1 line of output.



  • 6.  RE: how to list files in datastores?

    Broadcom Employee
    Posted Jun 25, 2008 10:58 AM

    I've just collect all files into a hashtable and then pipe then to export-cvs

    $fileList = @{}

    foreach ($folder in $searchResult) {

    foreach ($fileResult in $folder.File) {

    $file = "" | select Name, Size, Modified, FullPath

    $file.Name = $fileResult.Path

    $file.Size = $fileResult.Filesize

    $file.Modified = $fileResult.Modification

    $file.FullPath = $folder.FolderPath + $file.Name

    fileList.Add($file.FullPath, $file)

    }

    }

    $fileList.Values | select Name, Size, Modified, FullPath | export-csv "datastore.csv"

    Regards,

    Yasen



  • 7.  RE: how to list files in datastores?

    Broadcom Employee
    Posted Jun 26, 2008 08:34 AM

    Perfect, that's what I need. Appreciate it very much. Cheers!



  • 8.  RE: how to list files in datastores?

    Broadcom Employee
    Posted Jun 26, 2008 09:57 AM

    How can I loop it, so that I can capture multiple selected datastore? e.g. of certain naming convention type?

    Pardon me, I am very new to PowerShell, still trying to learn the basics.

    Cheers!



  • 9.  RE: how to list files in datastores?

    Broadcom Employee
    Posted Jun 26, 2008 11:42 AM

    You can just add the code for one datastore in a loop for each datastore you get:

    Get-Datastore <filter> | % {

    $ds = Get-View $_.Id

    process datastore files here

    }

    So the script will look smth like this:

    Get-Datastore <search criteria> | % {

    $ds = Get-View $_.Id

    $fileQueryFlags = New-Object VMware.Vim.FileQueryFlags

    $fileQueryFlags.FileSize = $true

    $fileQueryFlags.FileType = $true

    $fileQueryFlags.Modification = $true

    $searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec

    $searchSpec.details = $fileQueryFlags

    $searchSpec.sortFoldersFirst = $true

    $rootPath = "[{0}]" -f ($ds.summary.Name)

    $dsBrowser = Get-View $ds.browser

    $searchResult = $dsBrowser.SearchDatastoreSubFolders($rootPath, $searchSpec)

    $fileList = @{}

    foreach ($folder in $searchResult) {

    foreach ($fileResult in $folder.File) {

    $file = "" | select Name, Size, Modified, FullPath

    $file.Name = $fileResult.Path

    $file.Size = $fileResult.Filesize

    $file.Modified = $fileResult.Modification

    $file.FullPath = $folder.FolderPath + $file.Name

    $fileList.Add($file.FullPath, $file)

    }

    }

    }

    $fileList.Values | select Name, Size, Modified, FullPath | export-csv "datastore.csv"

    Regards,

    Yasen



  • 10.  RE: how to list files in datastores?

    Broadcom Employee
    Posted Jun 27, 2008 02:51 AM

    For some reason, some datastore takes a very very long time to generate. Any particular reason?



  • 11.  RE: how to list files in datastores?

    Posted Aug 06, 2008 09:26 AM

    I am trying to create a script that will list all vmdk's on all datastores. This script given here is my starting point. However I cannot make this work.

    $ds = Get-Datastore | %{Get-View $_.Id}

    Script errors on this. Each time I run this script I get an error like

    A parameter cannot be found that matches parameter name 'Datastore-datastore-20029'

    At line 18, position 110

    $ds = Get-Datastore | %{Get-View $_.Id}

    So I am thinking the problem is $_.id is "Datastore-datastore-20029" and Get-view does not recognize this. When I removed $_.I, i received a popup to enter a MoRef.

    Anyonhe cahelp me with this? I just need a script that lists all files on a datastore.



  • 12.  RE: how to list files in datastores?

    Posted Aug 06, 2008 09:40 AM

    I suspect you are using VI Toolkit v1 (use Get-VIToolkitversion to check, it should return build 103777).

    If yes, the Get-View cmdlet has changed.

    Do a Get-Help Get-View to see the new syntax.

    PS: try to open a new thread for a new question, it makes it easier for other community members to follow



  • 13.  RE: how to list files in datastores?

    Posted Aug 06, 2008 09:54 AM

    Thanks for reply,

    I used this thread because I thought code was the problem.

    It is weird though, this morning I installed "VMware-Vim4PS-1.0.0-103777.exe, however Get-ViToolkitversion is not recognised as a cmdlet. I guess I reïnstall everything. Thanks.



  • 14.  RE: how to list files in datastores?

    Posted Aug 06, 2008 09:47 AM

    Hi all,

    In VI Toolkit v1.0 there is a datastore provider. This may be very interesting feature to use in some cases. Here is how you can use it:

    First you need to mount the datastore provider to some drive:

    PS C:\&gt; Get-Datastore -Name "storage1 (3)"| New-DatastoreDrive -Name "ds"

    Name Provider Root CurrentLocation

    -


    -


    -


    -


    ds VimDatastore \Datastore\443@10.23.113.41\Data...

    Now you can 'cd' to it and do all directory operations you like:

    PS C:\&gt; cd ds:

    PS ds:\&gt; dir

    Datastore path:

    LastWriteTime Type Length Name

    -


    -


    -


    -


    2/25/2008 6:11 PM Folder Win2003 64bit

    2/24/2008 8:19 AM Folder Win2003 32bit

    2/24/2008 12:07 AM Folder WinXP 32bit

    6/16/2008 2:27 PM Folder RedHat 9

    2/24/2008 11:10 PM Folder WinXP 64bit

    6/20/2008 11:57 AM Folder PS20_win2003

    8/6/2008 11:15 AM Folder testMachine_Vista_32bit

    8/5/2008 5:07 PM Folder testMachine_2003_64bit

    8/6/2008 11:25 AM Folder testMachine_XP_64bit

    6/6/2008 2:22 PM Folder Ubuntu

    6/24/2008 2:29 PM Folder WinVista Business 64b...

    8/6/2008 11:00 AM Folder VC4.0



  • 15.  RE: how to list files in datastores?

    Posted Dec 18, 2012 09:40 AM

    Great script. Thanks for the time taken to put it together.

    Just one small correction:

    As it is, it will only export to CSV the contents of the last Datastore, because on each new call of the block a new blank $fileList is created.

    To report all of the datastores, you must move the line "$fileList = @{}" out of the loop, for instance before the "Get-Datastore" command.

    Thanks.

    Ferran.



  • 16.  RE: how to list files in datastores?

    Posted Aug 11, 2022 01:50 PM

    The script is listing all the files in the folder. But how to list files in a subfolder.



  • 17.  RE: how to list files in datastores?

    Posted Jun 25, 2008 01:50 PM

    Hi,

    Would like to find out how to list all the files and size in datastore? The objective is to help generate a spreadsheet for storage planning.

    Anyone has a sample script?

    Could not seem to get it working with get-datastore.

    Another alternative if you're feeling a bit adventurous would be to use the community extensions. There is a cmdlet within the extension called get-datastorefiles.

    Basically the full script you would use is:

    (new-object net.webclient).DownloadString("http://communities.vmware.com/servlet/JiveServlet/downloadBody/6051-102-1-3481/Extensions.psm1") > $env:temp/Extensions.psm1
    add-module $env:temp/Extensions.psm1
    get-datastorefiles (get-datastore ds1,ds2,ds3)
    
    

    This will return an array of objects that you can use on the pipeline, etc. Note: This requires the latest PowerShell 2 CTP.



  • 18.  RE: how to list files in datastores?

    Posted Sep 24, 2021 11:43 PM

    first connect your vCenter via Powercli, then run following code to enumerate datastore files. For me 79 datastores in 9 vcenters, it finished in half an hour and got totally 4K+ files. I think this speed is accetable.

    $vmstores=dir vmstores:
    $dcs=dir $vmstores.pspath
    $vmfss=dir $dcs.pspath
    $dsfiles=@()
    $vmfss | %{$dsfiles+=dir $_.pspath -rec | ? itemtype -eq File}
    $dsfiles