PowerCLI

 View Only
Expand all | Collapse all

orphan disk with provisoned space

  • 1.  orphan disk with provisoned space

    Posted Mar 19, 2020 12:05 PM

    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"

    }



  • 2.  RE: orphan disk with provisoned space

    Posted Mar 19, 2020 12:35 PM

    Wrong forum, move to PowerCLI.



  • 3.  RE: orphan disk with provisoned space

    Posted Mar 19, 2020 12:41 PM

    Moderator: Moved to PowerCLI



  • 4.  RE: orphan disk with provisoned space

    Posted Mar 19, 2020 12:47 PM

    Thanks scott28tt



  • 5.  RE: orphan disk with provisoned space
    Best Answer

    Posted Mar 19, 2020 02:19 PM

    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



  • 6.  RE: orphan disk with provisoned space

    Posted Mar 20, 2020 10:14 AM

    LucD

    the Provisioned space column is blank.



  • 7.  RE: orphan disk with provisoned space

    Posted Mar 20, 2020 10:27 AM

    Works for me



  • 8.  RE: orphan disk with provisoned space

    Posted Mar 20, 2020 10:51 AM

    Yes now for me also,

    thanks a lot for looking on this