2017-05-11 34 views
2

私は現在のディレクトリの下にあるlog4net.dllのすべてのバージョンを再帰的に検索して一覧表示するためにPowerShell 5.0を使用しています。Get-ChildItemを使用しているときにUnathorizedAccessExceptionを回避する方法

Get-ChildItem log4net.dll -Recurse | % versioninfo | Export-Csv "C:\MyJunk\log4net.csv" 

上記の文は、予想通り、バージョン情報を返す開始しますが、実行は、私がアクセス権限を持たない最初のフォルダで停止:

Get-ChildItem : The specified network name is no longer available. 
At line:1 char:1 
+ Get-ChildItem log4net.dll -Recurse | % versioninfo | Export-Csv "C:\M ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
+ CategoryInfo   : ReadError: (J:\ArcPlan_OracleWallet\Production:String) [Get-ChildItem], IOException 
+ FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand 

Get-ChildItem : Access is denied 
At line:1 char:1 
+ Get-ChildItem log4net.dll -Recurse | % versioninfo | Export-Csv "C:\M ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
+ CategoryInfo   : NotSpecified: (:) [Get-ChildItem], UnauthorizedAccessException 
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetChildItemCommand 

私はAdminstratorとしてのWindows PowerShell ISEを実行しています。 ExecutionPolicyはRemoteSignedであり、$ ErrorActionPreferenceはContinueです。

理想的には、スクリプトに各フォルダのACLを問い合わせてアクセス権のないすべてのフォルダ(およびその内容)をバイパスしたいと思います。しかし、別のソリューションでは、ハードコードされたフォルダがバイパスされます。 PowerShellの初心者であり、私は後で焦点を当てました。

最初の問題フォルダ(名前)をバイパスして、その作業を取得できるかどうかを確認しましたが、同じ例外が発生し、処理が停止します。

Get-ChildItem log4net.dll -Recurse | Where-Object { $_.FullName -notmatch '\\ArcPlan_OracleWallet\\?'} | export-csv 'C:\MyJunk\log4net.csv' 

ありがとうございます。

+2

'Get-ChildItem -Recurse -ErrorAction SilentlyContinue'? –

+0

@Ansgar。リプレイをありがとう。私はまだアクセスが拒否された例外を取得します。 –

+0

'指定されたネットワーク名は使用できなくなりました'このエラーメッセージは間違っていますか、それとも本当にネットワークに問題がありますか?これがスクリプトの実行が停止する理由でしょうか? – sodawillow

答えて

0

エラーを無視する場合は、-ErrorAction SilentlyContinueを使用してください。

herehereのように、このパラメータには他にも有用な値があります。

Here is a nice questionかなりトピックです。

Get-Help about_CommonParametersでこれに関するヘルプを取得することもできます。

(こんにちは、この回答を掘り下げれば、read this ^^)

+0

私はかなり頻繁に '-ErrorAction SilentlyContinue'を使用しています。より短いエイリアス' -ea 4'がより簡単に入力できるようになりました。 '-ea'は' -ErrorAction'と '4'のエイリアスです。なぜなら、' SilentlyContinue'は[ActionPreference](https://msdn.microsoft.com/en-us/library/system.management)の4番目の項目だからです。 Automation.actionpreference(v = vs.85).aspx)列挙型。 – TheMadTechnician

+0

私は適切な短いバージョンが-ea 0 'PS H:\> 0であるべきだと思います。5 | %{ライトホスト$ _ ([System.Management.Automation.ActionPreference] $ _)} ' '0 SilentlyContinue' ' 1 Stop' ' 2 Continue' '3 Inquire' ' 4無視する '5 Suspend' – Igor

+0

' -EA 0を理解する前に 'if(Test-Connection $ host -Quiet -Count 2 -EA 0){#host is online、go on} 'bit:) – sodawillow

0

Where-Objectが不要なディレクトリを除外する前に、Get-ChildItem log4net.dll -Recurseが失敗するという問題があったと思います。

私はディレクトリをハードコーディングするのを避けたいと思いますが、これまでの私の(klunky)ソリューションです。

## Version information will be retrieved for $fileName 
$fileName = 'log4net.dll' 

$ErrorActionPreference = 'Continue' 

## Get directies - excluding those you lack permission to access 
$directories = Get-ChildItem -Directory | 
    Where-Object {$_.FullName -inotmatch 'directory-1' -and 
       $_.FullName -inotmatch 'directory-2' -and 
       $_.FullName -inotmatch 'directory-3'      
} 

## Array to hold version information 
$allFilesVersionInfo = @() 

foreach ($directory in $directories) { 

    ## Get all files recursively 
    $files = Get-ChildItem -Path $directory.FullName $fileName -Recurse 

    foreach ($file in $files) { 

     ## Get version information and add to array 
     $fileVersionInfo = $file | % versioninfo 
     $allFilesVersionInfo += $fileVersionInfo 
    } 
} 

# Write version information in arra to file 
$exportFullPath = "C:\MyJunk\$($fileName)-version.csv" 
$allFilesVersionInfo | Export-Csv -Path $($exportFullPath) 
関連する問題