VMware Cloud Community
powercliauto
Contributor
Contributor
Jump to solution

Group the Files in multiple folders and write size and folder name in output

Is it possible to get these information through powercli?

I have a datastore which contains multiple file Like .iso, .zip,.exe,.vib etc. These files are distributed in multiple folders.

As I know the extensions I will give them as input

Ex:

$Extension1= '.iso',.exe

$Extension2=.zip,.vib

Here I am expecting output - similar files in $Extension1 should group and write output as below

Path

Object count= like .iso count or .zip files count

Extension = .zip or .vib

Size= how much size in MB

and folder name like $Extension1 belongs to image folder name

$Extension2 belongs to Executable folder name (These folder names are different in the datastore but while writing in output it should write with this name.)

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this

$dsName = 'MyDS'

$Extension1 = '.iso','.exe'

$Extension2 = '.zip','.vib'

$ext = $Extension1 + $Extension2 | %{"*$_"}

$ds = Get-Datastore -Name $dsName

Get-ChildItem -Path "$($ds.DatastoreBrowserPath)\*" -Recurse -Include $ext |

Group-Object -Property FolderPath -PipelineVariable folder |

ForEach-Object -Process {

    $folder.Group |

    Group-Object -Property {$_.Name.Split('.')[1]} -PipelineVariable type |

    ForEach-Object -Process {

        New-Object psobject -Property ([ordered]@{

            Variable = Get-Variable -Name Extension? | where {$_.Value -contains ".$($type.Name)"} | select -ExpandProperty Name

            Folder = $folder.Name

            Extension = $type.Name

            Count = $type.Group.Count

            SizeMB = [math]::Round(($type.Group | Measure-Object -Property Length -Sum | select -ExpandProperty Sum)/1MB,0)

        })

    }

}


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

View solution in original post

4 Replies
LucD
Leadership
Leadership
Jump to solution

Not sure what you want to see under Path, and what you mean with "folder name like $Extension1 belongs to image folder name".

Could you perhaps give some example output?


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

Reply
0 Kudos
powercliauto
Contributor
Contributor
Jump to solution

For Example

In a datastore there are multiple folders and in that folders .iso files, .zip files and .vib files etc.. are present so I need to do two filters with extensions with below variables

$filter1 = .vib,.exe

$filter2=.zip,.vib

pastedImage_0.png

Now I need to Get-Datastore 'datastorename' | dir $_.DatastoreBrowserPath -Recurse | where {$filter1 -contains ..

After fetching the extensions in $filter1 variable the group the similar extensions (ex: if .iso files are around 10 then group them and give the count and also calculate(sum) the Size of all the .iso files).

The output should be somewhat like this.

from which variable the extension is    : $filter1 or $filter2 

ObjectCount : 15 (15 iso files are available)

Extension   : .iso

Size    : 3318628 (Sum of all the iso file size)
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try something like this

$dsName = 'MyDS'

$Extension1 = '.iso','.exe'

$Extension2 = '.zip','.vib'

$ext = $Extension1 + $Extension2 | %{"*$_"}

$ds = Get-Datastore -Name $dsName

Get-ChildItem -Path "$($ds.DatastoreBrowserPath)\*" -Recurse -Include $ext |

Group-Object -Property FolderPath -PipelineVariable folder |

ForEach-Object -Process {

    $folder.Group |

    Group-Object -Property {$_.Name.Split('.')[1]} -PipelineVariable type |

    ForEach-Object -Process {

        New-Object psobject -Property ([ordered]@{

            Variable = Get-Variable -Name Extension? | where {$_.Value -contains ".$($type.Name)"} | select -ExpandProperty Name

            Folder = $folder.Name

            Extension = $type.Name

            Count = $type.Group.Count

            SizeMB = [math]::Round(($type.Group | Measure-Object -Property Length -Sum | select -ExpandProperty Sum)/1MB,0)

        })

    }

}


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

powercliauto
Contributor
Contributor
Jump to solution

Thank you LucD. Its giving results as expected. If anything additionally required i will get back to you

Reply
0 Kudos