[英]PowerShell Get-ChildItem how to catch Exception
Im currently programming a visual error GUI that catches any exception while processing and give's the user an "easy to understand" error message. 我目前正在编写一个可视错误GUI,在处理过程中捕获任何异常,并为用户提供“易于理解”的错误消息。 But it seems that I can't catch any exceptions while using the Get-ChildItem
cmdlet. 但似乎在使用Get-ChildItem
cmdlet时我无法捕获任何异常。 Do I have to use a different method than try / catch? 我是否必须使用与try / catch不同的方法?
Here's the PowerShell script: 这是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")
}
I tried to create an exception by using a false -path
which results in a DriveNotFoundException
. 我尝试使用false -path
创建一个异常,导致DriveNotFoundException
。 But it looks like I can't catch it by using try / catch. 但看起来我无法通过使用try / catch来捕获它。
Add -ErrorAction Stop
to the Get-ChildItem
cmdlet: 将-ErrorAction Stop
添加到Get-ChildItem
cmdlet:
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")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.