Powershell $ erroractionpreference overrides -erroraction

I am trying to create a user with the new-mailbox command. I need to wait for it to be created and replicated, so when I try to install the -aduser -description command, it actually finds the account.

I saw sleeping seconds, but would rather look at the true test and then execute a type method.

I set erroractionpreference to stop at the beginning of my script. But I thought I could change it for each team if I wanted.

-EA Test # 1 Fails $ erroractionpreference = "stop" 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 Test # 2 Works $ erroractionpreference = "silently continue" and no -Erroraction

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

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

-EA Test # 3 Fails. $ erroractionpreference = "silentcontinue" and -Erroraction Stop

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


    .........

$erroractionpreference, -erroractionpreference .

, ? .

$erroractionpreference = " " Script $ erroractionpreference = "stop"

0

Source: https://habr.com/ru/post/1627954/


All Articles