VMware Cloud Community
MukeshSingh92
Contributor
Contributor

Get all VMs swap file size

I need to find the swap file size of all VMs in my vcenter. My vcenter has 4 host cluster and 5 dataStore Clusters,

I searched for blogs and found one script posted by LucD, but that one is giving some errors.

Below is the script that I tried to use, and attached is the error i got.. ........running on vsphere 6.0

$clusterName = "My DataStore Cluster name"

$dsImpl = Get-Cluster $clusterName | Get-VMHost | Get-Datastore | where {$_.Type -eq "VMFS5"}

$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

Please suggest the required changes I should be doing !!

0 Kudos
2 Replies
vijayrana968
Virtuoso
Virtuoso

1. You have to correct this, here you have to mention compute cluster name, not datastore cluster name :

     $clusterName = "My DataStore Cluster name"

2. C:\File-report.csv, change the export path to something different as seems you have UAC enabled so you get access denied error. create a temp folder and in C: and give path 'C:\temp\File-Report.csv'

0 Kudos
MukeshSingh92
Contributor
Contributor

Did the changes, script runs but there's no output in csv file. Its blanck

0 Kudos