2013-04-26 162 views
4

以下是我要执行的脚本。这里的问题是,一旦发生异常,它会停止执行,我在catch块中使用了continue,但这不起作用。即使在发生异常之后,如何让它工作,它应该在foreach中循环。继续执行异常

我也使用了一个while($true)循环,但进入了无限循环。如何去做呢?

$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 
    [email protected]() 


    $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) 
} 

请帮忙。

+0

为什么不改变'$ ErrorActionPreference =“继续”'?不适合你吗? – 2013-04-26 07:31:40

+0

是否有导致异常的特定行? – 2013-04-26 07:31:59

回答

6

当您要处理错误时,请使用try {...} catch {...}。如果您想忽略它们,您应该将$ErrorActionPreference = "Continue"(或"SilentlyContinue")设置为@ C.B。建议,或者使用-ErrorAction "SilentlyContinue"来提升错误的特定操作。如果你想从某一指令处理错误,你就会把该指令在try {...} catch {...}块,而不是整个循环,例如:

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) 
    } 
} 
+2

因为我想记录非终止错误,所以设置'$ ErrorActionPreference'对我没有多大帮助。只有它的工作时间是'$ ErrorActionPreference =“停止”',但比捕获执行暂停。即使出现错误,我也想继续执行。请看我的答案,这解决了我的问题。感谢您的回答。 :) – Ishan 2013-04-26 07:58:36

+0

您需要'Stop'作为'try..catch'工作的错误动作,因为它只捕获终止错误。没有终止错误,首先就没有什么可以赶上的。 – 2013-04-26 08:26:33

0

修改为下面的代码。用下面的一段代码后move-spuser -identity $user -newalias $newalias -ignoresid -Confirm:$false

if($?) 
{ 
    LogWrite ("Done!") 
    LogWrite (" ") 
} 
else 
{ 
    LogWrite ($Error[0].ToString()) 
    LogWrite (" ") 
} 
+3

请勿使用'$?'。永远。请检查'$ LastExitCode'(请参阅[这里](http://stackoverflow.com/a/12679208/1630171))。 – 2013-04-26 08:12:11

0

一种适合我的工作就是设置$ErrorActionPreference变量停止,然后重新回来继续在catch块:

$e = $ErrorActionPreference 
$ErrorActionPreference="stop" 

try 
{ 
    #Do Something that throws the exception 
} 
catch 
{ 
    $ErrorActionPreference=$e 

} 

$ErrorActionPreference=$e;