2016-08-11 33 views
2

現在、Imは処理中に例外を検出してユーザーに「分かりやすい」エラーメッセージを表示する視覚的エラーGUIをプログラミングしています。しかし、Get-ChildItemコマンドレットを使用している間は例外を検出できないようです。 try/catch以外の方法を使用する必要がありますか?PowerShell Get-ChildItem catchの方法例外

ここでPowerShellのスクリプトは次のとおりです。

if ($checkBox1.Checked) { 
    Try{ 
     Get-ChildItem -path K:\adm_spm_logdb_data\ADP\DATA |Rename-Item -newname { $($_.BaseName -split '_provide')[0] + $_.Extension }; 
     }catch [System.Exception]{ 
     $listBox1.Items.Add("An error occured while trying to process ADP-Files please check the manual!") 
     } 
     $listBox1.Items.Add("Please check your System files to ensure the process has finished") 
    } 

私はDriveNotFoundException、その結果、偽-pathを使用して例外を作成しようとしました。でもtry/catchを使って捕まえられないようです。

答えて

3

Get-ChildItemコマンドレットに-ErrorAction Stopを追加します。

if ($checkBox1.Checked) { 
    Try{ 
     Get-ChildItem -path "K:\adm_spm_logdb_data\ADP\DATA" -ErrorAction Stop |Rename-Item -newname { $($_.BaseName -split '_provide')[0] + $_.Extension }; 
     }catch [System.Exception]{ 
     $listBox1.Items.Add("An error occured while trying to process ADP-Files please check the manual!") 
     } 
     $listBox1.Items.Add("Please check your System files to ensure the process has finished") 
    } 
+0

は、それが働いたありがとう!私はできるだけ早くこの答えを受け入れます。 –

関連する問題