Forum Discussion
cimpercee
Mar 06, 2023Copper Contributor
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...
Varun_Ghildiyal
Mar 07, 2023Iron Contributor
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.
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
Mar 07, 2023Copper 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