继续执行异常 [英] Continue execution on Exception
问题描述
以下是我要执行的脚本。这里的问题是一旦发生异常,它停止执行,我在catch块中使用了 continue
,但是没有起作用。 / p>
我还使用了一个 foreach
code> while($ true)循环,但进入无限循环。如何解决?
$ ErrorActionPreference =Stop;
尝试
{
#循环遍历站点中的每个用户
foreach($ user在$ users中)
{
#创建一个数组,将用于从域/会员提供者中分割用户名
$ a = @()
$ displayname = $ user.DisplayName
$ userlogin = $ user.UserLogin
#将用户名与域/会员提供者分开
if($ userlogin.Contains('\'))
{
$ a = $ userlogin.split(\)
$ username = $ a [1]
}
elseif($ userlogin.Contains(':'))
{
$ a = $ userlogin.split(:)
$ username = $ a [1]
}
#创建新的用户名基于给定的输入
$ newalias = $ newprovider +\+ $ username
if(-not $ convert)
{
$ answer =读取-Host您的第一个用户将从$ userlogin更改为$ newalias。是否要继续处理所有用户?[Y] ,[N] o
开关($ answer)
{
Y{$ convert = $ true}
y{$ convert = $如果(($ userlogin -like$ oldprovider *)和$ convert)
{
LogWrite(迁移用户旧:+ $ user +新用户:+ $ newalias +)
move-spuser -identity $ user -newalias $ newalias -ignoresid -Confirm:$ false
LogWrite(Done)
}
}
}
catch {
LogWrite(捕获异常)
LogWrite($ Error [0] .Exception)
}
请帮助。 / p>
您使用 try {...} catch {...}
当你想要处理错误。如果你想忽略它们,你应该设置 $ ErrorActionPreference =Continue
(或SilentlyContinue
)as @ CB建议或使用 -ErrorActionSilentlyContinue
来处理提高错误的特定操作。如果你想处理某个指令中的错误,你可以把这个指令放在 try {...} catch {...}
中,而不是整个循环,例如:
foreach($ user in $ users){
...
try {
如果(($ userlogin -like$ oldprovider *)和$ convert){
LogWrite(迁移用户旧:+ $ user +新用户:+ $ newalias +)
move-spuser -identity $ user -newalias $ newalias -ignoresid -Confirm:$ false
LogWrite(Done)
}
} catch {
LogWrite 捕获异常)
LogWrite($ Error [0] .Exception)
}
}
Below is the script I want to execute. The issue here is once an exception occurs it stops executing, I used continue
in the catch block but that did not work. How do I get it working even after an exception occurs it should loop in foreach
.
I also used a while($true)
loop but that went into infinite loop. How to go about it?
$ErrorActionPreference = "Stop";
try
{
# Loop through each of the users in the site
foreach($user in $users)
{
# Create an array that will be used to split the user name from the domain/membership provider
$a=@()
$displayname = $user.DisplayName
$userlogin = $user.UserLogin
# Separate the user name from the domain/membership provider
if($userlogin.Contains('\'))
{
$a = $userlogin.split("\")
$username = $a[1]
}
elseif($userlogin.Contains(':'))
{
$a = $userlogin.split(":")
$username = $a[1]
}
# Create the new username based on the given input
$newalias = $newprovider + "\" + $username
if (-not $convert)
{
$answer = Read-Host "Your first user will be changed from $userlogin to $newalias. Would you like to continue processing all users? [Y]es, [N]o"
switch ($answer)
{
"Y" {$convert = $true}
"y" {$convert = $true}
default {exit}
}
}
if(($userlogin -like "$oldprovider*") -and $convert)
{
LogWrite ("Migrating User old : " + $user + " New user : " + $newalias + " ")
move-spuser -identity $user -newalias $newalias -ignoresid -Confirm:$false
LogWrite ("Done")
}
}
}
catch {
LogWrite ("Caught the exception")
LogWrite ($Error[0].Exception)
}
Kindly help.
You use try {...} catch {...}
when you want to handle errors. If you want to ignore them, you should set $ErrorActionPreference = "Continue"
(or "SilentlyContinue"
) as @C.B. suggested, or use -ErrorAction "SilentlyContinue"
for the particular operation raising the error. If you want to handle errors from a certain instruction, you'd put that instruction in the try {...} catch {...}
block, not the entire loop, e.g.:
foreach($user in $users) {
...
try {
if(($userlogin -like "$oldprovider*") -and $convert) {
LogWrite ("Migrating User old : " + $user + " New user : " + $newalias + " ")
move-spuser -identity $user -newalias $newalias -ignoresid -Confirm:$false
LogWrite ("Done")
}
} catch {
LogWrite ("Caught the exception")
LogWrite ($Error[0].Exception)
}
}
这篇关于继续执行异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!