Error Reporting

This topic contains 5 replies, has 2 voices, and was last updated by  Stuart 3 months, 3 weeks ago.

  • Author
    Posts
  • #97259

    Stuart
    Participant

    Hi Guys,

    Have a strange problem that's got me scratching my head (I'll be going bald soon at this rate)

    For the experts here this will be very simple. Trying to pass an array into a function. Here is the mechanism of parsing:

    $LogData=
                    @{
                        DelegateName=$DelegateName
                        DelegateAccess=$DelegateAccess
                        Mailbox=$DisplayName
                        FriendlyText="Delegate Found"
                        Code="DELEGATES"
    
                     } #END Splat
    
                    #*** Send to Log Function ***
    
                    Write-Log $LogData
    

    Here is the function:

    
    Function Write-Log ([Array]$LogData,[Array]$InvocationError) {
    
        If ($LogData.Code -eq "SUCCESS") {
            Write-Host "[SUCCESS]`t $($LogData.FriendlyText) `t ObjectGuid $($LogData.GUID) `t DisplayName $($LogData.DisplayName)" -ForegroundColor Green
            "[SUCCESS]`t $($LogData.FriendlyText) `t $($LogData.GUID) `t DisplayName $($LogData.DisplayName)`n"| Out-File $Log -Append  }
    
        ElseIf ($LogData.Code -eq "DELEGATES") {
            Write-Host "[DELEGATES]`t $($LogData.DelegateName) has access to Mailbox $($LogData.Mailbox) with access level $($LogData.DelegateAccess)" -ForegroundColor Yellow
            "[DELEGATES]`t $($LogData.FriendlyText)`t`t`t $($LogData.DelegateName) has access to Mailbox $($LogData.Mailbox) with access level $($LogData.DelegateAccess)`n"| Out-File $Log -Append  }
    
        ElseIf ($LogData.Code -eq "ERROR") {
            Write-Host "[ERROR]`t`t TRY Index: $($LogData.TryIndex) Name: $($LogData.Name) TargetName: $($LogData.TargetName) Exception: $($LogData.Exception)" -BackgroundColor Red
            "[ERROR]`t`t TRY Index: $($LogData.TryIndex)`t`t $($LogData.GUID) Name: $($LogData.Name)`t TargetName: $($LogData.TargetName) Exception: $($LogData.Exception)" | Out-File $Log -Append
            "[ERROR]`t`t TRY Index: $($LogData.TryIndex)`t`t $($LogData.GUID) Name: $($LogData.Name) DN: $($LogData.DN)`t TargetName: $($LogData.TargetName) Exception:  $($LogData.Exception) Error0: $($LogData.Error0) Error1: $($LogData.Error1) Error2: $($LogData.Error2)" | Out-File $ErrorLog -Append
             $invocation | Out-File $ErrorLog -Append }
            }
    
    

    Debugging reveals that before the parsing the $logData is populated, but when it arrives at the Write-Log function its empty. I did a $logData.Get-Type() and its a hashtable type system.object as expected before being parsed.

    Here is its contents:

    Name                           Value                                                                                                                                                                                                     
    ----                           -----                                                                                                                                                                                                     
    TargetName                     ToMB                                                                                                                                                                                                      
    Error2                         Exception calling ".ctor" with "2" argument(s): "Cannot process argument because the value of argument "password" is null. Change the value of argument "password" to a non-null value."                  
    Exception                      Method invocation failed because [Deserialized.Microsoft.Exchange.Data.ByteQuantifiedSize] does not contain a method named 'ToMB'.                                                                        
    Error1                         The property cannot be processed because the property "DisplayName" already exists.                                                                                                                       
    GUID                           b4f3eca8-2b53-4feb-9759-c19358d925b5                                                                                                                                                                      
    DN                             CN=Dawn,OU=Users,OU=CompanyName,DC=corp,DC=expopssandbox3,DC=co,DC=uk                                                                                                                                     
    Name                           Dawn                                                                                                                                                                                                      
    TryIndex                       002 FN-PM                                                                                                                                                                                                 
    Code                           Error                                                                                                                                                                                                     
    Error0                         Method invocation failed because [Deserialized.Microsoft.Exchange.Data.ByteQuantifiedSize] does not contain a method named 'ToMB'. 
    

    Here is its type:

    IsPublic IsSerial Name                                     BaseType                                                                                                                                                                      
    -------- -------- ----                                     --------                                                                                                                                                                      
    True     True     Hashtable                                System.Object  
    

    THanks

    Stuart

  • #97262

    Stuart
    Participant

    OMG, being a spanner,

    Write-Log -LogData $LogData (kill me)

  • #97292

    Joel Sallow
    Participant

    Yeah, if all that was meant to go to a single parameter, you need to specify the name. OR you need to define your parameter as hashtable type AND give it a parameter position (which looks like this):

    param(
        [Parameter(Position = 0)]
        [hashtable]
        $LogData
    )
    

    Positionless parameters must be called out by name in most cases. If you specify a position you can skip having to specify the name.

    The other possibility is that each entry in the hashtable is a different parameter. In that case, your function call needs to look like this:

    Write-Log @LogData
    

    The @ instead of $ tells PowerShell to take the key/value pairs in the table and splat them to the appropriate separate parameters. 🙂

    EDIT:

    And as usual I miss something on my first read. The reason it wasn't assigned properly was that you're typing the parameter as an [array]. Arrays look like this:

    $array = @("one", "two", "three)
    $SimplerArray = "This","is","functionally","equivalent"

    Arrays don't allow you to name each value. What you're passing in is a hashtable, which looks like what you had. Different object types, for slightly different things. You can only refer to elements of an array using index numbers, but hashtables can use names:

    $array[0] = "one"
    $hashtable['name'] = "value"
    

    I think PS will also autobind things like:

    $hashtable.Name = "value"

    But it'll be much faster in terms of processing speed to just use the index notation due to how dot notation works behind the scenes. 🙂

  • #97416

    Stuart
    Participant

    Hello again,

    So I had defined the function (Write-Log) to accept a variable of type [Array] ($LogData) already and started that Hashtable with @{ which I thought makes it a hashtable? I guess I should change the Function to [HashTable] and not [Array] perhaps?
    When the function receives the hashtable it then references it with $LogData.TryIndex etc rather than using [0] or [1] but thanks for pointing that out.

    Is what I have done wrong as it seems to work ok?

    Many Thanks

    Stuart

  • #97458

    Joel Sallow
    Participant

    The function just needs to have its input typed as [hashtable] instead of [array] and it'll all be perfectly fine. 🙂

  • #97467

    Stuart
    Participant

    cool thanks 🙂

You must be logged in to reply to this topic.