Quantcast
Channel: Exchange Server Development forum
Viewing all articles
Browse latest Browse all 7132

How to catch errors when using powershell 4.0, Office 365 cmdlets

$
0
0
Hi
I want to catch errors when using Powershell Office 365 cmdlets but I am unable to do so.
E.g. Suppose I want to catch the error when I am creating a user account that already exists:
try
{
 New-MsolUser -DisplayName "Andre Thompson" -UserPrincipalName andre.thompson@my.uwi.edu
}
catch
{
"error found..."
$error[0]
}
However the error is not caught because I get:
PS C:\Windows\system32> try
>> {
>>   New-MsolUser -DisplayName "Andre Thompson" -UserPrincipalName andre.thompson@my.uwi.edu
>> }
>> catch
>> {
>> "error"
>> $error[0]
>> }
>>
New-MsolUser : Unable to add this user because a user with this user principal name already exists -
andre.thompson@my.uwi.edu.
At line:3 char:2
+  New-MsolUser -DisplayName "Andre Thompson" -UserPrincipalName andre.thompson@my ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [New-MsolUser], MicrosoftOnlineException
    + FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.UserAlreadyExistsException,Microsoft.Online.A
   dministration.Automation.NewUser
PS C:\Windows\system32>
What am I doing wrong?
- Andre

Regards Andre Thompson


Viewing all articles
Browse latest Browse all 7132

Trending Articles