PowerShell获取完整路径信息

3
我有一个名为Videos的目录。在这个目录中,有许多不同相机的子目录。我有一个脚本,将检查每个相机,并删除早于某个日期的录像。
我在获取相机的完整目录信息方面遇到了一些问题。我使用以下代码来获取它:
#Get all of the paths for each camera
$paths = Get-ChildItem -Path "C:\Videos\" | Select-Object FullName

然后我循环遍历$paths中的每个路径并删除我需要的内容:

foreach ($pa in $paths) {
    # Delete files older than the $limit.
    $file = Get-ChildItem -Path $pa -Recurse -Force | Where-Object { $_.PSIsContainer -and $_.CreationTime -lt $limit } 
    $file | Remove-Item -Recurse -Force
    $file | Select -Expand FullName | Out-File $logFile -append
}

当我运行脚本时,出现如下错误:

@{FullName=C:\Videos\PC1-CAM1}
Get-ChildItem : Cannot find drive. A drive with the name '@{FullName=C' does not exist.
At C:\scripts\BodyCamDelete.ps1:34 char:13
+     $file = Get-ChildItem -Path $pa -Recurse -Force | Where-Object { $_.PSIsCont ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (@{FullName=C:String) [Get-ChildItem], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

有没有办法将@{FullName=}从路径中去掉?我认为这可能是问题所在。
2个回答

6
在你的情况下,$pa 是一个带有 FullName 属性的对象。访问该属性的方式如下。
$file = Get-ChildItem -Path $pa.FullName -Recurse -Force | Where-Object { $_.PSIsContainer -and $_.CreationTime -lt $limit } 

然而,只更改此行并保留其他内容可能会更简单。

$paths = Get-ChildItem -Path "C:\Videos\" | Select-Object -ExpandProperty FullName

-ExpandProperty 只会返回字符串,而不是 Select-Object 返回的对象。


3

您已经非常接近成功了。您需要使用Select-Object的-ExpandProperty参数。这将返回该属性的值,而不是仅返回一个FileInfo对象且该对象只包含一个FullName属性。以下代码应该能解决您的问题:

$paths = Get-ChildItem -Path "C:\Videos\" | Select-Object -ExpandProperty FullName

编辑:看起来 Matt 比我早了一分钟。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接