Hi,
I’m having problems running a script against O365 users. I’m trying to change a phone number attribute for some users. It works fine, unless it can’t find the user. Here’s the relevant code:
$users = get-content -Path C:\mfareset.csv
$log = "c:\mfaresetlog.csv"
$number = "(phone number to change to)"
foreach($user in $users){
Try{
$status = Get-MsolUser -UserPrincipalName $user -ErrorAction Stop
Set-MSOLUser -UserPrincipalName $user -AlternateMobilePhones $number
export-csv -Path $log -InputObject $status -Append -NoTypeInformation
}
Catch{[Microsoft.Online.Administration.Automation.MicrosoftOnlineException]
(if $error.exception -like "User Not Found. *"){
$problem = "$User Not Found."
export-csv -path $log -InputObject $problem -Append -NoTypeInformation -Force
}
}
Here’s the error I keep getting:
Get-MsolUser : User Not Found. User: abcdef@contoso.com.
At C:\resetmfa.ps1:12 char:27
-
$status = Get-MsolUser -UserPrincipalName $user 1>> $log | sele ...
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
CategoryInfo : OperationStopped: ( [Get-MsolUser], MicrosoftOnlineException
-
FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.UserNotFoundException,Microsoft.Online.Administration.Automation.GetUser
Any help you could provide would be appreciated.
Thank you
What happens if you change your catch to “Microsoft.Online.Administration.Automation.UserNotFoundException,Microsoft.Online.Administration”
Hi Mike,
not sure how your CSV file looks like. but rather than using get-content, I would use Import-csv and iterate through each object. For example if your CSV file had the UPN with the users UPN.
$status = Get-MsolUser -UserPrincipalName $user.UPN
Set-MSOLUser -UserPrincipalName $user.UPN -AlternateMobilePhones $number
$status | export-csv -Path $log NoTypeInformation -Append
}
Hey Mike,
If the goal is to capture any the error message and user, even if it is not due to “User not found” you could try.
$User = "DoesnotExist"
Try {
Get-MsolUser -UserPrincipalName $User -ErrorAction Stop
}
Catch {
$Problem = "$_"
$Problem | Out-File $Log -Append
}
Running this three times returns the following in the $Log path.
User Not Found. User: DoesnotExist.
User Not Found. User: DoesnotExist.
User Not Found. User: DoesnotExist.
Thanks, Shihan - I’ve actually tried both import-csv and get-content, and the error remains the same regardless of which technique I use.
Hope you are connecting to MSO service(steps mentioned below) before implementing the above script.
-Import-Module MSOnline
-$cred = Get-Credential
-Connect-MsolService -Credential $cred
Jon - no change in the error.
Kaj - I tried your code snippet, but with no success. I got the following error:
Cannot process argument because the value of argument “name” is not valid. Change the value of the “name” argument and run the operation again.
At C:\resetphone.ps1:20 char:17
-
export-csv -path $log -InputObject $problem -Append -NoTypeInfor ...
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
CategoryInfo : InvalidArgument: ( [Export-Csv], PSArgumentException
-
FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.ExportCsvCommand
Hey Mike,
Did you retain:
Catch{[Microsoft.Online.Administration.Automation.MicrosoftOnlineException]
(if $error.exception -like "User Not Found. *"){
$problem = "$User Not Found."
export-csv -path $log -InputObject $problem -Append -NoTypeInformation -Force
Because I completely replaced the Catch block with:
Catch {
$Problem = "$_"
$Problem | Out-File $Log -Append
}
Hey, Kaj - yes, I replaced the catch block:
Try{
$status = Get-MsolUser -UserPrincipalName -ErrorAction Stop
Set-MSOLUser -UserPrincipalName $user -AlternateMobilePhones $number
export-csv -Path $log -InputObject $status -Append -NoTypeInformation
}
Catch{
$problem = "$_"
export-csv -path $log -InputObject $problem -Append -NoTypeInformation
}
}
BTW, I’m using PS version 4.0, if that makes a difference.
Is there an issue with using “$Problem | Out-File $Log -Append” instead of “Export-Csv”
None. Changed it from out-file to export-csv…same error. i don’t think the output option is the issue. It’s almost like PS isn’t recognizing the underlying .NET class of the error. In fact, I even tried the same script on a different machine…same result.
Apologies I was not clear previously, I am not using the .Net Class
Connect-MsolService
$users = get-content -Path C:\mfareset.csv
$log = "c:\mfaresetlog.csv"
$number = "(phone number to change to)"
foreach($user in $users){
Try{
$status = Get-MsolUser -UserPrincipalName $user -ErrorAction Stop
Set-MSOLUser -UserPrincipalName $user -AlternateMobilePhones $number
export-csv -Path $log -InputObject $status -Append -NoTypeInformation
}
Catch {
$Problem = "$_"
$Problem | Out-File $Log -Append
}
Thanks, Kaj - I used your script and (with a slight tweak) it worked. I think part of the problem is that when it can’t find the user, it throws more than 1 error. I changed
$problem = “$_”
to
$notfound = $error[2].Exception.Message
$Problem = “$user,$notfound”
$Problem | Out-File $Log -Append
$error.clear()
It’s actually writing to the log file now, in exactly the format I wanted.
Thanks again for getting me back on track.