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]
}
{
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
>> {
>> 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