VMware Cloud Community
NuggetGTR
VMware Employee
VMware Employee
Jump to solution

Require script to list files with sizes on all datastores in a cluster

HI ALL,

Please be gentle, im not new to Vmware but new to powershell/powercli and need your help.

is this something power cli can do or is there tools available that will do this for me?

I need to generate a list of all vm files in all datastores available to a cluster and their sizes to go along with it.

I am mainly interested in the .log files and the vmdk files but I don’t mind if it lists all.

I don’t mind how its laid out im not after something fancy just a list type of output something like

/

I have done some searching but there is nothing that I have found that will do this.

My environment is currently esx 3.0.2 with vcenter servers. (currently in the process of upgrading to 4)

Thanks heaps for any help

________________________________________ Blog: http://virtualiseme.net.au VCDX #201 Author of Mastering vRealize Operations Manager
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Yes, that's a know "feature" when using PowerCLI 4u1 against a VI 3.x environment.

There is a bypass, try this

$dsImpl = Get-Cluster <cluster-name> | Get-VMHost | Get-Datastore | where {$_.Type -eq "VMFS"}
$dsImpl | % {
	$ds = $_ | Get-View
	$path = ""
	$dsBrowser = Get-View $ds.Browser
	$spec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
	$spec.Details = New-Object VMware.Vim.FileQueryFlags
	$spec.Details.fileSize = $true
	$spec.Details.fileType = $true
	$vmdkQry = New-Object VMware.Vim.VmDiskFileQuery
	$spec.Query = (New-Object VMware.Vim.VmDiskFileQuery),(New-Object VMware.Vim.VmLogFileQuery)
	#Workaround for vSphere 4 fileOwner bug
	if ($dsBrowser.Client.Version -eq "Vim4") {
		$spec = [http://VMware.Vim.VIConvert|http://VMware.Vim.VIConvert]::ToVim4($spec)
		$spec.details.fileOwnerSpecified = $true
		$dsBrowserMoRef = [http://VMware.Vim.VIConvert|http://VMware.Vim.VIConvert]::ToVim4($dsBrowser.MoRef);
		$taskMoRef = $dsBrowser.Client.VimService.SearchDatastoreSubFolders_Task($dsBrowserMoRef, $path, $spec)
		$result = [http://VMware.Vim.VIConvert|http://VMware.Vim.VIConvert]::ToVim($dsBrowser.WaitForTask([http://VMware.Vim.VIConvert|http://VMware.Vim.VIConvert]::ToVim($taskMoRef)))
	} else {
		$taskMoRef = $dsBrowser.SearchDatastoreSubFolders_Task($path, $spec)
		$task = Get-View $taskMoRef
		while("running","queued" -contains $task.Info.State){
			$task.UpdateViewData("Info")
		}
		$result = $task.Info.Result
	}

	$result | % {
		$vmName = ([regex]::matches($_.FolderPath,"\[\w*\]\s*([^/]+)"))[0].groups[1].value
		$_.File | % {
			New-Object PSObject -Property @{
				DSName = $ds.Name
				VMname = $vmName
				FileName = $_.Path
				FileSize = $_.FileSize
			}
		}
	}
} | Export-Csv "C:\File-report.csv" -NoTypeInformation -UseCulture

I attached the script to avoid any problems with the square brackets.

And the regex expression is updated to accomodate guest names with blanks.






____________

Blog: LucD notes

Twitter: lucd22


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

View solution in original post

0 Kudos
7 Replies
jeveenj
Enthusiast
Enthusiast
Jump to solution

Hi,

You can try below code snippet, for ESX 3.X it will give file name only, no size since it does not support data object VirtualmachineFileLayoutExFileInfo it is supported in ESX 4.

 Connect-VIServer -Server <server> -User <user> -Password <password> 
$allVM = Get-Cluster | Get-VM 
$no = $allVM.Count 
for ($i = 0; $i -lt $no; $i++ ) {
$name = $allVM.SyncRoot[$i].Name  
$vm = Get-VM -Name $name | Get-View  
# Layout property of VM works for ESX 3.x, but it does not size. It only displays name of the file.
$vm.Layout | Select-Object -Property ConfigFile,LogFile,Disk,Snapshot,SwapFile
#since "Disk" returns the key, this will give the Disk file name  
$vm.Layout.Disk | Select-Object -Property Diskfile
#In 4.0, LayoutEx has been introduced, which returns both name and size. Uncomment below to run script for 4.0
#$vm.LayoutEx.File | Select-Object -Property Name, Size
}

Hope this helps.

-If you found this information useful, please consider awarding points for Correct or Helpful.
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Have a go at the following script.

It uses the SearchDatastoreSubFolders_Task method from the SDK.

The script will output all the information into a CSV file.

This version requires PowerShell v2 RTM.

If you want a PowerShell v1 version let me know.

$clusterName = <your-cluster-name>
$dsImpl = Get-Cluster $clusterName | Get-VMHost | Get-Datastore | where {$_.Type -eq "VMFS"}
$dsImpl | % {
	$ds = $_ | Get-View
	$path = ""
	$dsBrowser = Get-View $ds.Browser
	$spec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
	$spec.Details = New-Object VMware.Vim.FileQueryFlags
	$spec.Details.fileSize = $true
	$spec.Details.fileType = $true
	$vmdkQry = New-Object VMware.Vim.VmDiskFileQuery
	$spec.Query = (New-Object VMware.Vim.VmDiskFileQuery),(New-Object VMware.Vim.VmLogFileQuery)
	
	$taskMoRef = $dsBrowser.SearchDatastoreSubFolders_Task($path, $spec)
	$task = Get-View $taskMoRef
	while("running","queued" -contains $task.Info.State){
		$task.UpdateViewData("Info")
	}
	$task.Info.Result | %{
			$vmName = ([regex]::matches($_.FolderPath,"\[\w*\]\s*(\w+)"))[0].groups[1].value
			$_.File | %{
				New-Object PSObject -Property @{
					DSName = $ds.Name
					VMname = $vmName
					FileName = $_.Path
					FileSize = $_.FileSize
				}
			}
	}
} | Export-Csv "C:\File-report.csv" -NoTypeInformation -UseCulture

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
NuggetGTR
VMware Employee
VMware Employee
Jump to solution

Thanks heaps for the help

LucD im getting an error with your script.

Exception calling "SearchDatastoreSubFolders_Task" with "2" argument(s): "Not i

nitialized: boolean fileOwner"

At C:\powercli\list.ps1:14 char:56

+ $taskMoRef = $dsBrowser.SearchDatastoreSubFolders_Task <<<< ($path, $spec

)

+ CategoryInfo : NotSpecified: (Smiley Happy [], MethodInvocationException

+ FullyQualifiedErrorId : DotNetMethodException

Get-View : Cannot validate argument on parameter 'Id'. The argument is null or

empty. Supply an argument that is not null or empty and then try the command ag

ain.

At C:\powercli\list.ps1:15 char:18

+ $task = Get-View <<<< $taskMoRef

+ CategoryInfo : InvalidData: (Smiley Happy , ParameterBindingVal

idationException

+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom

ation.Commands.DotNetInterop.GetVIView

any thoughts?

Cheers again Smiley Happy

________________________________________ Blog: http://virtualiseme.net.au VCDX #201 Author of Mastering vRealize Operations Manager
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, that's a know "feature" when using PowerCLI 4u1 against a VI 3.x environment.

There is a bypass, try this

$dsImpl = Get-Cluster <cluster-name> | Get-VMHost | Get-Datastore | where {$_.Type -eq "VMFS"}
$dsImpl | % {
	$ds = $_ | Get-View
	$path = ""
	$dsBrowser = Get-View $ds.Browser
	$spec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
	$spec.Details = New-Object VMware.Vim.FileQueryFlags
	$spec.Details.fileSize = $true
	$spec.Details.fileType = $true
	$vmdkQry = New-Object VMware.Vim.VmDiskFileQuery
	$spec.Query = (New-Object VMware.Vim.VmDiskFileQuery),(New-Object VMware.Vim.VmLogFileQuery)
	#Workaround for vSphere 4 fileOwner bug
	if ($dsBrowser.Client.Version -eq "Vim4") {
		$spec = [http://VMware.Vim.VIConvert|http://VMware.Vim.VIConvert]::ToVim4($spec)
		$spec.details.fileOwnerSpecified = $true
		$dsBrowserMoRef = [http://VMware.Vim.VIConvert|http://VMware.Vim.VIConvert]::ToVim4($dsBrowser.MoRef);
		$taskMoRef = $dsBrowser.Client.VimService.SearchDatastoreSubFolders_Task($dsBrowserMoRef, $path, $spec)
		$result = [http://VMware.Vim.VIConvert|http://VMware.Vim.VIConvert]::ToVim($dsBrowser.WaitForTask([http://VMware.Vim.VIConvert|http://VMware.Vim.VIConvert]::ToVim($taskMoRef)))
	} else {
		$taskMoRef = $dsBrowser.SearchDatastoreSubFolders_Task($path, $spec)
		$task = Get-View $taskMoRef
		while("running","queued" -contains $task.Info.State){
			$task.UpdateViewData("Info")
		}
		$result = $task.Info.Result
	}

	$result | % {
		$vmName = ([regex]::matches($_.FolderPath,"\[\w*\]\s*([^/]+)"))[0].groups[1].value
		$_.File | % {
			New-Object PSObject -Property @{
				DSName = $ds.Name
				VMname = $vmName
				FileName = $_.Path
				FileSize = $_.FileSize
			}
		}
	}
} | Export-Csv "C:\File-report.csv" -NoTypeInformation -UseCulture

I attached the script to avoid any problems with the square brackets.

And the regex expression is updated to accomodate guest names with blanks.






____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
NuggetGTR
VMware Employee
VMware Employee
Jump to solution

LucD mate you are a legend!! works beautifully

Thanks so much for that I will buy you a beer or 10 if our paths ever cross.

________________________________________ Blog: http://virtualiseme.net.au VCDX #201 Author of Mastering vRealize Operations Manager
0 Kudos
NuggetGTR
VMware Employee
VMware Employee
Jump to solution

LucD mate just one question if i ever wanted to add swap file or snapshot file im guessing i can just add to the query line "$spec.Query = (New-Object VMware.Vim.VmDiskFileQuery),(New-Object VMware.Vim.VmLogFileQuery)" ?

Ive found the swap file one which would be "(New-Object VMware.Vim.VmSnapshotFileQuery)" is this correct?

and would would the swap be? i couldnt see one for it.

Doesnt matter if i cant I have what i needed the most.

Cheers heaps

________________________________________ Blog: http://virtualiseme.net.au VCDX #201 Author of Mastering vRealize Operations Manager
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You're right, there is, afaik, no way to extract the .vswp files specifically.

What you can do, is use the "general" FileQuery for the search.

This will return all files and you can then extract the .vswp files in the script.

Attached an updated script (note that the extract of the .vswp files is not yet in that script, the CSV contains all the files).

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos