Quantcast
Channel: VMware Communities : Blog List - All Communities
Viewing all articles
Browse latest Browse all 3135

How to detect invalid VMs folder or Unregisterd Folder which are present in the datastore for make free space.

$
0
0

I have written a powershell script which will detect the invalid folder or unregistered folder which are still in the datastore.

 

 

Below function has to run before running the main script.

 

function Get-SetOperationResult

{

    [CmdletBinding()]

    param

    (

        [Parameter(Mandatory=$true,

                   Position=0)]

        [object[]]

        $Left,

 

        [Parameter(Mandatory=$true,

                   Position=1)]

        [object[]]

        $Right,

 

        [Parameter(Mandatory=$false,

                   Position=2)]

        [ValidateSet("Union","Intersection","Difference-LeftMinusRight","Difference-RightMinusLeft","ComplementLeft","ComplementRight")]

        [string]

        $OperationType="Intersection"

    )

    

    

    BEGIN

    {       

    }

    

    PROCESS

    {

        

        [object] $result = @()

 

        #-----------

        #Union = Given two sets, the distinct set of values from both

        #-----------

        if ($OperationType -eq 'Union')

        {

            $result = Compare-Object $Left $Right -PassThru -IncludeEqual                   # union

        }

 

        #-----------

        #Intersection = Given two sets, the distinct set of values that are only in both

        #-----------

        if ($OperationType -eq 'Intersection')

        {

            $result = Compare-Object $Left $Right -PassThru -IncludeEqual -ExcludeDifferent # intersection

        }

 

        #-----------

        #Difference = Given two sets, the values in one (minus) the values in the other

        #-----------

        if ($OperationType -eq 'Difference-LeftMinusRight')

        {

            $result = $Left | ?{-not ($Right -contains $_)}

        }

        if ($OperationType -eq 'Difference-RightMinusLeft')

        {

            $result = $Right | ?{-not ($Left -contains $_)}

        }

        

        #-----------

        #Complement = Given two sets, everything in the universe which is the UNION (minus) the values in the set being "Complemented"

        #-----------

        if ($OperationType -eq 'ComplementLeft')

        {

            $result = Compare-Object $Left $Right -PassThru -IncludeEqual |                  # union

                                ?{-not ($Left -contains $_)}

        }

        if ($OperationType -eq 'ComplementRight')

        {

            $result = Compare-Object $Left $Right -PassThru -IncludeEqual |                  # union

                                ?{-not ($Right -contains $_)}

        }

        

        Write-Output $result

    }

    

    END

    {

    }

}

 

 

Main program starts from here:

 

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -confirm:$false

Connect-VIServer -Server $ip -Protocol https -Username $username -Password $pwd

$x = " this is the program of which snapshot of which vmdk"

$y = "_" * $x.Length

$c =@{}

$files=@{}

$files = New-Object System.Collections.ArrayList

$c = New-Object System.Collections.ArrayList

$e = New-Object System.Collections.ArrayList

$vmdks = New-Object System.Collections.ArrayList

$vms = New-Object System.Collections.ArrayList

 

 

$datastore_1 = Get-Datastore | select-object DatastoreBrowserPath, Name

 

for( $j=0 ; $j -lt $datastore_1.Length ;$j++){

 

$template_1 = Get-Template | Select-Object Name

$template = Get-HardDisk -Template $template_1.name | Select-Object Filename

 

if((Get-Datastore -Name $datastore_1[$j].Name | Get-VM | Select-Object name) -ne $null){

 

 

    $datastore_1[$j].datastorebrowserpath

    $files_1 = dir $datastore_1[$j].datastorebrowserpath | Select-Object name

    $files = $files_1.name -like "[a-z]*"

 

 

    $vms = Get-Datastore -Name $datastore_1[$j].Name | Get-VM | Select-Object name

    $vmdk_vm = Get-HardDisk $vms.name | Select-Object Filename

 

    $vmdks = $template + $vmdk_vm

    $a = Split-Path $vmdks.filename | Select-Object -Unique

 

    for ($i=0 ; $i -lt $a.count ;$i++){

       

        $b = $a[$i]

        $n = $b.Split('{]}',2)

        $d = $n[1].Split('{ }',2)

                        #$d = -join $b[14..$b.Length]

        $c.add($d[1])

        }

        $e = $c | Select-Object -Unique

        $c.clear()

 

        if((Get-SetOperationResult -Left $files -Right $e -OperationType Difference-LeftMinusRight ) -eq $null)

        {  

 

            Write-Host no Invalid folder are there in datastore

           

            $e.Clear()

        }

        else

        {   

            Get-SetOperationResult -Left $files -Right $e -OperationType Difference-LeftMinusRight

           

        }

    }

    else

    {

        $datastore_1[$j].datastorebrowserpath

        $files_1 = dir $datastore_1[$j].datastorebrowserpath | Select-Object name

        $files = $files_1.name -like "[a-z]*"

        $files

    }

$template.clear()  

}

Disconnect-VIServer -Server $ip -confirm:$false

 

 

Result :

 

vmstores:\$IP@443\Server Datacenter\Bay10_datastore2

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

no Invalid folder are there in datastore

vmstores:\$IT@443\Server Datacenter\Bay10_Datastore1

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

temp

 

 

#This above temp file/folder has detected in Bay10_Datastore1.

#And 24 number's are number of VM's  in Bay10_Datastore1 and Template's in vcenter.


Viewing all articles
Browse latest Browse all 3135

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>