piyushranusri
Enthusiast
Enthusiast

orphan disk with provisoned space

Jump to solution

Currently am using below script to get orphan vmdk, now am trying to add provisoned space here.

looking suggestion expert, fi you can guide me to get. I tried but its not happening

Import-Module VMware.VimAutomation.Vds

$allVip = @()

foreach($vcenterIP in $allVip){

Connect-VIServer $vcenterIP -User   -Password

switch ($vcenterIP)

{

    '

    Default {$vcenterNAME = "$vcenter" }

}

$report = @()

$arrUsedDisks = Get-View -ViewType VirtualMachine | % {$_.Layout} | % {$_.Disk} | % {$_.DiskFile}

$arrDS = Get-Datastore | Sort-Object -property Name

$outputfile="C:\Windows\addins\Orphan_VMs\$vcenterNAME-OrphanDisk.csv"

foreach ($strDatastore in $arrDS) {

    Write-Host $strDatastore.Name

    $ds = Get-Datastore -Name $strDatastore.Name | % {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.matchPattern = "*.vmdk"

    $searchSpec.sortFoldersFirst = $true

    $dsBrowser = Get-View $ds.browser

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

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

    foreach ($folder in $searchResult)

    {

        foreach ($fileResult in $folder.File)

        {

            if ($fileResult.Path)

            {

                if (-not ($arrUsedDisks -contains ($folder.FolderPath + $fileResult.Path))){

                    $row = "" | Select DS, Path, File, Size, ModDate

                    $row.DS = $strDatastore.Name

                    $row.Path = $folder.FolderPath

                    $row.File = $fileResult.Path

                    $row.Size = $fileResult.FileSize

                    $row.ModDate = $fileResult.Modification

                    $report += $row

                }

            }

        }

    }

}

disconnect-viserver -confirm:$false

$report | Export-Csv -NoTypeInformation "$outputfile"

}

1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Try like this

$report = @()

$arrUsedDisks = Get-View -ViewType VirtualMachine | % {$_.Layout} | % {$_.Disk} | % {$_.DiskFile}

$arrDS = Get-Datastore | Sort-Object -property Name


foreach ($strDatastore in $arrDS) {

    Write-Host $strDatastore.Name

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


    $flags = New-Object VMware.Vim.FileQueryFlags

    $flags.FileSize = $true

    $flags.FileType = $true

    $flags.Modification = $true

      

    $disk = New-Object VMware.Vim.VmDiskFileQuery

    $disk.details = New-Object VMware.Vim.VmDiskFileQueryFlags

    $disk.details.capacityKb = $true

    $disk.details.diskExtents = $true

    $disk.details.diskType = $true

    $disk.details.thin = $true

    $searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec

    $searchSpec.details = $flags

    $searchSpec.Query += $disk

    $searchSpec.sortFoldersFirst = $true

    $searchSpec.details = $flags

    $searchSpec.matchPattern = "*.vmdk"

    $searchSpec.sortFoldersFirst = $true

  

    $dsBrowser = Get-View $ds.browser

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

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

    foreach ($folder in $searchResult)

    {

        foreach ($fileResult in $folder.File)

        {

            if ($fileResult.Path)

            {

                if (-not ($arrUsedDisks -contains ($folder.FolderPath + $fileResult.Path))){

                    $row = "" | Select DS, Path, File, SizeGB, ModDate, ProvisionedGB

                    $row.DS = $strDatastore.Name

                    $row.Path = $folder.FolderPath

                    $row.File = $fileResult.Path

                    $row.SizeGB = [math]::Round($fileResult.FileSize/1GB,1)

                    $row.ModDate = $fileResult.Modification

                    $row.ProvisionedGB = [math]::Round($fileResult.CapacityKb/1MB,1)

                    $report += $row

                }

            }

        }

    }

}


$report


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

View solution in original post

0 Kudos
7 Replies
daphnissov
Immortal
Immortal

Wrong forum, move to PowerCLI.

0 Kudos
scott28tt
VMware Employee
VMware Employee

Moderator: Moved to PowerCLI

piyushranusri
Enthusiast
Enthusiast

Thanks scott28tt

0 Kudos
LucD
Leadership
Leadership

Try like this

$report = @()

$arrUsedDisks = Get-View -ViewType VirtualMachine | % {$_.Layout} | % {$_.Disk} | % {$_.DiskFile}

$arrDS = Get-Datastore | Sort-Object -property Name


foreach ($strDatastore in $arrDS) {

    Write-Host $strDatastore.Name

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


    $flags = New-Object VMware.Vim.FileQueryFlags

    $flags.FileSize = $true

    $flags.FileType = $true

    $flags.Modification = $true

      

    $disk = New-Object VMware.Vim.VmDiskFileQuery

    $disk.details = New-Object VMware.Vim.VmDiskFileQueryFlags

    $disk.details.capacityKb = $true

    $disk.details.diskExtents = $true

    $disk.details.diskType = $true

    $disk.details.thin = $true

    $searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec

    $searchSpec.details = $flags

    $searchSpec.Query += $disk

    $searchSpec.sortFoldersFirst = $true

    $searchSpec.details = $flags

    $searchSpec.matchPattern = "*.vmdk"

    $searchSpec.sortFoldersFirst = $true

  

    $dsBrowser = Get-View $ds.browser

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

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

    foreach ($folder in $searchResult)

    {

        foreach ($fileResult in $folder.File)

        {

            if ($fileResult.Path)

            {

                if (-not ($arrUsedDisks -contains ($folder.FolderPath + $fileResult.Path))){

                    $row = "" | Select DS, Path, File, SizeGB, ModDate, ProvisionedGB

                    $row.DS = $strDatastore.Name

                    $row.Path = $folder.FolderPath

                    $row.File = $fileResult.Path

                    $row.SizeGB = [math]::Round($fileResult.FileSize/1GB,1)

                    $row.ModDate = $fileResult.Modification

                    $row.ProvisionedGB = [math]::Round($fileResult.CapacityKb/1MB,1)

                    $report += $row

                }

            }

        }

    }

}


$report


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

View solution in original post

0 Kudos
piyushranusri
Enthusiast
Enthusiast

LucD

the Provisioned space column is blank.

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership

Works for me

thin.jpg


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

0 Kudos
piyushranusri
Enthusiast
Enthusiast

Yes now for me also,

thanks a lot for looking on this

0 Kudos