VMware Cloud Community
jvm2016
Hot Shot
Hot Shot
Jump to solution

finding .iso files in all datastores in vcenter_powercli

Hi Luc ,

if you can check the following

i am looking to check each datasore for .iso file .i dont think there is direct way of doing this .can you please check the folowing script if this works fine ??

$datastoresFR=get-datastore

$count=$datastoresFR.count

foreach($d in $datastoresFR)

{

$ds=get-datastore -name $d

for ($i=0; $i -le $count ; $i++)

{

New-PSDrive -Location $ds -Name DS[$i] -PSProvider VimDatastore -Root '\' | Out-Null

Set-Location DS[$i]:\

$isofile=Get-ChildItem *.iso

if($isofile.count -ge "1")

{

write-host "there are/is .iso files stored in " $ds.name

$isofile

Remove-PSDrive -Name DS[$i] -Confirm:$false

}

}

}

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I don't use the PSProvider anymore, since it is way too slow.

I use something like this

Get-Datastore -PipelineVariable ds |

ForEach-Object -Process {

   $browser = Get-View -Id $ds.ExtensionData.Browser


   $spec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec

   $spec.MatchPattern = '*.iso'

   $spec.Details = New-Object VMware.Vim.FileQueryFlags

   $spec.Details.FileOwner = $true

   $spec.Details.FileSize = $true

   $spec.Details.FileType = $true

   $spec.Query += New-Object VMware.Vim.IsoImageFileQuery

   $result = $browser.SearchDatastoreSubFolders("[$($ds.Name)]",$spec)

   if($result.File){

       $result.File |

       ForEach-Object -Process {

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

                Datastore = $ds.Name

                Path = $result.FolderPath

                File = $_.Path

                Size = $_.FileSize

            })

       }

    }

}


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

View solution in original post

Reply
0 Kudos
10 Replies
LucD
Leadership
Leadership
Jump to solution

There is.
The HostDatastoreBrowser has a SearchDatastoreSubFolders_Task method.


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

i am checking this if yu coud suggest the one which i posted will work or not .??

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I don't use the PSProvider anymore, since it is way too slow.

I use something like this

Get-Datastore -PipelineVariable ds |

ForEach-Object -Process {

   $browser = Get-View -Id $ds.ExtensionData.Browser


   $spec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec

   $spec.MatchPattern = '*.iso'

   $spec.Details = New-Object VMware.Vim.FileQueryFlags

   $spec.Details.FileOwner = $true

   $spec.Details.FileSize = $true

   $spec.Details.FileType = $true

   $spec.Query += New-Object VMware.Vim.IsoImageFileQuery

   $result = $browser.SearchDatastoreSubFolders("[$($ds.Name)]",$spec)

   if($result.File){

       $result.File |

       ForEach-Object -Process {

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

                Datastore = $ds.Name

                Path = $result.FolderPath

                File = $_.Path

                Size = $_.FileSize

            })

       }

    }

}


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

thanks luc .

i remember it was indeed very slow as i can recall one the scripts .

however in order to get proper output of following

Datastore = $ds.Name

                Path = $result.FolderPath

                File = $_.Path

                Size = $_.FileSize

do we need to use pscustom object instead od new-object??

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can store the information in whatever format you like.
This is just 1 example of what I often use.


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

thanks luc

Reply
0 Kudos
VB_Windstream
Contributor
Contributor
Jump to solution

I know this is an older post but when I try running it I am getting the following error.

 

Exception calling "SearchDatastoreSubFolders" with "2" argument(s): "Datastore '********' is not accessible. No connected and accessible host is attached to this datastore.

 

Is there a way to make it ignore this and keep going?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I think the error is clear, there is no ESXi node that has access to this datastore.
Check which 

Get-VMHost -Datastore <ds-name>


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

Reply
0 Kudos
VB_Windstream
Contributor
Contributor
Jump to solution

PS C:\Windows\system32> Get-VMHost -Datastore **********
WARNING: PowerCLI scripts should not use the 'DatastoreIdList' property of VMHost type. The property will be removed in a future release.

Name ConnectionState PowerState NumCpu CpuUsageMhz CpuTotalMhz MemoryUsageGB MemoryTotalGB Version
---- --------------- ---------- ------ ----------- ----------- ------------- ------------- -------
******** Connected PoweredOn 32 12615 82976 110.537 383.734 6.5.0

 

Looks like it finds the Host OK, I also confirmed this from the UI. Anything else you can think of might cause this? Is there a way to make it just ignore these errors and keep scanning?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Not that I know of I'm afraid


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

Reply
0 Kudos