powershell - 如何处理 Copy-Item 异常?

标签 powershell exception-handling

我正在编写一个使用 Copy-Item 的 PowerShell 脚本.我想知道Copy-Item时会抛出什么样的异常无法将文件从源复制到目标?

来自 this link ,看不出会抛出什么样的异常。

我想在失败的情况下处理异常。

最佳答案

在 Try-Catch 中将标签“-errorAction stop”添加到 Copy-Item 调用,如下所示。 errorAction 抛出一个 Try-Catch 可以处理的错误。

$filepathname = 'valid file path and name'
$dest = '\DoesntExist\'

try
{
    Copy-Item $filepathname $dest  -errorAction stop
    Write-Host "Success"
}
catch
{
    Write-Host "Failure"
}

关于powershell - 如何处理 Copy-Item 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32727252/

相关文章:

c# - 在 PowerShell 中隐式转换为 C# 中定义的结构的 bool 失败

powershell - PowerShell 中的停止器(程序执行期间耗时)

azure - 使用 Powershell 将范围添加到 Azure AD 应用程序(公开 API)

powershell - 在 powershell 中执行 Enter-PSSession 时出现随机错误

oracle - 如何在异常处理 block 中重新引发pl/sql异常?

ruby-on-rails - 如何在 Rails 应用程序中引发错误?

powershell - Powershell:AD-GET无法与变量一起使用

c# - 为什么要使用特定的异常捕获 block

php - Yii:多事件最佳实践

c# - 实现通用自定义异常的优缺点