VMware Cloud Community
bvi1006
Enthusiast
Enthusiast

Get all .vmx files with 0 bytes

Hi,

I am trying to find .vmx files which are 0 bytes. I am using a portion of LucD's script but I am not sure what I am doing :smileyconfused: ..... can someone help? Eventually I would like save all of the file names which are 0 bytes into a file. Basically, even if I skip the loop  and just run this against 1 datastore the script seems to never end or return anything.

Thanks!

$ESXHost = Get-Cluster $Cluster | Get-VMHost | select -First 1

  

$Datastores = Get-Datastore | where {$_.Name -notlike "*local"}

  

foreach ($Datastore in $Datastores) {

 

     $zerobytes = @{}

 

        Get-VM -Datastore  $datastore | %{$_.Extensiondata.LayoutEx.File | where {$_.Name -like "*.vmx"} }

                 New-PSDrive -Name TgtDS -Location $Datastore -PSProvider VimDatastore -Root '\' | Out-Null

 

 

$zerobytes = @(Get-ChildItem -Path TgtDS: -Recurse | where {$_.FolderPath -notmatch ".snapshot" -and $_.Name -like "*.vmx"  -and $_.length -eq 0})

 

PSDrive-NameTgtDS

0 Kudos
3 Replies
LucD
Leadership
Leadership

Try something like this, but note that the datastore psprovider is notr particularly fast.

It might take some time, depending on the number and the size of your datastores.

$clusterName = "MyCluster"
Get-Cluster -Name $clusterName | Get-VMHost | Get-Random | Get-Datastore |
where {$_.Name -notmatch "local" -and $_.Type -eq "VMFS"} | %{
 
New-PSDrive -Name TgtDS -Location $_ -PSProvider VimDatastore -Root '\' | Out-Null
 
Get-ChildItem -Path TgtDS:\ -Recurse |
 
where {$_.ItemType -eq "VmConfigFile" -and $_.Length -eq 0} |
 
Select DatastoreFullPath
 
Remove-PSDrive -Name TgtDS -Confirm:$false
}


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

0 Kudos
bvi1006
Enthusiast
Enthusiast

Hi Luc,

I cannot seem to get any output. I know there is a .vmx file with zero bytes in a subdirectory on one datastore... I have even modified the script to just get the datastore and search recursively.... would you please take a second look? Thanks

0 Kudos
LucD
Leadership
Leadership

Just did some tests, and for me the script reports all 0-byte VMX files it finds.

testvm-vmx.png  testvm-vmx-script.png

Are you sure that the VMX file actually has a 0-byte length ?

Try running this slightly adapted script, it will show all VMX files and their size.

$clusterName = "MyCluster"
Get-Cluster -Name $clusterName | Get-VMHost | Get-Random | Get-Datastore |
where {$_.Name -notmatch "local" -and $_.Type -eq "VMFS"} | %{
 
New-PSDrive -Name TgtDS -Location $_ -PSProvider VimDatastore -Root '\' | Out-Null
 
Get-ChildItem -Path TgtDS:\ -Recurse |
 
where {$_.ItemType -eq "VmConfigFile"} |
 
Select DatastoreFullPath,Length
 
Remove-PSDrive -Name TgtDS -Confirm:$false
}


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

0 Kudos