powershell $ erroractionpreference overrides -erroraction

时间:2014-07-26 22:33:29

标签: powershell

我正在尝试使用new-mailbox命令创建用户。我需要等待它创建和复制,所以当我尝试set-aduser -description命令时,它实际上找到了一个帐户。

我已经看过sleep -seconds但是宁愿看一个真正的测试,然后执行type方法。

我已将错误偏好设置为在脚本开头停止。但是如果我想的话,我想我可以在每个命令上改变它。

-EA测试#1失败$ erroractionpreference ="停止" and -Erroraction SilentlyContinue

PS C:\Windows\system32>     $erroractionpreference = "stop"
Do {
   Sleep -seconds 1
   $UserExists = Get-aduser notavaliduser -Erroraction SilentlyContinue |fw IsValid
   write-host "." -NoNewline
   }
While (!$UserExists)

Get-aduser : Cannot find an object with identity: 'notavaliduser' under: 'DC=XXX,DC=local'.
At line:4 char:22
+        $UserExists = Get-aduser notavaliduser -Erroraction SilentlyContinue |fw  ...
+                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (notavaliduser:ADUser) [Get-ADUser],        ADIdentityNotFoundException
    + FullyQualifiedErrorId :  ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,
Microsoft.Acti    veDirectory.Management.Commands.GetADUser

-EA测试#2工作$ erroractionpreference ="默默地继续"并且没有去除

   PS C:\Windows\system32>    $erroractionpreference = "silentlycontinue"
Do {
   Sleep -seconds 1
   $UserExists = Get-aduser notavaliduser  |fw IsValid
   write-host "." -NoNewline
   }
 While (!$UserExists)

 ...............................

-EA测试#3失败。 $ erroractionpreference ="默默地继续"和 - 停止

    PS C:\Windows\system32> $erroractionpreference = "silentlycontinue"
    Do {
    Sleep -seconds 1
    $UserExists = Get-aduser notavaliduser -Erroraction Stop |fw IsValid
    write-host "." -NoNewline
    }
    While (!$UserExists)


    .........

为什么$ erroractionpreference工作但是-rororactionpreference似乎完全被忽略了。

是否可以从停止更改变量,然后在执行此位后将其放回?看起来像一个钻机。

$ erroractionpreference ="默默地继续" 脚本 $ erroractionpreference ="停止"

0 个答案:

没有答案
相关问题