PowerShell 中的 Get-ChildItem 操作不当 [英] Misbehaving Get-ChildItem Operation in PowerShell

查看:56
本文介绍了PowerShell 中的 Get-ChildItem 操作不当的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个包含 UNC 路径形式的托管文件夹"的后端 SQL DB.在 PowerShell 中使用 SQL 查询我有一个循环,它将通过这些文件夹运行,并对它们运行 GCI 操作以计算它们使用了多少磁盘空间.

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"

为了这个问题,$managedFolder 如上声明.下面的失败命令:

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)

现在,如果我要在 PS 控制台中手动运行此命令,那就没问题了,它会拉回数据.但是,一旦将其打包到脚本中,就会失败并显示以下错误.该文件夹可从服务器访问,因为它在本地 PS 控制台会话中工作正常.

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:

我被难住了.

推荐答案

您的路径的问题在于它没有任何关于使用哪个提供程序的指示,因此 PowerShell 只使用当前的提供程序.如果当前提供者不是文件系统提供者,那么它将失败.所以你需要在路径中指定提供者,以允许 PowerShell 选择正确的一个而不管当前提供者:

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"

这篇关于PowerShell 中的 Get-ChildItem 操作不当的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆