VMware Cloud Community
MorDrakka
Contributor
Contributor
Jump to solution

Error listing files on datastore

Ok,

New error, new topic, as was suggested to me.

I am trying to list all files on a datastore with the following powershell code(After connect-VIserver offcourse):

$ds = Get-Datastore -Name DATA-SERVERNAME | %{Get-View $_.Id}

$fileQueryFlags = New-Object VMware.Vim.FileQueryFlags

$fileQueryFlags.FileSize = $true

$fileQueryFlags.FileType = $true

$fileQueryFlags.Modification = $true

$searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec

$searchSpec.details = $fileQueryFlags

$searchSpec.sortFoldersFirst = $true

$dsBrowser = Get-View $ds.browser

$rootPath = "" -f ($ds.summary.Name)

$searchResult = $dsBrowser.SearchDatastoreSubFolders($rootPath, $searchSpec)

foreach ($folder in $searchResult) {

foreach ($fileResult in $folder.File) {

$file = "" | select Name, Size, Modified, FullPath

$file.Name = $fileResult.Path

$file.Size = $fileResult.Filesize

$file.Modified = $fileResult.Modification

$file.FullPath = $folder.FolderPath + $file.Name

$file | out-default

}

}

The error I receive is this:

Exception calling "SearchDatastoreSubFolders" with "2" argument(s): "Invalid datastore path 'DATA-SERVERNAME'."

At line 30, position 53

$searchResult = $dsBrowser.SearchDatastoreSubFolders($rootPath, $searchSpec)

Anyone idea's?

0 Kudos
1 Solution

Accepted Solutions
hugopeeters
Hot Shot
Hot Shot
Jump to solution

Small update to the script for nicer output:

See attachment

View solution in original post

0 Kudos
5 Replies
hugopeeters
Hot Shot
Hot Shot
Jump to solution

The method SearchDatastoreSubFolders would like square brackets around the root folder.

So, change the following line:

$rootPath = "{0}" -f ($ds.summary.Name)

into:

SEE ATTACHMENT

Message was edited by: hugopeeters

Forum messed up my code Smiley Sad

hugopeeters
Hot Shot
Hot Shot
Jump to solution

Small update to the script for nicer output:

See attachment

0 Kudos
MorDrakka
Contributor
Contributor
Jump to solution

Thanks Hugo!

0 Kudos
VirtualInStanit
Contributor
Contributor
Jump to solution

Thanks Hugo - great insight. I know this thread is a little rusty so maybe my problem is due to a change or one of the upgrades we've done on our path to vSphere.

We have a couple datastores on shared storage, and those datastores are accessible to hosts in different datacenters. So in ViClient Datastore view I see multiple instances of my shared storage (once in each datacenter).

I think that's the root of my problem getting the script to run cleanly. When it hits one of these cross-datacenter datastores, this command...

$ds = Get-Datastore -Name $datastorename | %{Get-View $_.Id}


Returns this:
Datastore-datastore-14271
Datastore-datastore-32

So when this command runs...
$dsBrowser = Get-View $ds.browser

,,,it returns this:
Cannot validate argument on parameter 'VIObject'. The argument cannot be null or empty.
$dsBrowser = Get-View <<<<  $ds.browser


On datastores that only return one ID, it works like a champ. But from what I could debug and test if Get-Datastore returns more than one value for $_.ID then Get-View will fail.

Trying to resolve this myself but I'm still pretty green with powershell and PowerCLI. Looking for a way to have Get-View iterate through multiple $_.ID vaules when it encounters them, and then use the ID instead of the $ds.browser property to call Get-View. I have several other datastore reports I'm trying to build via PowerCLI and this cross-datacenter datastore stuff has been a thorn in my side. Any advice ([ther than "don't use storage across datacenters :-)] is appreciated.

-Thanks

0 Kudos
avlieshout
VMware Employee
VMware Employee
Jump to solution

Always use a foreach loop.

Foreach($ds in get-datastore | get-view) {

$dsbrowser=$ds.browser

}

Sent from my iPad

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
0 Kudos