我正在编写一个使用
Copy-Item
的PowerShell脚本。我想知道当Copy-Item
无法将文件从源复制到目标时会抛出什么样的异常?
在此链接中,我没有看到会抛出什么样的异常。
在失败的情况下,我想处理该异常。Copy-Item
的PowerShell脚本。我想知道当Copy-Item
无法将文件从源复制到目标时会抛出什么样的异常?
在此链接中,我没有看到会抛出什么样的异常。
在失败的情况下,我想处理该异常。$filepathname = 'valid file path and name'
$dest = '\DoesntExist\'
try
{
Copy-Item $filepathname $dest -errorAction stop
Write-Host "Success"
}
catch
{
Write-Host "Failure"
}
CopyTo()
和CreateSubdirectory()
方法定义了以下异常:
try {
Copy-Item ...
} catch {
$exception = $_
...
}
Copy-Item
会抛出非终止错误,这些错误不能被try-catch块处理。使用-ErrorAction Stop
将错误提升为脚本终止错误,就像使用throw
创建的那样。脚本终止错误可以被try-catch块处理,因此问题得到了解决! - undefined