PowerShell 中 Power BI Rest API 的错误处理
Error Handling in PowerBI RestAPIs in PowerShell
我正在尝试为我的 PowerBI 休息 API 在 PowerShell 中遇到错误时创建一个自动电子邮件系统。基本上,我的 PowerBI RestAPI 有时会出现以下错误:
每当出现此错误时,我希望我的 PowerShell 脚本给我发送一封电子邮件。我目前正在测试以下代码,但它根本不起作用。理想情况下,当以下代码发生错误时,它应该打印 "Error"。相反,它只是打印错误并停止。我想知道是否有人有使用 PowerBI rest API 进行错误处理的经验。
$User = "email@hotmail.com"
$PW = "mypw"
$SecPasswd = ConvertTo-SecureString $PW -AsPlainText -Force
$myCred = New-Object System.Management.Automation.PSCredential($User,$SecPasswd)
Connect-PowerBIServiceAccount -Credential $myCred
# 2. Refresh Dataset
try{
Invoke-PowerBIRestMethod -Url "datasets/2-cdaf123fa5/refreshes" -Method Post -Body '{"notifyOption": "MailOnFailure"}'
}
# 3. Check if there is an error
catch {
$emailSmtpServer = "smtp.gmail.com"
$emailSmtpServerPort = "587"
$emailSmtpUser = "email@gmail.com"
$emailSmtpPass = "password"
$emailFrom = "email@gmail.com"
$emailTo = "email@gmail.com"
$emailMessage = New-Object System.Net.Mail.MailMessage( $emailFrom , $emailTo )
$emailMessage.Subject = "subject"
$emailMessage.Body = "body"
$SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort )
$SMTPClient.EnableSsl = $True
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass );
$SMTPClient.Send( $emailMessage )
}
处理这个问题的方法很少。你有一个非终止错误。如果我的解决方案不符合您的需求,您可以返回上面的 link 并搜索下面的问题。
Try/Catch/Finally The Try/Catch/Finally statements, added in
PowerShell 2.0, are the preferred way of handling terminating errors.
They cannot be used to handle non-terminating errors, unless you force
those errors to become terminating errors with ErrorAction or
$ErrorActionPreference set to Stop.
A value of Stop causes non-terminating errors to be treated as
terminating errors instead, immediately halting the Cmdlet’s
execution. This also enables you to intercept those errors in a
Try/Catch or Trap statement, as described later in this section.
在脚本顶部添加
$ErrorActionPreference = "Stop"
希望对您有所帮助。
我正在尝试为我的 PowerBI 休息 API 在 PowerShell 中遇到错误时创建一个自动电子邮件系统。基本上,我的 PowerBI RestAPI 有时会出现以下错误:
每当出现此错误时,我希望我的 PowerShell 脚本给我发送一封电子邮件。我目前正在测试以下代码,但它根本不起作用。理想情况下,当以下代码发生错误时,它应该打印 "Error"。相反,它只是打印错误并停止。我想知道是否有人有使用 PowerBI rest API 进行错误处理的经验。
$User = "email@hotmail.com"
$PW = "mypw"
$SecPasswd = ConvertTo-SecureString $PW -AsPlainText -Force
$myCred = New-Object System.Management.Automation.PSCredential($User,$SecPasswd)
Connect-PowerBIServiceAccount -Credential $myCred
# 2. Refresh Dataset
try{
Invoke-PowerBIRestMethod -Url "datasets/2-cdaf123fa5/refreshes" -Method Post -Body '{"notifyOption": "MailOnFailure"}'
}
# 3. Check if there is an error
catch {
$emailSmtpServer = "smtp.gmail.com"
$emailSmtpServerPort = "587"
$emailSmtpUser = "email@gmail.com"
$emailSmtpPass = "password"
$emailFrom = "email@gmail.com"
$emailTo = "email@gmail.com"
$emailMessage = New-Object System.Net.Mail.MailMessage( $emailFrom , $emailTo )
$emailMessage.Subject = "subject"
$emailMessage.Body = "body"
$SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort )
$SMTPClient.EnableSsl = $True
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass );
$SMTPClient.Send( $emailMessage )
}
处理这个问题的方法很少。你有一个非终止错误。如果我的解决方案不符合您的需求,您可以返回上面的 link 并搜索下面的问题。
Try/Catch/Finally The Try/Catch/Finally statements, added in PowerShell 2.0, are the preferred way of handling terminating errors. They cannot be used to handle non-terminating errors, unless you force those errors to become terminating errors with ErrorAction or $ErrorActionPreference set to Stop.
A value of Stop causes non-terminating errors to be treated as terminating errors instead, immediately halting the Cmdlet’s execution. This also enables you to intercept those errors in a Try/Catch or Trap statement, as described later in this section.
在脚本顶部添加
$ErrorActionPreference = "Stop"
希望对您有所帮助。