Powershell 在一个数组中连接两个或多个调用的结果以获取ChildItem的最干净的方法是什么?

Powershell 在一个数组中连接两个或多个调用的结果以获取ChildItem的最干净的方法是什么?,powershell,powershell-2.0,Powershell,Powershell 2.0,我面临着使用PowerShell在文件系统上移动和复制某些项的问题 我通过实验了解到,即使使用PowerShell v3,cmdlet复制项、移动项和删除项也无法正确处理重分析点,如连接点和符号链接,如果与开关-递归一起使用,可能导致灾难 我想防止这种不平等。每次运行我都要处理两个或多个文件夹,所以我想这样做 $Strings = @{ ... } $ori = Get-ChildItem $OriginPath -Recurse $dri = Get-ChildItem $Destinatio

我面临着使用PowerShell在文件系统上移动和复制某些项的问题

我通过实验了解到,即使使用PowerShell v3,cmdlet
复制项
移动项
删除项
也无法正确处理重分析点,如连接点和符号链接,如果与开关
-递归
一起使用,可能导致灾难

我想防止这种不平等。每次运行我都要处理两个或多个文件夹,所以我想这样做

$Strings = @{ ... }
$ori = Get-ChildItem $OriginPath -Recurse
$dri = Get-ChildItem $DestinationPath -Recurse

$items = ($ori + $dri) | where { $_.Attributes -match 'ReparsePoint' }
if ($items.Length -gt 0)
{
    Write-Verbose ($Strings.LogExistingReparsePoint -f $items.Length)
    $items | foreach { Write-Verbose "    $($_.FullName)" }
    throw ($Strings.ErrorExistingReparsePoint -f $items.Length)
}
这不起作用,因为
$ori
$dri
也可以是单个项目,而不是数组:
操作添加将失败。改为

$items = @(@($ori) + @($dri)) | where { $_.Attributes -match 'ReparsePoint' }
造成另一个问题,因为
$ori
$dri
也可以是
$null
,我可以以包含
$null
的数组结束。当管道将连接resutl连接到
Where Object
时,我可以再次以
$null
、单个项或数组结束

唯一明显有效的解决方案是下面更复杂的代码

$items = $()
if ($ori -ne $null) { $items += @($ori) }
if ($dri -ne $null) { $items += @($dri) }
$items = $items | where { $_.Attributes -match 'ReparsePoint' }

if ($items -ne $null)
{
    Write-Verbose ($Strings.LogExistingReparsePoint -f @($items).Length)
    $items | foreach { Write-Verbose "    $($_.FullName)" }
    throw ($Strings.ErrorExistingReparsePoint -f @($items).Length)
}
有更好的方法吗

我很有兴趣确定是否有一种方法可以用PowerShell cmdlet以正确的方式处理重分析点,但是我更感兴趣的是知道如何加入和筛选两个或更多的“PowerShell集合”

我的结论是,目前,PowerShell的这个特性,即“多态数组”,对我来说似乎没有什么好处


感谢阅读。

只需添加一个过滤器即可抛出空值。你在正确的轨道上


$items=@(@($ori)+@($dri))|?{$\une$null}

我已经在Powershell 3上运行了一段时间,但从我所知道的来看,这在2.0中也应该可以工作:

$items = @($ori, $dri) | %{ $_ } | ? { $_.Attributes -match 'ReparsePoint' }
基本上,
%{$}
是一个foreach循环,它通过迭代内部数组并沿管道传递每个内部元素(
${/code>)来展开内部数组。空值将自动从管道中排除。

文件系统的dir(又名Get ChildItem)具有-Attributes参数<代码>目录-递归-文件-属性!重解析点
。另一个诀窍是,如果希望以列表或数组结束,则始终以空数组开始:
$items=@()+$ori+$dri#数组concat,而不是math
。PS3 MSDN: