4

There is a back-end SQL DB contains "managed folders" in the form of UNC paths. Using SQL queries in PowerShell I have a loop that will work it's way through these folders and run a GCI operation against them to work out how much disk space they are using.

$managedFolder = "\\server\share\folder\subfolder"

For the sake of the question, $managedFolder is declared as above. The failing command below:

$diskTrendsInitialUsage = "{0:N2}" -f ((Get-ChildItem $managedFolder -Recurse -Force | Measure-Object -Property Length -Sum).Sum / 1GB)

Now if I were to run this command manually in PS console it's fine, it pulls back data. But as soon as it's packaged in a script, it fails with the below error. The folder is accessible from the server, as it works fine from a local PS console session.

ERROR: Get-ChildItem : Invalid Path: '\\server\share\folder\subfolder'.
AddManagedFolder.psf (17): ERROR: At Line: 17 char: 42
ERROR: +         $diskTrendsInitialUsage = "{0:N2}" -f ((Get-ChildItem $managedFolder -Recurse  ...
ERROR: +                                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR:     + CategoryInfo          : NotSpecified: (:) [Get-ChildItem], ArgumentException
ERROR:     + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.GetChildItemCommand
ERROR:

I'm stumped.

PnP
  • 3,133
  • 17
  • 62
  • 95

2 Answers2

7

The problem with your path is that it does not have any indication about which provider to use, so PowerShell just use current one. And if current provider is not a file system provider, then it will fail. So you need to specify provider in path, to allow PowerShell to choose right one regardless of current provider:

$managedFolder = "filesystem::\\server\share\folder\subfolder"
user4003407
  • 21,204
  • 4
  • 50
  • 60
2

My guess is your are using the SQL PS cmdlets prior to running GCI, this is changing your provider path to SQL: which is what is causing GCI to be unhappy.

Prior to running GCI do cd c:\ to change the path back to the file system and GCI will work.

Noah Sparks
  • 1,710
  • 13
  • 14
  • Thanks Noah, I decided to with the above option as thats keeps the code cleaner, but many thanks for your answer. – PnP Dec 31 '14 at 19:03
  • You're welcome. He used my information to post his answer, but specifying it that way is definitely cleaner. – Noah Sparks Dec 31 '14 at 23:02