-->

How to Find orphaned VMDK’s vSphere VMs using PowerCLI

What is the VMDK file ?

vmdk file's content is the virtual machine's data, with a small portion allotted to virtual machine overhead. If the virtual machine is connected directly to a physical disk, rather than to a virtual disk, the . vmdk file stores information about the partitions the virtual machine is allowed to access.

How to Find orphaned VMDK’s in vCenter by using PowerClI

Login to Powercli and connect to vCenter.

 >> Connect-VI server

>> Enter required credentials here.

Please save the below script as Ps1 formate and run it.

Note: XXXX means vCenter IP/FQDN.

---------------------------------------------------------------------------------------------------------------

$arrayVC = "XXXXXX"

Foreach ($strVC in $arrayVC)

{

 Connect-VIServer $strVC 

 $arrUsedDisks = Get-VM | Get-HardDisk | %{$_.filename}

 $arrUsedDisks += get-template | Get-HardDisk | %{$_.filename}

 $arrDS = Get-Datastore

 Foreach ($strDatastore in $arrDS)

 {

 $strDatastoreName = $strDatastore.name

 $ds = Get-Datastore -Name $strDatastoreName | %{Get-View $_.Id}

 $fileQueryFlags = New-Object VMware.Vim.FileQueryFlags

 $fileQueryFlags.FileSize = $true

 $fileQueryFlags.FileType = $true

 $fileQueryFlags.Modification = $true

 $searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec

 $searchSpec.details = $fileQueryFlags

 $searchSpec.sortFoldersFirst = $true

 $dsBrowser = Get-View $ds.browser

 $rootPath = "["+$ds.summary.Name+"]"

 $searchResult = $dsBrowser.SearchDatastoreSubFolders($rootPath, $searchSpec)

 $myCol = @()

 foreach ($folder in $searchResult)

 {

 foreach ($fileResult in $folder.File)

 {

 $file = "" | select Name, FullPath 

 $file.Name = $fileResult.Path

 $strFilename = $file.Name

 IF ($strFilename)

 {

 IF ($strFilename.Contains(".vmdk")) 

 {

 IF (!$strFilename.Contains("-flat.vmdk"))

 {

 IF (!$strFilename.Contains("delta.vmdk")) 

 {

 $strCheckfile = "*"+$file.Name+"*"

 IF ($arrUsedDisks -Like $strCheckfile){}

 ELSE 

 { 

 $strOutput = $strDatastoreName + " Orphaned VMDK Found: " + $strFilename

 $strOutput

 } 

 }

 } 

 }

 }

 }

 } 

 } 

}

--------------------------------------------------------------------------------

How to Find orphaned VMDK’s vSphere VMs using PowerCLI How to Find orphaned VMDK’s vSphere VMs using PowerCLI Reviewed by Virtulization on April 26, 2021 Rating: 5

No comments:

Home - PageNavi (show/hide)

hide
Powered by Blogger.