get-mailbox error handling

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.

May 28th, 2015 2:36pm

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

}

Free Windows Admin Tool Kit Click here and download it now
May 28th, 2015 4:30pm

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

May 28th, 2015 5:17pm

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,

Free Windows Admin Tool Kit Click here and download it now
May 29th, 2015 5:01am

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 ?

June 1st, 2015 10:57am

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,

Free Windows Admin Tool Kit Click here and download it now
June 3rd, 2015 5:30am

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"

	}
June 3rd, 2015 6:28am

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

	}
 
}

Free Windows Admin Tool Kit Click here and download it now
June 3rd, 2015 7:19am

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?

June 3rd, 2015 2:11pm

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.
Free Windows Admin Tool Kit Click here and download it now
June 3rd, 2015 4:46pm

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 Usersfou

June 4th, 2015 8:58am

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

 }
 
}

Free Windows Admin Tool Kit Click here and download it now
June 8th, 2015 4:56pm

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

June 9th, 2015 5:40am

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

Free Windows Admin Tool Kit Click here and download it now
June 10th, 2015 12:50pm

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"
 }

June 15th, 2015 7:20am

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 13 hours 18 minutes ago
Free Windows Admin Tool Kit Click here and download it now
June 17th, 2015 2:17pm

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 13 hours 18 minutes ago
June 17th, 2015 2:17pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics