我是PowerShell的新手。我想写一个简单的程序来列出所有的*.bak文件,然后我可以按日期或大小排序,如下所示。
$Drives = Get-WMIObject -class win32_logicaldisk -filter "DriveType = 3" ;
foreach ($d in $Drives){
If (($d.deviceId -ne "C:") -and ($d.VolumeName -ne "PAGEFILE")) {
$backups += Get-ChildItem -Path $d.deviceID -Recurse -filter *.bak
}这通常工作得很好,除非例如D:驱动器只有一个*.bak文件。在这种情况下,我会得到一个错误。
Method invocation failed because [System.IO.FileInfo] doesn't contain a method named 'op_Addition'.
At F:\work\PowerShell\DiskSpace\generate-disk-report-v2.ps1:39 char:13
+ $backups += <<<< Get-ChildItem -Path $d.deviceID -Recurse -filter *.bak
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound如果我向该驱动器添加一个额外junk.bak,它可以正常工作。
发布于 2013-10-11 02:29:11
在我的例子中,我发现需要将变量初始化为数组,而Get-ChildItem需要作为数组返回,特别是如果它只返回一个文件。
在您的案例中:
$backups = @() - (Before calling Get-ChildItem)和
$backups = @(Get-ChildItem -Path $d.deviceID -Recurse -filter *.bak) - (Cast as an array)https://stackoverflow.com/questions/17436071
复制相似问题