O365 query problem

Welcome Forums General PowerShell Q&A O365 query problem

This topic contains 13 replies, has 5 voices, and was last updated by

 
Participant
3 weeks, 3 days ago.

  • Author
    Posts
  • #114411

    Participant
    Points: -13
    Rank: Member

    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

  • #114417
    Jon

    Participant
    Points: 22
    Rank: Member

    What happens if you change your catch to "Microsoft.Online.Administration.Automation.UserNotFoundException,Microsoft.Online.Administration"

    • #114463

      Participant
      Points: -13
      Rank: Member

      Jon – no change in the error.

  • #114420

    Participant
    Points: -17
    Rank: Member

    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
    
    }
    
    

     

    • #114460

      Participant
      Points: -13
      Rank: Member

      Thanks, Shihan – I've actually tried both import-csv and get-content, and the error remains the same regardless of which technique I use.

  • #114424
    Kaj

    Participant
    Points: 1
    Rank: Member

    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.

    • #114465

      Participant
      Points: -13
      Rank: Member

      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

    • #114510
      Kaj

      Participant
      Points: 1
      Rank: Member

      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
          }
    • #114523

      Participant
      Points: -13
      Rank: Member

      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.

    • #114525
      Kaj

      Participant
      Points: 1
      Rank: Member

      Is there an issue with using "$Problem | Out-File $Log -Append" instead of "Export-Csv"

    • #114528

      Participant
      Points: -13
      Rank: Member

      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.

  • #114462
    vjs

    Participant
    Points: 2
    Rank: Member

    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

  • #114538
    Kaj

    Participant
    Points: 1
    Rank: Member

    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
        }
    • #115020

      Participant
      Points: -13
      Rank: Member

      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. 🙂

You must be logged in to reply to this topic.