Powershell正在获取完整路径信息

Powershell正在获取完整路径信息,powershell,Powershell,我有一个叫视频的目录。在这个目录中,有许多不同摄像机的子目录。我有一个脚本,可以检查每个摄像头,并删除超过某个日期的录制 我在获取摄像机的完整目录信息时遇到了一些问题。我使用以下方法来获得它: #Get all of the paths for each camera $paths = Get-ChildItem -Path "C:\Videos\" | Select-Object FullName 然后我在$path中循环遍历每个路径并删除我需要的任何内容: foreach ($pa in

我有一个叫视频的目录。在这个目录中,有许多不同摄像机的子目录。我有一个脚本,可以检查每个摄像头,并删除超过某个日期的录制

我在获取摄像机的完整目录信息时遇到了一些问题。我使用以下方法来获得它:

#Get all of the paths for each camera
$paths = Get-ChildItem -Path "C:\Videos\" | Select-Object FullName
然后我在$path中循环遍历每个路径并删除我需要的任何内容:

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=从路径中剥离?我想这可能就是问题所在。

在您的例子中,
$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
将只返回字符串,而不是
选择对象
返回的对象。

您就快到了。您需要的是选择对象的-ExpandProperty参数。这将返回该属性的值,而不是只有一个属性的FileInfo对象,该属性为FullName。This应为您解决此问题:

$paths = Get-ChildItem -Path "C:\Videos\" | Select-Object -ExpandProperty FullName
编辑:看起来马特比我快了一分钟