首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >"[“和"]”角色搞砸了孩子们的物品

"[“和"]”角色搞砸了孩子们的物品
EN

Stack Overflow用户
提问于 2014-02-06 21:03:35
回答 1查看 5.9K关注 0票数 9

使用PowerShell 4.0,我试图获取多个目录的大小,并且在windows告诉我的内容和代码告诉我的内容之间得到非常不一致的结果。

有关守则是:

代码语言:javascript
复制
$temp4 = ($folderInfo.rootFolder).fullname
$folderInfo.directories += Get-ChildItem -LiteralPath $temp4 -Recurse -Force -Directory
$folderInfo.directories += $folderInfo.rootFolder
foreach ($dir in $folderInfo.directories)
{
    $temp3 = $dir.fullname
    $temp2 = Get-ChildItem -LiteralPath $temp3 -Force
    $temp = (Get-ChildItem -LiteralPath $dir.fullname -Force -File | Measure-Object -Property length -Sum -ErrorAction SilentlyContinue).Sum
    $folderInfo.totalSize += $temp
}
return $folderInfo

如果$folderInfo.rootFolder = D:\sample,我得到了我想要的,但是如果$folderInfo.rootFolder = D:\[sample,我得到了

无法检索cmdlet的动态参数。指定的通配符字符模式无效: sample [示例在C:\powershell scripts\test.ps1:55 char:12 + $temp =(Get-char $dir.fullname -Force -File -Property -Object-Property l.+ CategoryInfo : InvalidArgument:(:) Get-ChildItem,ParameterBindingException + FullyQualifiedErrorId : GetDynamicParametersException,CategoryInfo)

如果D:\sample在其子目录中包含一个文件夹,即"[sample",情况也是如此。我将从所有其他方面得到正确的结果,但问题目录中或问题目录之外的任何内容都是如此。$dir.pspath$dir.fullname都把事情搞砸了。

编辑:更改上述代码以反映其当前状态,并包含完整的错误。

再次编辑:上面的代码现在有一些调试临时变量。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-06 21:22:00

使用-LiteralPath参数代替-Path来抑制通配符全局化。此外,由于您使用的是V4,所以可以使用-Directory开关并免除$_.iscontainer筛选器:

代码语言:javascript
复制
$folderInfo.directories = 
 Get-ChildItem -LiteralPath $folderInfo.rootFolder -Recurse -Force -Directory 

如果目录树下有更多squre括号,请在后续的Get-ChildItem命令中继续使用文字路径:

代码语言:javascript
复制
$folderInfo.directories += Get-ChildItem -LiteralPath $folderInfo.rootFolder -Recurse -Force -Directory
    $folderInfo.directories += Get-Item -LiteralPath $folderInfo.rootFolder
    foreach ($dir in $folderInfo.directories)
    {
        $temp2 = Get-ChildItem -LiteralPath $dir.PSPath -Force
        $temp = (Get-ChildItem -LiteralPath $dir.fullname -Force -File | Measure-Object -Property length -Sum -ErrorAction SilentlyContinue).Sum
        $folderInfo.totalSize += $temp
    }
    return $folderInfo
票数 18
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21613997

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档