Powershell获取完整路径信息
问题描述:
我有一个名为Videos的目录。在这个目录里面,有各种相机的一些子目录。我有一个脚本可以检查各个相机,并删除比特定日期更早的记录。Powershell获取完整路径信息
我在获取相机的完整目录信息时遇到了一些问题。我现在用的是以下获得它:
#Get all of the paths for each camera
$paths = Get-ChildItem -Path "C:\Videos\" | Select-Object FullName
然后,我通过在$路径中的每个路径循环,并删除任何我需要:
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
将刚刚返回的而不是Select-Object
被返回对象的字符串。
答
你快到了。你想要的是Select-Object的-ExpandProperty参数。这将返回该属性的值,而不是具有一个属性的FileInfo对象,该属性为FullName。这应该解决它为您:
$paths = Get-ChildItem -Path "C:\Videos\" | Select-Object -ExpandProperty FullName
编辑:看起来像马特通过一分钟打我给它。