Implicit remoting (import-pssession) / Error Handling

This topic contains 4 replies, has 4 voices, and was last updated by Profile photo of Craig Lawrence Craig Lawrence 1 year, 4 months ago.

  • Author
    Posts
  • #11292
    Profile photo of Kenneth Gaffron
    Kenneth Gaffron
    Participant

    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/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

  • #11295
    Profile photo of Don Jones
    Don Jones
    Keymaster

    I seem to recall this being a known problem with the way the Exchange cmdlets in particular work. The cmdlet is running remotely, of course, but its exception doesn't get bubbled up through to the local proxy function used by implicit remoting. Let me se eif I can find where I saw that.

  • #11343
    Profile photo of Kenneth Gaffron
    Kenneth Gaffron
    Participant

    Thanks Don... Hope you are able to find it.
    I have read some of your books and seen a all your CBT Nuggets, except the last.

  • #11499
    Profile photo of stealthfield
    stealthfield
    Participant

    I am running into the same thing with Windows 8.1/PowerShell 4.0/2.0 and Office365.
    PowerShell 3.0 on Windows 8.0 works just fine which is upsetting.

    # Simple demo:
    try
    {
    Get-Mailbox -Identity bogusAlias -ErrorAction 'Stop'
    }
    catch
    {
    Write-Host "This should trip, but it never does! The error action is ignored."
    }

    # This works:
    try
    {
    $Global:ErrorActionPreference = 'Stop'
    Get-Mailbox -Identity bogusAlias -ErrorAction 'Stop'
    }
    catch
    {
    Write-Host "Exception caught!"
    }

  • #33199
    Profile photo of Craig Lawrence
    Craig Lawrence
    Participant

    thanks StealthField – works perfectly!

You must be logged in to reply to this topic.