Error Handling with Implicit Remoting

Welcome Forums General PowerShell Q&A Error Handling with Implicit Remoting

This topic contains 1 reply, has 2 voices, and was last updated by

 
Keymaster
4 years, 3 months ago.

  • Author
    Posts
  • #11291

    Participant
    Points: 0
    Rank: Member

    Hi
    I have some problems getting try/Catch working with implicit remoting (import-pssession).
    i'm getting the error:

    The operation couldn't be performed because object 'domain/Afdelinger/Logistikcenter Horsens/Resource accounts/SMA' couldn't b
    e found on 'server'.
        + CategoryInfo          : NotSpecified: (:) [Get-Mailbox], ManagementObjectNotFoundException
        + FullyQualifiedErrorId : C28F0184,Microsoft.Exchange.Management.RecipientTasks.GetMailbox
        + PSComputerName        : exchagneserver

    The code I am running is:

    $RemoteSession = New-PSSession -ConnectionUri "http://exhub-v01.ao-vvs.dk/PowerShell" -ConfigurationName "Microsoft.Exchange" -Authentication Kerberos -Credential user
    Import-PSSession -Session $RemoteSession
    
    $ListOfAllDistributionGroup = Get-DistributionGroup | Select-Object -Property Name, DisplayName, PrimarySmtpAddress, ManagedBy
    foreach ($DistributionGroup in $ListOfAllDistributionGroup) {
    	$Obj = New-Object -TypeName PSObject
    	try {
        	$DistributionGroupManagedBy = $DistributionGroup | Select-Object -ExpandProperty ManagedBy | Get-Mailbox -ErrorAction 'stop'
    		$Obj | Add-Member -MemberType NoteProperty –Name "Name" -Value $DistributionGroup.Name
    		$Obj | Add-Member -MemberType NoteProperty –Name "PrimaryEmail" -Value $DistributionGroup.PrimarySmtpAddress
    		$Obj | Add-Member -MemberType NoteProperty –Name "ManagedBy" -Value $DistributionGroupManagedBy.Alias
    	} catch {
    		$Obj | Add-Member -MemberType NoteProperty –Name "Name" -Value $DistributionGroup.Name
    		$Obj | Add-Member -MemberType NoteProperty –Name "PrimaryEmail" -Value $DistributionGroup.PrimarySmtpAddress
    		$Obj | Add-Member -MemberType NoteProperty –Name "ManagedBy" -Value "Error: The User dosen't have a mailbox"
    	}
    	Write-Output $Obj
    }

    Hope anyone is able to help.

    Kenneth

  • #18828

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    This is a problem with a lot of the Exchange cmdlets, and it has to do with how they throw their errors. The error isn't being brought over the Remoting channel, unfortunately. This isn't something you can address directly; the only workaround would be to directly remote the commands instead of implicitly.

The topic ‘Error Handling with Implicit Remoting’ is closed to new replies.