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'?
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
Did you already try adding the parameter -ErrorAction SilentlyContinue to the dir command?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
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
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.
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
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
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
Wow! Didn't consider PSDrive!
That works.Thanks, Luc!
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 ?
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