PowerShell:try-catch 不起作用 [英] PowerShell: try-catch not working

查看:82
本文介绍了PowerShell:try-catch 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 PowerShell 脚本,可以从文件中获取文件名列表,在文件夹中搜索文件名,将它们存档,然后执行其他操作.

I have a PowerShell script that gets a list of file names from a file, searches a folder for the file names, archives them, and then does other stuff.

#make non-terminating errors behave like terminating errors (at script level)
$ErrorActionPreference = "Stop"

#set the folder that has the list and the files
$some_path = "D:\some_folder\"
$archive = "D:\archive\"

#set the list file name
$file_list = $some_path + "file_list.txt"

#get the files that I'm searching for from this list file
$files_to_retrieve = Select String -Path $file_list -Pattern "something" | Select-Object Line

#get the number of files for this search string
$n = $file_list.Length - 1

#seed the while loop counter
$i = 0

#while loop to archive and modify the files
While ($i -le $n)
{
    #set the current file name
    $current_file = $path + $files_to_retrieve[$i].Line

    try
    {
        Copy-Item -Path $current_file -Destination $archive_path
    }
    catch
    {
        Write-Host ("file " + $files_to_retrieve[$i].Line + " not found")
    }

    $data = Get-Content $current_file

    #do modifications here
}

try-catch 没有按预期工作.我在文件列表中有一个 $some_path 中不存在的文件名.我期待 try-catch 停止执行并执行 Write-Host.相反,它不运行 Write-Host 并继续执行 $data = Get-Content $current_file 步骤,这会引发终止错误,因为缺少文件的路径不存在.我该如何解决这个问题?

The try-catch isn't working as expected. I have a file name in the file list that is not present in $some_path. I was expecting try-catch to stop the execution and do the Write-Host. Instead, it doesn't run the Write-Host and continues to the $data = Get-Content $current_file step, which is throwing a terminating error because the path doesn't exist for the missing file. How can I fix this?

推荐答案

正如您所知,您的第一个问题是 try/catch.简要看一下about_Try_Catch_Finally 的文档,您会看到..

Your first problem is the try/catch as you know. Taking a brief look at the documentation for about_Try_Catch_Finally you will see that..

使用 Try、Catch 和 finally 块来响应或处理 终止脚本中的错误.

Use Try, Catch, and Finally blocks to respond to or handle terminating errors in scripts.

您的 Copy-Item 行没有抛出终止错误.我们使用通用参数 -ErrorAction

Your Copy-Item line is not throwing a terminating error. We fix that with the common parameter -ErrorAction

Copy-Item -Path $current_file -Destination $archive_path -ErrorAction Stop

因此,如果出现问题,则应调用 Catch 块.假设那是那里的真正问题.

So if there is a problem then the Catch block should be called. Assuming that was the real issue there.

我认为您还有另一个问题,可能只是打字错误.我不止一次看到以下片段.

You have another issue I think as well that might just be a typo. I see the following snippet more than once.

$file_list[$i].Line

之前您已将 $file_list 声明为D:\some_folder\file_list.txt",这是一个字符串.我想你的意思是在下面.上面的代码将为空,因为 string 没有 line 属性.但是 Select-String 的返回可以!

Earlier you have declared $file_list as "D:\some_folder\file_list.txt" which is a string. I think what you meant to have is below. The above code would be null since string dont have a line property. But the return from Select-String can!

$files_to_retrieve[$i].Line

这篇关于PowerShell:try-catch 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆