How to catch errors when using powershell 4.0, Office 365 cmdlets
hi
i want catch errors when using powershell office 365 cmdlets unable so.
e.g. suppose want catch error when creating user account 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 error not caught because get:
ps c:\windows\system32> try
>> {
>> new-msoluser -displayname "andre thompson" -userprincipalname andre.thompson@my.uwi.edu
>> }
>> catch
>> {
>> "error"
>> $error[0]
>> }
>>
new-msoluser : unable add user because user user principal name 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 add user because user user principal name 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 doing wrong?
regards andre thompson
hi andre,
try adding -erroraction stop new-msoluser. can't test myself, have standard ad cmdlets figured worth mentioning.
don't retire technet! - (don't give yet - 12,950+ strong , growing)
Windows Server > Windows PowerShell
Comments
Post a Comment