微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Powershell - 发生错误后继续执行脚本

如何解决Powershell - 发生错误后继续执行脚本

在下面的脚本中,我让它运行良好,但是一旦发生错误,它就无法在错误之前停止的地方继续,我对此感到有些困惑,因此任何帮助都会很棒.

首先,它加载一个包含通讯组列表的文本文件,然后使用交换查询此列表以获取完全匹配的显示名称,然后通过 CSV 循环将每个用户添加到通讯组列表,这一切正常,这是发生错误时的问题。

catch 语句理解错误,然后执行任务,一旦任务完成,它会从 catch 语句中的 forloop 运行脚本,而不是在 try 中。

添加了 Updatedistros 函数

允许脚本再次运行,但问题是它再次遍历整个文本文件,而不是从发生错误的地方开始,即使它在第 1 行或第 25 行。

下面是代码

##                                                  ##
##  Adds users from text file to distributon lists  ##
##                                                  ##
######################################################
Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline -UserPrincipalName 'UserPrincipalName'
Function Updatedistros{
$ErroractionPreference = 'Stop'
$Path = "C:\INSTALLS\Exchange-distro_Lists\Data"

$res = $Path+"\Results"
$txt1 = "distros.txt"
$txt2 = "Results.txt"
$txt3 = "Mail_Contacts.txt"
$txt4 = "Errors.txt"
$DS1 = gc $Path"\"$txt1  # Gets information from a text file
$1 = "Error Occurred"
$R1 = $res+"\"+$txt2
$R2 = $res+"\"+$txt3
$R3 = $res+"\"+$txt4
try{
Foreach($DS in $DS1){ # Cycles through text file
$DSG = Get-distributionGroup -identity $DS | Select * -ExpandProperty Alias # Gets display name for distribution group
$DS2 = $path+ "\"+ "$DSG.csv"
$NUS = Import-csv $DS2 
#Foreach { $_ + '@zenith.co.uk' } | ` # ammends all users in each text file to contain @zenith.co.uk
#Foreach { $_ –replace “ “,”.” } # ammends all users in each text file to contain @zenith.co.uk
Foreach($NU in $NUS){ # Cycles through each record in each text file
$N1 = $NU.DP
$N2 = $NU.EM
#$NU
Add-distributionGroupMember -identity $DS -Member $N1 #Adds each record to the correct distribution List
"$N1 added to $DS"  | Out-file  $R1  -Append
Write-host "Completed Script"
}
}
}
Catch{
$Erw = $_.Exception.Message
$Erw = $Erw -replace """",""
$NMC = get-content $DS2
$Rers = "Couldn't find object $N1. Please make sure that it was spelled correctly or specify a different object."
$Erw
$Rers
$A1 = $N1 -replace "\s",""
if($Erw -contains $Rers){
Foreach($NM in $NMC){
    Write-host $1
    Write-host $Erw
    $Unq = 'There are multiple recipients matching identity "$N1". Please specify a unique value.'
    $ERM = $_.Exception.Message
      if($ERM -contains $Unq){
      $NU = $NU + '- Cartwright' 
      New-MailContact -Name $N1 -ExternalEmailAddress $N2 -Alias $A1
      "New Mail contact created for $N1" | Out-file $R2 -Append -ErrorAction continue
      Updatedistros
      }
      ElseIf($Erw -contains $Rers){
      New-MailContact -Name $N1 -ExternalEmailAddress $N2 -Alias $A1
      "New Mail contact created for $N1" | Out-file $R2 -Append -ErrorAction continue
      Updatedistros
      }
      Else{
      $Erw | Out-file $R3 -Append -ErrorAction continue
      Updatedistros
      } 
      }
}
Else{
      $Erw | Out-file $R3 -Append -ErrorAction continue }
      Updatedistros
}
}
Updatedistros

解决方法

您不应将整个脚本主体放在 try/catch 块中。这样做会导致很难判断错误发生在哪里以及如何处理。

首先,删除您拥有的 try/catch 块。

接下来,添加一个专门针对您要处理的问题。您说脚本在无法将用户添加到通讯组时无法继续,这使得这是一个很好的添加处理的地方。

另外,我将注释移到行首,而不是行尾。

整行文本难以阅读,但相对较短的列可以轻松扫描。

try{
   #Adds each record to the correct distribution List
   Add-DistributionGroupMember -identity $DS -Member $N1 
}
catch{
   $message = "Failed to modify group $($DS) with new member $($N1)"
   Write-warning $message
   $message | Add-content C:\pathTo\SomeLogfile.txt
   CONTINUE
  }

最后的 Continue 命令是一个特殊的 PowerShell 流控制命令。

它的意思是“在我们当前的循环中停止处理这个项目并继续下一个”。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。