Forum Discussion

cimpercee's avatar
cimpercee
Copper Contributor
Mar 06, 2023

Unable to catch the error

I am trying to query a user and was hoping to build a csv file for those that I can and cannot get. I'm not really sure why.

 

 

users = import-csv ".\OU.csv" -Delimiter ";"
    foreach ($user in $users) {
    $O365user = Get-ADUser -SearchBase $user.ou -Properties * -Filter * | ? {$_.admindescription -eq $null -and $_.emailaddress -ne $null} | select EmailAddress,SamaccountName
                              }
    foreach ($User in $O365user){ 
   try {
        $dUser = Get-mailbox $user.emailaddress -ErrorAction Stop
    }
    catch {
        if ($_ -like "*The operation couldn't be performed because object*") {
            "User '$u' does not exist."
        }
        else {
            "An error occurred: $_"
        }
        continue
    }
    "User '$($user.displayname)' exists."
    # Do stuff with $ADUser

}

 

 

7 Replies

  • cimpercee I think this should work:

     

    users = import-csv ".\OU.csv" -Delimiter ";"
        foreach ($user in $users) {
        $O365user = Get-ADUser -SearchBase $user.ou -Properties * -Filter * | ? {$_.admindescription -eq $null -and $_.emailaddress -ne $null} | select EmailAddress,SamaccountName
                                  }
        foreach ($User in $O365user){ 
       try {
            $dUser = Get-mailbox $user.emailaddress -ErrorAction Stop
            $dUser | export-csv -path c:\data\users.csv -NoTypeInformation -Delimiter ';' -Encoding UTF8 -Append 
        }
        catch {
                "User '$u' does not exist."
    }

     

    If the user can be found, it exports the data to a CSV file and appends it for every user. It the user can't be found, it will just write that it can't be found and continue processing

  • It looks like you're trying to query a list of users from a CSV file and then check if they have an Office 365 mailbox. The script seems to be missing the part where you write the results to a CSV file.

    To write the results to a CSV file, you can use the Export-CSV cmdlet. Here's an updated version of your script that writes the results to a CSV file:

    $users = import-csv ".\OU.csv" -Delimiter ";"
    $results = @()
    foreach ($user in $users) {
    $O365user = Get-ADUser -SearchBase $user.ou -Properties * -Filter * | ? {$_.admindescription -eq $null -and $_.emailaddress -ne $null} | select EmailAddress,SamaccountName
    foreach ($User in $O365user){
    try {
    $dUser = Get-mailbox $user.emailaddress -ErrorAction Stop
    $result = [PSCustomObject]@{
    DisplayName = $user.displayname
    EmailAddress = $user.emailaddress
    Status = "Exists"
    }
    $results += $result
    }
    catch {
    if ($_ -like "*The operation couldn't be performed because object*") {
    $result = [PSCustomObject]@{
    DisplayName = $user.displayname
    EmailAddress = $user.emailaddress
    Status = "Does not exist"
    }
    $results += $result
    }
    else {
    "An error occurred: $_"
    }
    continue
    }
    }
    }
    $results | Export-Csv -Path ".\Results.csv" -NoTypeInformation


    This script creates an empty array $results and then adds a custom object to it for each user that is found. The custom object contains the user's display name, email address, and status (either "Exists" or "Does not exist").

    At the end of the script, the results are exported to a CSV file using the Export-Csv cmdlet. The -NoTypeInformation parameter is used to exclude the object type information from the CSV file.
    • cimpercee's avatar
      cimpercee
      Copper Contributor
      I did cut the rest of the code because for this query, I can't even catch the error. thanks. I will try your suggestion

Resources