Author Posts

May 4, 2016 at 1:50 pm

Hi Guys, how can I catch $UserInfo.Manager being empty or not found?
If it is empty I would like it to tell me then exit, if it not found I would like it to go to next step of checking AD.

I'm trying to catch it if it is a SharedMailbox that has no owner, as opposed to its not a exchange object so move on to checking AD.

Function Get-Owner {

Param(
	[Parameter(Mandatory=$true)]
	[string]$object
	) #exit Param

Write-Verbose "Connecting to Exchange"
getExchangeSession
	
Try
{
	Write-Verbose "Looking for Owner on Exchange"
	$UserInfo = Get-User -Identity $Object #-erroraction 'silentlycontinue'
	$UserInfo.Manager
}
Catch[System.Management.Automation.CommandNotFoundException]
{
Write-Verbose "ManagedBy Not Found on Exchange!" -Verbose
Write-Host "CommandNotFoundException"
}
Catch{
Write-Verbose "Not Found on Exchange"
Write-Host "Other exception"
}
Finally{
if ([string]::IsNullOrEmpty($UserInfo.Manager)){
	Write-Verbose "Checking AD" -Verbose
	$ManagedBy = Get-ADGroup -Identity $Object -Properties ManagedBy 
	$ManagedBy.ManagedBy
}
}
Get-PSSession | Remove-PSSession
}

May 5, 2016 at 2:22 am

Hi Gary,

I don't have Exchange here so I can't test the code but to test a condition in PowerShell is ridiculously easy.

$UserInfo = Get-User -Identity $Object #-erroraction 'silentlycontinue'
if ($UserInfo.Manager){
# Means its not Null
Run your code
}

Your example code has two Catch statements – An error message somehow got embedded in there with one of them.

Also the Finally block is the clean up area rather than where the main code should be executed. Here's an example (again I haven't run it given I've no Exchange but it should get you going again)

I also removed Write-Host. Although Jeffrey Snover did do a presentation with Don Jones recently and said it's no longer a faux pas for scripts. I didn't hear why though so better to use Write-Output or continue your use of the Write-Verbose cmdlet.

Function Get-Owner {

Param(
	[Parameter(Mandatory=$true)]
	[string]$object
	) #exit Param

Write-Verbose "Connecting to Exchange"
#getExchangeSession
	
Try{
	Write-Verbose "Looking for Owner on Exchange"
	$UserInfo = Get-User -Identity $Object #-erroraction 'silentlycontinue'
	
    if ($UserInfo.Manager){
	    Write-Verbose "Checking AD" -Verbose
	    $ManagedBy = Get-ADGroup -Identity $Object -Properties ManagedBy 
	    $ManagedBy.ManagedBy
    }
}

Catch{
    Write-Verbose "ManagedBy Not Found on Exchange!" -Verbose
}

Finally{}

Get-PSSession | Remove-PSSession
}

May 5, 2016 at 2:04 pm

$UserInfo = Get-User -Identity $Object
if ($UserInfo.Manager){
# Means its not Null
Run your code
}

Correct me if I am wrong but wouldn't this be null if the user is not found on exchange or if the manager is not set? I'm struggling to find a way of managing these 2 things.

The error code above was just a test, i was finding mixed results so was testing it with a few different exceptions. That was my mistake, I should have cleaned that up before posting.

I'll have more time to look at this later but looks like JRV had the same problem.

https://social.technet.microsoft.com/Forums/scriptcenter/en-US/dbb95788-28ca-4327-89f2-26ca070fda63/powershell-remote-sessions-trycatch-erroractionpreference?forum=ITCG

May 5, 2016 at 10:57 pm

As previously mentioned you can use an IF statement, I just want to point out, you can use something like:

If(!$UserInfo.Manager){
Write-Error "Your Custom Error Message"
Break
}

What this will do is write an error message to the screen and then break out of the script if the variable is empty.
Having the "!" in front of the variable basically tell the if statement "If the variable is null" rather than "If the variable is not null".
Which means you don't have to encase your entire script in an if statement.

Apologies, I should have read your entire post.

You could possible try running it on a try catch block, along the lines of:


Try{
$UserInfo = Get-User -Identity $Object
$userinfo.manager
}
catch{
if ($error[0] -matches "does not exsist"){do something}else{do something else}
}

If the error messages are different between it not existing and not being set then, in theory it might work.

Hope this helps.

May 6, 2016 at 9:49 pm

Thank you Alex. I think I see where I have been going wrong. I didn't have the code block in the try block. I will do some testing now and see if they return different errors and see if this method returns the required results.

May 9, 2016 at 6:45 am

Okay, let me know how the testing goes 🙂

May 9, 2016 at 6:30 pm

Thanks, I used a few different things that you both helped me with to finally get it working.
But it checks for owner of mailbox\email, DL, aduser.. hopefully, I haven't missed anything..

It's still a mess, but I'll deal with that when I learn more.

Function Test-GetOwner {

Param([Parameter(Mandatory=$true)][string]$object)

Write-Verbose "Connecting to Active Directory" -Verbose
Import-Module ActiveDirectory

Write-Verbose "Connecting to Exchange" -Verbose
getExchangeSession

$MailboxUser = Get-Mailbox -Identity $object -ErrorAction SilentlyContinue
if($MailboxUser -eq $null){
	Write-Host "Not Found on Exchange. Continue checking AD"
		Try{$User = Get-ADUser -Identity $Object -Properties Manager}
		Catch{If($User -eq $null){
		Write-Verbose "No User Found in Active Directory, Check if Group" -Verbose
			$DLname = Get-ADGroup -Identity $object -Properties ManagedBy
			if($DLname.managedby -eq $null){
				Write-Host "Distribution Group found but no Manager"}
			else{$DLname.managedby}
		}
	Else{If($DLname -eq $null){
	Write-Host "Not a Active Directory Account or Group. Terminating Function"}
	break}
	break}
if($User.Manager -eq $null){
	Write-Host "No Manager found on Active Directory. Terminating Function"
	Write-Verbose "Remove ALL exchange sessions" -Verbose
Get-PSSession | Remove-PSSession
	break}
	else{$User.manager}	
	}

else{Write-Host "Found on Exchange"
	$ExchangeUser = Get-user -Identity $object  | Select Manager 
	if($ExchangeUser.Manager -eq $null){Write-Host "There is no owner of this Email"}
	else{$ExchangeUser.Manager}
	}
Write-Verbose "Remove ALL exchange sessions" -Verbose
Get-PSSession | Remove-PSSession
}

May 9, 2016 at 10:44 pm

Looks good. The only thing I would look at, is

Write-Host "Not Found on Exchange. Continue checking AD" 

and

Write-Host "Distribution Group found but no Manager"

I would have as write-warning (so they always appear without having to run the function with the -verbose option. That is more a preference though.

May 11, 2016 at 11:37 pm

I'll look at it. Thanks.
I don't think verbose is working the way it should anyway as it always shows which wasn't what I wanted.

May 12, 2016 at 12:15 am

When you are writing the "Write-verbose" don't include the -verbose parameter, it seems to always display it when set. (just tested it on my machine).

so just have:

write-verbose "the text in here"