【发布时间】:2020-11-11 17:26:53
【问题描述】:
我有一个代码,可以从 SharePoint 下载文件,对其进行编辑,然后将其上传回 SharePoint,最后发送一封确认电子邮件。我有每个任务的功能。代码运行良好。
但是,我想为某些用户在 SharePoint 中打开文件时的条件添加错误异常,显示错误消息和退出代码。我遇到的问题是即使出现异常代码也会继续运行。在下面的代码中,即使 getSharePointFile 函数失败,也会调用 sendMail 函数。
我已经尝试过 $ErrorActionPreference = "Stop" 但是这样,catch 块没有执行,我的自定义错误消息框也没有显示。提前致谢。
以下是相关代码:
function getSharePointFile {
Connect-PnPOnline $SharepointURL -UseWebLogin
Get-PnPFile -Url $fileRelativeURL -Path $localFilePath -FileName $fileName -AsFile -Force
}
function runCatch {
$showMessage = [System.Windows.Forms.MessageBox]::Show($_)
exit
}
try {getSharePointFile}
catch{runCatch}
try {updateAuditResults}
catch{runCatch}
try {uploadToSharePoint}
catch{runCatch}
try {sendMail}
catch{runCatch}
【问题讨论】:
标签: powershell try-catch exit