Skagnola
Enthusiast
Enthusiast

Search for ISOs across all datastores, excluding those 'Inactive'

Jump to solution

Hello!

I am running into an issue when searching for ISOs. When a datastore is marked as 'Inactive', instead of the script continuing, it is halting on the inaccessible datastore.

dir vmstores:\ -Recurse -include *.iso | Select Name,FolderPath

dir : Datastore 'vmrepo1-rep1' is not accessible. No connected and accessible host is attached to this datastore.

At C:\locateisos.ps1:2 char:1

+ dir vmstores:\ -Recurse -include *.iso | Select Name,FolderPath

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Get-ChildItem], InaccessibleDatastore

    + FullyQualifiedErrorId : VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InaccessibleDatastore,Microsoft.PowerShell.Commands.GetChildItemCommand

Is there a way to pass an exclude arg if the datastore is marked 'Inactive'?

1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Looks like the PSProvider vimstores doesn't handle errors in an optimal way.

That means you should get the desired result, scanning for ISO files, in another way.Try like this

Get-Datastore | where{$_.State -eq 'Available'} | %{

    New-PSDrive -Location $_ -Name ds -PSProvider VimDatastore -Root '\' > $null

    Get-ChildItem -Path ds: -Recurse -Filter *.iso | Select DatastoreFullPath

    Remove-PSDrive -Name ds -Confirm:$false

}


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

View solution in original post

10 Replies
LucD
Leadership
Leadership

Did you already try adding the parameter -ErrorAction SilentlyContinue to the dir command?


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

0 Kudos
Skagnola
Enthusiast
Enthusiast

Hey, Luc

Did not; have just tried the following

dir vmstores:\ -Recurse -ErrorAction SilentlyContinue -include *.iso  | Select Name,FolderPath

but it is still failing.

Edited the order of the dir command

0 Kudos
LucD
Leadership
Leadership

What do you get back when you do the following on one of these inaccessible datastores?

Get-Datastore -Name <inaccessible-datastore-name> | Select *


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

0 Kudos
Skagnola
Enthusiast
Enthusiast

You read my mind! Was just about to reply with

When using Get-Datastore 'datastorename' | Select *, there is a field 'State' that shows 'Unavailable', presumably what shows up as 'Inactive' in the vSphere GUI.

I wonder if there is a way to pipe that in the search.

$Datastores = Get-Datastore | Select Name,State

if ($Datastores.State -like "Available")

Not sure what to do with it, though.

0 Kudos
LucD
Leadership
Leadership

Then try like this.

We add the name of the faulty datastore on the Exclude parameter

$badDS = Get-Datastore | where{$_.State -ne 'Available'} | %{"$($_.Name)"}

Get-ChildItem -Path vmstores: -Include *.iso -Exclude $badDS -Recurse


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

0 Kudos
Skagnola
Enthusiast
Enthusiast

Hmm still no joy.

$badDS = Get-Datastore | where{$_.State -eq 'Unavailable'} | %{"$($_.Name)"}

Get-ChildItem -Path vmstores: -Include *.iso -Exclude $badDS -Recurse

Get-ChildItem : Datastore 'vmrepo1-rep1' is not accessible. No connected and accessible host is attached to this datastore.

At C:\locateisos2.ps1:3 char:1

+ Get-ChildItem -Path vmstores: -Include *.iso -Exclude $badDS -Recurse

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Get-ChildItem], InaccessibleDatastore

    + FullyQualifiedErrorId : VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InaccessibleDatastore,Microsoft.PowerShell.Commands.GetChildItemCommand

0 Kudos
LucD
Leadership
Leadership

Looks like the PSProvider vimstores doesn't handle errors in an optimal way.

That means you should get the desired result, scanning for ISO files, in another way.Try like this

Get-Datastore | where{$_.State -eq 'Available'} | %{

    New-PSDrive -Location $_ -Name ds -PSProvider VimDatastore -Root '\' > $null

    Get-ChildItem -Path ds: -Recurse -Filter *.iso | Select DatastoreFullPath

    Remove-PSDrive -Name ds -Confirm:$false

}


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

Skagnola
Enthusiast
Enthusiast

Wow! Didn't consider PSDrive!

That works.Thanks, Luc!

0 Kudos
AlbertWT
Virtuoso
Virtuoso

Does the command Remove-PSDrive -Name ds -Confirm:$false is considered safe to be executed on the Datastore that is not put into maintenance mode ?

/* Any kind of comment or input would be greatly appreciated */
0 Kudos
LucD
Leadership
Leadership

Afaik yes, you, or your script, would just be another user of the datastore.

Regular locking mechanisms apply.


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

0 Kudos