Answered by:
get-mailbox error handling

Question
-
hello all,
here is my scenario.
i have a large txt file with a list of aliases. i want to run the entire list against get-mailbox.
i would like for the FOUND mailbox aliases to be output to a txt file or csv sheet.
i would like the errors "The operation couldn't be performed because object 'ACCOUNTNAME' couldn't be found ..." to be output to a separate file or csv sheet.
when i currently tried to do this i have been unable to get the errors to output to a file.
Thursday, May 28, 2015 6:33 PM
Answers
-
hello all,
thanks for all your attempts to assist. i have not been able to get the get-mailbox cmd to catch and output errors to a txt file. here is the solution i came up with. just thought id share.
$Users = Get-content "C:\userlist.txt"
$users | ForEach { $exist = [bool](Get-mailbox $_ -erroraction SilentlyContinue);
if ($Exist){$_ >> "C:\_Found.txt"}
else
{$_ >> "C:\_not_found.txt"}
}hope this helps someone else
- Marked as answer by Drcybercure Tuesday, June 16, 2015 5:58 PM
Tuesday, June 16, 2015 5:58 PM
All replies
-
here is my current script - the FOUND aliases go to the csv file just fine.
How can i get the aliases of the NOT found "The operation couldn't be performed because object 'ACCOUNTNAME' couldn't be found ..." to be output to a separate text file or csv sheet.
$Users = Get-content "C:\users.txt"
$Users | foreach-object {
Get-Mailbox $_ |select-object Alias |export-csv -append "C:\found.csv" -NoTypeInformation
}
Thursday, May 28, 2015 8:27 PM -
Here is a good starting point for information about redirecting output:
https://technet.microsoft.com/en-us/library/hh847746.aspx
I find this one to be a little easier to follow:
http://www.computerperformance.co.uk/powershell/powershell3-redirection.htm
Thursday, May 28, 2015 9:14 PM -
Hi Drcybercure,
Try the following command:
Get-content "C:\users.txt" | foreach-object { Get-Mailbox $_ |select-object Alias |export-csv -append "C:\found.csv" -NoTypeInformation} 2>c:\nofound.csv
Best regards,
Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.
Niko Cheng
TechNet Community SupportFriday, May 29, 2015 8:59 AM -
Hello,
Thanks this is good. however..........
I do get the 2 files found.csv and no found.csv. the found.csv file conatins the list of good aliases as expected however the nofound.csv file is the entire error.
"The operation couldn't be performed because object 'alias' couldn't be found
on 'server name'."Is there a way to only have the nofound alias in the nofound.csv just like the found csv ?
Monday, June 1, 2015 2:55 PM -
Hi Drcybercure,
Base on my knowledge, I don't think there is any way can achieve your requirement.
We can only filter the nofound alias manually from the csv file.
Best regards,
Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.
Niko Cheng
TechNet Community SupportWednesday, June 3, 2015 9:27 AM -
Hi Drcybercure,
Use exception handling, put the error expected cmdlet inside the try{} block and catch block should have the action upon error that would be writing out to a file or standard output
try { Get-Mailbox $Username } catch { Write-Host "$Username not found in AD" }
References:
Using Try, Catch, Finally Blocks for PowerShell Error Handling
about_Try_Catch_Finally
https://technet.microsoft.com/en-us/library/hh847793.aspx
Regards,
Satyajit
Please“Vote As Helpful” if you find my contribution useful or “MarkAs Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.
- Edited by Satyajit321 Wednesday, June 3, 2015 10:29 AM Added referneces
Wednesday, June 3, 2015 10:25 AM -
Hi
I have done some corrections,changes and your script should be working just fine.
If you had tried earlier script block must be giving error and not working as expected for Exchange cmdlets.
Reason being mentioned in the article "Why does catch not catch in Exchange?"Issue:Get-mailbox doesn't stop on error, it attempts to find results for all the inputs given to it. Hence try block fails to work.
Solution:
Adding -ErrorAction Stop after the get-mailbox cmdlet.#Get-UserMailbox.ps1
$Users = Get-content "C:\Users.txt" foreach ($Username in $Users) { try { Get-Mailbox $Username -ErrorAction Stop |select-object Alias |export-csv -append "C:\UsersFound.csv" -NoTypeInformation } catch { $temp = "$Username, not found in AD" Out-File -append -InputObject $temp "C:\UsersNotfound.csv" -NoClobber } }
Regards,
Satyajit
Please “Vote As Helpful” if you find my contribution useful or “Mark As Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.
Wednesday, June 3, 2015 11:16 AM -
hello,
i tried this out but im still getting the output on the screen and not getting just the aliases in the notfound.csv.
any other ideas?
Wednesday, June 3, 2015 6:09 PM -
Hi,try this
Get-content "C:\Users.txt"|ForEach-Object{if($account=([adsisearcher]"(samaccountname=$($_.samaccountname))").findone()){$exists=$true$enabled=[bool]($account.properties.useraccountcontrol[0]-band2)}else{$exists=$false$enabled=$false} [PSCustomObject]@{Samaccountname=$_.samaccountnameExists=$existsEnabled=$enabled}} |Export-CsvOutput.csv-NoTypeInformation
it will export as csv ,just import it in excel.Please mark as helpful if you find my contribution useful or as an answer if it does answer your question. That will encourage me - and others - to take time out to help you. Thank you! Off2work
Wednesday, June 3, 2015 8:43 PM -
Hi Drcybercure,
Please post the script and the method you are using to run it.
I have tested its working just fine for me.
Please confirm you are using " -ErrorAction Stop" 2nd post of mine.
NOTE:- The output filenames in my script are UsersNotfound.csv and Usersfound.csv
Regards,
Satyajit
Please“Vote As Helpful” if you find my contribution useful or “MarkAs Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.
- Edited by Satyajit321 Thursday, June 4, 2015 9:02 AM
Thursday, June 4, 2015 8:58 AM -
i have tried this a few times with the same result. so even using -ErrorAction Stop doesn't get the not found aliases put into a file.
still getting
The operation couldn't be performed because object 'x' couldn't be found
here is the script am using. i only changed the directory location of the output.
$Users = Get-content "C:\PSscripts\_CustomScriptReports\_customscriptlist.txt"
foreach ($Username in $Users){
try
{Get-Mailbox $Username -ErrorAction Stop |select-object Alias |export-csv -append "C:\PSscripts\_CustomScriptReports\UsersFound.csv" -NoTypeInformation
}
catch
{
$temp = "$Username, not found in AD"
Out-File -append -InputObject $temp "C:\PSscripts\_CustomScriptReports\UsersNotfound.csv" -NoClobber}
}Monday, June 8, 2015 8:55 PM -
Hi,
Lets start with this first:
Don't make any changes, save it as TempTry.ps1 under PSscripts.
Run it as below on a new Exchange Shell session
[PS] C:\PSscripts>.\TempTry.ps1
x, not found in AD#TempTry.ps1
try { Get-Mailbox x -ErrorAction Stop } catch { "x, not found in AD" }
If its not working, try out the below link, test some more samples.
about_Try_Catch_Finally
https://technet.microsoft.com/en-us/library/hh847793.aspx
Regards,
Satyajit
Please “Vote As Helpful” if you find my contribution useful or “Mark As Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.
Tuesday, June 9, 2015 9:38 AM -
still no luck. still getting the red errors on screen.
PS C:\PSscripts> .\temptry.ps1
The operation couldn't be performed because object 'x' couldn't be found
Wednesday, June 10, 2015 4:46 PM -
Hi,
Powershell 2.0 on Windows7 and onwards supports try_catch.
Please try it on normal Windows PS session and post the results. Seems like there is some issue with your PS. what is the version?
Test some more samples.about_Try_Catch_Finally
https://technet.microsoft.com/en-us/library/hh847793.aspx
http://blogs.technet.com/b/heyscriptingguy/archive/2010/03/11/hey-scripting-guy-march-11-2010.aspx
TestTryCatchFinally.ps1
$ob1 = "kenobie" "Begin test" Try { "Attempting to create new object $ob1" $a = new-object $ob1 "Members of the $ob1" "New object $ob1 created" $a | Get-Member } Catch [system.exception] { "caught a system exception" } Finally { "end of script" }
Regards,
Satyajit
Please “Vote As Helpful” if you find my contribution useful or “Mark As Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.
Monday, June 15, 2015 7:20 AM -
hello all,
thanks for all your attempts to assist. i have not been able to get the get-mailbox cmd to catch and output errors to a txt file. here is the solution i came up with. just thought id share.
$Users = Get-content "C:\userlist.txt"
$users | ForEach { $exist = [bool](Get-mailbox $_ -erroraction SilentlyContinue);
if ($Exist){$_ >> "C:\_Found.txt"}
else
{$_ >> "C:\_not_found.txt"}
}hope this helps someone else
- Marked as answer by Drcybercure Tuesday, June 16, 2015 5:58 PM
Tuesday, June 16, 2015 5:58 PM -
OMG YES THANK YOU!
I have been trying all kinds of try catch if else all day to get this to work on so many mailboxes and write out the response to a file.Thank you!
Tuesday, May 22, 2018 6:30 AM -
Great Solution, it was what I looking for.
GRAZIE
Friday, September 27, 2019 2:13 PM