PowerCLI

 View Only
Expand all | Collapse all

How to list delta files in all datastores

  • 1.  How to list delta files in all datastores

    Posted Oct 08, 2008 04:06 PM

    I've recently found the VI Toolkit and have learned a lot from these forums.

    I'd like to be able to search the contents of all datastores for the existence of any delta files, indicating the presence of lingering snapshots...old or new.

    I'd like to see the paths, filenames, sizes and dates of any delta files.

    Can anyone point me in the right direction?

    Thank you...Mike



  • 2.  RE: How to list delta files in all datastores
    Best Answer

    Posted Oct 08, 2008 06:29 PM

    This is pretty easy with the VI Toolkit Community Extensions.

    If you use that, you can run

    get-datastore | get-tkedatastorefile | where { $_.Path -match "delta" }

    to find these files.

    The extensions require PowerShell 2 CTP2. If that's a non-starter for you for some reason the script code should at least tell you enough to get started.



  • 3.  RE: How to list delta files in all datastores

    Posted Oct 08, 2008 07:58 PM

    Wow...that was easy...thank you very much.

    Is there an easy way to have the output not truncate, so I can see the whole Path and Modification fields?

    Path Size Modification Datastore

    [HNVMW30001SAN01... 16777216 1/14/2008 7:00:1... HNVMW30001SAN01

    [HNVMW30001SAN01... 16777216 1/14/2008 7:00:1... HNVMW30001SAN01

    Thank you...Mike



  • 4.  RE: How to list delta files in all datastores

    Posted Oct 08, 2008 08:10 PM

    Either pipe the output into

    format-list *

    or you can pipe it into

    select Path, Modification

    .



  • 5.  RE: How to list delta files in all datastores

    Posted Oct 08, 2008 08:20 PM

    Perfect...thanks again...but...when I run the following commands separately from the Powershell command line they work fine...

    -


    Add-PSSnapin VMWare.VImAutomation.Core

    "C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\Scripts\Initialize-VIToolkitEnvironment.ps1"

    Add-Module "C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\coreModule\viToolkitExtensions.psm1"

    Connect-VIServer -Server 192.168.xxx.xxx -User xxx -Password xxx

    Get-Datastore | get-tkedatastorefile | where { $_.Path -match "delta" } | format-list * > c:\deltachk.txt

    -


    ...but when I run them inside a script using "PowerShell.exe c:\deltachk.ps1" I get the following errors...

    -


    Get-View : 10/10/2008 11:28:32 AM Get-View You are not currently connected to any servers. Please connect first using Connect-VIServer or one of its aliases.

    At C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\coreModule\viToolkitExtensions.psm1:460 char:28

    + $datastoreView = get-view <<<< $ds.id

    Get-View : The argument cannot be null or empty.

    At C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\coreModule\viToolkitExtensions.psm1:461 char:31

    + $datastoreBrowser = get-view <<<< $datastoreView.browser

    You cannot call a method on a null-valued expression.

    At C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\coreModule\viToolkitExtensions.psm1:468 char:59

    + $task = $datastoreBrowser.SearchDatastoreSubFolders_Task <<<< ("[$name] /$subpath", $spec)

    -


    ...what am I doing wrong???

    Thank you...MIke

    Message was edited by: kastek



  • 6.  RE: How to list delta files in all datastores

    Posted Oct 14, 2008 04:45 PM

    Can anyone see what I'm doing wrong in thepost above?

    Thank you...Mike



  • 7.  RE: How to list delta files in all datastores

    Posted Oct 14, 2008 05:04 PM

    The message says you are not connected.

    So perhaps add a Connect-VIServer at the beginning of your c:\deltachk.ps1 script.



  • 8.  RE: How to list delta files in all datastores

    Posted Oct 14, 2008 05:31 PM

    Thanks LucD...but I do call the "Connect-VIServer" command already?



  • 9.  RE: How to list delta files in all datastores

    Posted Oct 14, 2008 05:35 PM

    To further analyse what exactly goes wrong it would perhaps be useful if you posted the script.



  • 10.  RE: How to list delta files in all datastores

    Posted Oct 14, 2008 05:39 PM

    Script attached...



  • 11.  RE: How to list delta files in all datastores

    Posted Oct 14, 2008 05:42 PM

    I think you'd be ok if you ditched line 4.

    Skark166



  • 12.  RE: How to list delta files in all datastores

    Posted Oct 14, 2008 05:52 PM

    Line 4...you mean the one that adds the viToolkitExtensions.psm1?

    If I don't have that the "get-tkedatastorefile" command is not recognized.



  • 13.  RE: How to list delta files in all datastores

    Posted Oct 14, 2008 05:58 PM

    The problem may be that the connection is not actually succeeding.

    Can you replace line 6 with just "get-datastore" and see if you get any output?



  • 14.  RE: How to list delta files in all datastores

    Posted Oct 14, 2008 06:02 PM

    Yes I do get output...and just to clarify...when I issue the commands one at a time inside powershell it works fine...just doesn't work via the script?



  • 15.  RE: How to list delta files in all datastores

    Posted Oct 14, 2008 05:59 PM

    Add-Module is a cmdlet from PowerShell v2, and PowerShell 2 CTP2 is required for the VITK Community Extensions.

    So that should be ok.

    And I can't see anything wrong in the script.

    Are you sure the Connect-VIServer is working ?

    Do you see the "Name Port User" message in the output ?



  • 16.  RE: How to list delta files in all datastores

    Posted Oct 14, 2008 06:13 PM

    Yes I do see the "Name Port User" in the output



  • 17.  RE: How to list delta files in all datastores

    Posted Oct 14, 2008 06:25 PM

    There is a scoping problem with the $defaultVIServer variable.

    This is a workaround: in your script change:

    Connect-VIServer -Server 192.168.xxx.xxx -User xxx -Password xxx

    to

    $server = Connect-VIServer -Server 192.168.xxx.xxx -User xxx -Password xxx
    $global:defaultVIServer = $server
    

    It seems there is something peculiar about script cmdlets that makes them run in a different context from other cmdlets. The way we populate and use $defaultVIServer is probably not helping.



  • 18.  RE: How to list delta files in all datastores

    Posted Oct 14, 2008 06:43 PM

    Still not working...same error



  • 19.  RE: How to list delta files in all datastores

    Posted Oct 15, 2008 05:57 PM

    Des anyone have a method to search all datastores for "delta" files, without using the VI Toolkit Community Extensions?



  • 20.  RE: How to list delta files in all datastores

    Posted Oct 15, 2008 06:38 PM

    Sure, something I had lying around in my goodies chest :smileywink:

    function Get-DatastoreDir ($DSName){
    
      $dsImpl = Get-Datastore -Name $DSName
      $ds = Get-Datastore -Name $DSName | Get-View
      $dsBrowser = Get-View -Id $ds.Browser
    
      $datastorepath = "[http://" + $ds.Summary.Name + "|http://" + $ds.Summary.Name + "]"
      
      $searchspec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
      $searchSpec.matchpattern = "*"
      $searchSpec.details = New-Object VMware.Vim.FileQueryFlags
      $searchSpec.details.fileSize = $true
      $searchSpec.details.fileType = $true
      $searchSpec.details.modification = $true
     
      $taskMoRef = $dsBrowser.SearchDatastoreSubFolders_Task($datastorePath, $searchSpec) 
      $task = Get-View $taskMoRef 
      while ($task.Info.State -eq "running"){$task = Get-View $taskMoRef}
    
      $report = @() 
      foreach ($result in $task.info.Result){
        foreach($file in $result.File){
          if($file.GetType().Name -ne "FolderFileInfo"){
    	    $row = "" | Select Filename, Folder, Filesize, Modification
            $row.Filename = $file.Path
    	    $row.Folder = $result.FolderPath
    	    $row.Filesize = $file.FileSize
    	    $row.Modification = $file.Modification
    	    $report += $row
    	  }
        }
      }
      $report
    }
    
    Get-DatastoreDir <datastore-name> | Where-Object {$_.Filename -match "flat"}
    

    If you want to do this for all your datastores you could change the last line to

    Get-Datastore | %{Get-DatastoreDir $_.Name} | Where-Object {$_.Filename -match "flat"}
    



  • 21.  RE: How to list delta files in all datastores

    Posted Oct 15, 2008 07:46 PM

    That did it..perfectly...thank you very, very much!



  • 22.  RE: How to list delta files in all datastores

    Posted Jul 20, 2009 08:34 PM

    @c_shanklin : your workaround works for me (i got the "You are not currently connected..." case too)

    Thanks !



  • 23.  RE: How to list delta files in all datastores

    Posted Oct 14, 2008 05:05 PM

    I always use this to initialize the toolkit:

    $vmsnap = Get-PSSnapin vmware* -ErrorAction SilentlyContinue
    if ($vmsnap -eq $NULL) { Add-PSSnapIn -Name VMWare.VimAutomation.core }
    Initialize-VIToolkitEnvironment.ps1

    Skark166



  • 24.  RE: How to list delta files in all datastores

    Posted Oct 14, 2008 05:16 PM

    That loads the VITK snapin and defines some aliases and functions (in Initialize-VIToolkitEnvironment.ps1) but it doesn't do a Connect-VIServer.



  • 25.  RE: How to list delta files in all datastores

    Posted Sep 10, 2009 06:32 AM

    Do you know how to get from command line? RCLI.

    I cannot find it.

    Thank you



  • 26.  RE: How to list delta files in all datastores

    Posted Sep 10, 2009 07:21 AM

    That should normally be in the vifs.pl command but I'm afraid it doesn't allow masking nor recursive folder listings.