vin01
Expert
Expert

Find VMs which are missing .vmdk descriptor file

Jump to solution

Hello

In a environment where there are multiple VM's which are missing descriptor file and its very hard to find which hard disk vmdk file is missing.

1. VM's are in powered on State.

2. Need to find VM's whose harddisk doesn't consists of .vmdk file.

My idea is to list all VM's and there harddisk with corresponding .vmdk file(descriptor file) . There we find the VM's with no vmdk file will show blank in csv file.

I tried to execute below code but its trimming the harddisk file name and showing vmdk path.

Can some one direct me on correct path please?

foreach ($VM in (Get-VM 'log1' | Sort-Object -Property Name)) {

foreach ($HardDisk in ($VM | Get-HardDisk | Sort-Object -Property Name)){

$ds = Get-Datastore -Name $HardDisk.Filename.Split(']')[0].TrimStart('[')

$vmdkPath = $HardDisk.Filename.Split(']')[1].TrimStart(' ')

""| select @{N="VM";E={$VM.Name}},

@{N="Hard Disk";E={$HardDisk.Name}},

@{N="Datastore";E={$ds.Name}},

@{N="VMDKpath";E={$vmdkPath}},

@{N="VMDK Size";E={($VM.extensiondata.layoutex.file|?{$_.name -contains $HardDisk.filename.replace(".","-flat.")}).size/1GB}},

@{N="Drive Size";E={$HardDisk.CapacityGB}}

}

}

Regards Vineeth.K
0 Kudos
1 Solution

Accepted Solutions
vin01
Expert
Expert

Thanks for your 'VMX Raiders Revisited' Script. With the help of that script I tried this variation and tested on single VM. It worked.

Any modifications will be helpful for faster and accurate output.

foreach ($VM in (Get-VM 'log1' | Sort-Object -Property Name)) {

foreach ($HardDisk in ($VM | Get-HardDisk | Sort-Object -Property Name)){

$ds = Get-Datastore -Name $HardDisk.Filename.Split(']')[0].TrimStart('[')

$vmdkPath = $HardDisk.Filename.Split(']')[1].TrimStart(' ')

$vmdkPathtrim =$vmdkPath.split('/')[1].trimstart('/')

# Set up Search for .VMDK Files in Datastore

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

$vmdksearch = @(Get-ChildItem -Path TgtDS: -Recurse | `

where {$_.FolderPath -notmatch ".snapshot" -and $_.Name -like "$vmdkPathtrim"})

Remove-PSDrive -Name TgtDS

""| select @{N="VM";E={$VM.Name}},

@{N="Hard Disk";E={$HardDisk.Name}},

@{N="Datastore";E={$ds.Name}},

@{N="VMDKpath";E={$vmdkPath}},

@{N="VMDK Size";E={($VM.extensiondata.layoutex.file|?{$_.name -contains $HardDisk.filename.replace(".","-flat.")}).size/1GB}},

@{N="Drive Size";E={$HardDisk.CapacityGB}},

@{N="VMDK Exist In Datastore";E={$vmdksearch}}

}

}

Regards Vineeth.K

View solution in original post

0 Kudos
6 Replies
LucD
Leadership
Leadership

Did you already try to list the files via the VimDatastore PSDrive?


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

0 Kudos
vin01
Expert
Expert

‌no LucD. Can you please direct me to list the files using vimDatastore PSDrive.

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership

Sure, there is a sample script in 1.  Re: DataStore Folder & Size Details

Hope that helps.


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

0 Kudos
vin01
Expert
Expert

Thanks for your 'VMX Raiders Revisited' Script. With the help of that script I tried this variation and tested on single VM. It worked.

Any modifications will be helpful for faster and accurate output.

foreach ($VM in (Get-VM 'log1' | Sort-Object -Property Name)) {

foreach ($HardDisk in ($VM | Get-HardDisk | Sort-Object -Property Name)){

$ds = Get-Datastore -Name $HardDisk.Filename.Split(']')[0].TrimStart('[')

$vmdkPath = $HardDisk.Filename.Split(']')[1].TrimStart(' ')

$vmdkPathtrim =$vmdkPath.split('/')[1].trimstart('/')

# Set up Search for .VMDK Files in Datastore

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

$vmdksearch = @(Get-ChildItem -Path TgtDS: -Recurse | `

where {$_.FolderPath -notmatch ".snapshot" -and $_.Name -like "$vmdkPathtrim"})

Remove-PSDrive -Name TgtDS

""| select @{N="VM";E={$VM.Name}},

@{N="Hard Disk";E={$HardDisk.Name}},

@{N="Datastore";E={$ds.Name}},

@{N="VMDKpath";E={$vmdkPath}},

@{N="VMDK Size";E={($VM.extensiondata.layoutex.file|?{$_.name -contains $HardDisk.filename.replace(".","-flat.")}).size/1GB}},

@{N="Drive Size";E={$HardDisk.CapacityGB}},

@{N="VMDK Exist In Datastore";E={$vmdksearch}}

}

}

Regards Vineeth.K

View solution in original post

0 Kudos
LucD
Leadership
Leadership

Using the VimDatastore provider is not the fastest process I'm afraid.
Had a look at your script, but can't see major improvements.


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

vin01
Expert
Expert

Thanks for your kind support GuruSmiley Happy

As of now I will use this code to fetch missing .vmdk details.

Regards Vineeth.K
0 Kudos