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?
Thanks Hugo!
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
Always use a foreach loop.
Foreach($ds in get-datastore | get-view) {
$dsbrowser=$ds.browser
}
Sent from my iPad