- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用PowerShell 4.0,
我试图获取多个目录的大小,并且在Windows告诉我的内容和我的代码告诉我的内容之间却得到非常不一致的结果。
有问题的代码是:
$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
然后我得到
Get-ChildItem : Cannot retrieve the dynamic parameters for the cmdlet. The specified wildcard character pattern is not valid: sample [sample At C:\powershell scripts\test.ps1:55 char:12 + $temp = (Get-ChildItem $dir.fullname -Force -File | Measure-Object -Property l ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException + FullyQualifiedErrorId : GetDynamicParametersException,Microsoft.PowerShell.Commands.GetChildItemCommand
D:\sample
在子级中的某个位置包含一个名为
"[sample"
的文件夹,则同样适用。我将从其他所有内容中获取正确的结果,但是问题目录中或目录之外的任何内容。
$dir.pspath
和
$dir.fullname
都搞砸了。
最佳答案
使用-LiteralPath
参数代替-Path
可以抑制通配符泛滥。另外,由于您使用的是V4,因此可以使用-Directory
开关并省去$_.iscontainer
过滤器:
$folderInfo.directories =
Get-ChildItem -LiteralPath $folderInfo.rootFolder -Recurse -Force -Directory
$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
关于powershell - "["和"]"字符弄乱了get-childitem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21613997/
我是一名优秀的程序员,十分优秀!