2014-12-04 92 views
0

我試圖弄清楚如何保持變量$Error不受System.IO.PathTooLongException例外的影響,但仍然在ArrayList中收集這些長路徑供以後使用。

實施例:

$LongPath1 = "\\domain.net\bnl\Other stuff\DEPARTMENTS\SITE\Administration\Plant\Securite\Bien etre\Bien etre\Bien-être au travail\Travaux de fin d'année\Other-Stuff" 
$LongPath2 = "\\domain.net\bnl\Other stuff\DEPARTMENTS\SITE\Administration\Plant\Laboratoire\Sauvegarde PC\Bibliothèques\Documents\Sauvegarde dossier Labo\Laboratoire (sauvegarde 03-2014)\Laboratoire\Travaux en cours" 

Get-ChildItem -Recurse -LiteralPath $LongPath1 -ErrorVariable e -ErrorAction SilentlyContinue 
Get-ChildItem -Recurse -LiteralPath $LongPath2 -ErrorVariable +e -ErrorAction SilentlyContinue 

$PathTooLong = New-Object System.Collections.ArrayList($null) 

$e | where {$_.Exception -like 'System.IO.PathTooLongException*'} | ForEach { 
    $PathTooLong.Add($_.TargetObject) | Out-Null 
} 

$PathTooLong 

它是如何能夠刪除存儲在ErrorVariable的「e」的長路徑從自動生成$Error變量完全相同的錯誤?

嘗試:

Get-ChildItem -Recurse -LiteralPath $LongPath1 -ErrorVariable e -ErrorAction Ignore 

我認爲將是天才之處在於使用的選項Ignore所以$Error變量不被自動填寫,但隨後ErrorVariable「E」保持空.. 。

謝謝你的幫助。

回答

0

顯然它比我想象的要簡單得多。下面是我找到了答案:

$LongPath1 = "\\domain.net\bnl\Other stuff\DEPARTMENTS\SITE\Administration\Plant\Securite\Bien etre\Bien etre\Bien-être au travail\Travaux de fin d'année\Other-Stuff" 
$LongPath2 = "\\domain.net\bnl\Other stuff\DEPARTMENTS\SITE\Administration\Plant\Laboratoire\Sauvegarde PC\Bibliothèques\Documents\Sauvegarde dossier Labo\Laboratoire (sauvegarde 03-2014)\Laboratoire\Travaux en cours" 

Get-ChildItem -Recurse -LiteralPath $LongPath1 -ErrorVariable e -ErrorAction SilentlyContinue 
Get-ChildItem -Recurse -LiteralPath $LongPath2 -ErrorVariable +e -ErrorAction SilentlyContinue 

$PathTooLong = New-Object System.Collections.ArrayList($null) 

$e | where {$_.Exception -like 'System.IO.PathTooLongException*'} | ForEach { 
    $PathTooLong.Add($_.TargetObject) | Out-Null 
    $Error.Remove($_) 
} 

$PathTooLong 

我可以很容易地刪除像$Error.Remove($_)錯誤。喜歡PowerShell! :)