none
Get-MessageTrackingLog Recipients RRS feed

  • Question

  • Trying to output the list of results in the "Recipients" column to my datagrid. Below is my working code...

    JobScript = {
    				Param ($global:usercred,
    					$global:SenderAddress,
    					$global:RecipientAddress,
    					$global:StartDate,
    					$global:EndDate)
    				
    				$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://pse.domain.com/powershell -Credential $global:usercred -Authentication Kerberos -ErrorAction SilentlyContinue
    				Import-Module (Import-PSSession $s -AllowClobber -DisableNameChecking) -DisableNameChecking
    				
    				$servers = Get-MailboxServer | where { $_.name -like "servers*" } | select -ExpandProperty Name
    				$Table = @()
    				foreach ($item in $servers)
    				{
    					$result = Get-MessageTrackingLog -Server $item -Sender $global:SenderAddress -Recipients $global:RecipientAddress -start $global:StartDate -end $global:EndDate -ResultSize Unlimited
    					$Table += $result
    				}
    				
    				$Table
    CompletedScript = {
    				Param ($Job)
    				$results = Receive-Job -Job $Job
    				$DT = ConvertTo-DataTable -InputObject $results
    				$DT2 = $DT | select Timestamp, MessageId, TotalBytes, Directionality, ConnectorId, RecipientCount, EventID, Source, Sender, { $_.Recipients }, MessageSubject
    				
    				$global:DT3 = ConvertTo-DataTable -InputObject $DT2
    				$datagridview1.DataSource = $global:DT3

    I am using ConvertTo-DataTable in Powershell Studio so that I can have the columns sorted upon mouse click, as that is the only way to achieve this. All of the columns are populated correctly except the Recipients column. I thought by using {$_.} it would expand the list, but it appears to be treated differently here.. Here is the error I get when I run my program...

    ERROR: +                             $row.Item($prop.Name) = $prop.Value
    ERROR: +                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ERROR:     + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
    ERROR:     + FullyQualifiedErrorId : CatchFromBaseAdapterParameterizedPropertySetValue
    ERROR:
    ERROR: Exception setting "Item": "Exception calling "set_Item" with "2" argument(s): "Type of value has a mismatch with column typeCouldn't store
    ERROR: <Jane.Doe@Domain.com> in  $_.Recipients  Column.  Expected type is Object[].""
    MainForm.psf (527, 7): ERROR: At Line: 527 char: 7
    ERROR: +                             $row.Item($prop.Name) = $prop.Value
    ERROR: +                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ERROR:     + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
    ERROR:     + FullyQualifiedErrorId : CatchFromBaseAdapterParameterizedPropertySetValue
    ERROR:

    Thursday, May 30, 2019 4:15 PM

All replies

  • You code is badly broken.  The syntax is missing closures.  Fix the syntax and eliminate the arrays and the code will not produce errors.


    \_(ツ)_/

    Thursday, May 30, 2019 9:09 PM
  • It's only partial code, I didn't want to paste the entire thing to make it a bit easier, between my statement and code you should be able to see what I am getting at. However, here is the full code snippit below...

    #region Run job1
    	if ($RecipientCheck -eq "Yes" -and $SubjectCheck -eq "No")
    	{
    		$Job1 = @{
    			Name	  = 'Job1'
    			JobScript = {
    				Param ($global:usercred,
    					$global:SenderAddress,
    					$global:RecipientAddress,
    					$global:StartDate,
    					$global:EndDate)
    				
    				$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://pse.domain.com/powershell -Credential $global:usercred -Authentication Kerberos -ErrorAction SilentlyContinue
    				Import-Module (Import-PSSession $s -AllowClobber -DisableNameChecking) -DisableNameChecking
    				
    				$servers = Get-MailboxServer | where { $_.name -like "server*" } | select -ExpandProperty Name
    				$Table = @()
    				foreach ($item in $servers)
    				{
    					$result = Get-MessageTrackingLog -Server $item -Sender $global:SenderAddress -Recipients $global:RecipientAddress -start $global:StartDate -end $global:EndDate -ResultSize Unlimited
    					$Table += $result
    				}
    				
    				$Table
    				
    				for ($i = 0; $i -lt 50; $i++) { Start-Sleep -Milliseconds 100 }
    				
    			}
    			ArgumentList = ($global:usercred, $global:SenderAddress, $global:RecipientAddress, $global:StartDate, $global:EndDate)
    			CompletedScript = {
    				Param ($Job)
    				$results = Receive-Job -Job $Job
    				$DT = ConvertTo-DataTable -InputObject $results
    				$DT2 = $DT | select Timestamp, MessageId, TotalBytes, Directionality, ConnectorId, RecipientCount, EventID, Source, Sender, { $_.Recipients }, MessageSubject
    				
    				$global:DT3 = ConvertTo-DataTable -InputObject $DT2
    				$datagridview1.DataSource = $global:DT3
    				
    				$global:Job1 = "Complete"
    			}
    			UpdateScript = {
    				Param ($Job)
    				
    			}
    		}
    		Add-JobTracker @Job1
    	}
    	#endregion

    Before you respond, I am aware you may do things differently, and my code pasted above is good known working code that is currently in my program. Every field gets returned to the datagrid as expected except recipients. My question, and only question is, how can I go about expanding the "recipients" value to show in my datagrid using the code above. 

    Friday, May 31, 2019 10:47 AM
  • You cannot show a collection in a single cell.  The collection has to be turned into a string.

    $collection -join '|'

    This will convert a simple array into a string.


    \_(ツ)_/

    Friday, May 31, 2019 11:01 AM
  • I am not trying to load them to a single cell, it's a datagrid as I mentioned above, so "Recipients" is a column, each row has a single cell in that column if that is what you meant by "single cell"?
    Monday, June 3, 2019 1:40 PM
  • Here is the correct way to code a job in tracker.

    #region Run job1
    if ($RecipientCheck -eq "Yes" -and $SubjectCheck -eq "No"){
        $Job1 = @{
            Name      = 'Job1'
            JobScript = {
                Param (
                    $usercred,
                    $SenderAddress,
                    $RecipientAddress,
                    $StartDate,
                    $EndDate
                )
                
                $s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://pse.domain.com/powershell -Credential $global:usercred -Authentication Kerberos -ErrorAction SilentlyContinue
                Import-PSSession $s -AllowClobber -DisableNameChecking
                
                $servers = Get-MailboxServer | 
                    Where-Object { $_.name -like 'server*' } | 
                    Select-Object -ExpandProperty Name
                foreach ($item in $servers){
                    Get-MessageTrackingLog -Server $item -Sender $SenderAddress -Recipients $RecipientAddress -start $StartDate -end $EndDate -ResultSize Unlimited
                }
                
                $Table
            }
            ArgumentList = $global:usercred, $global:SenderAddress, $global:RecipientAddress, $global:StartDate, $global:EndDate
            CompletedScript = {
                Param ($Job)
                $props = @(
                    'Timestamp', 
                    'MessageId', 
                    'TotalBytes', 
                    'Directionality', 
                    'ConnectorId',
                    'RecipientCount',
                    'EventID',
                    'Source',
                    'Sender', 
                    @{n='Recipients';e={$_.Recipients -join '|' }},
                    'MessageSubject'
                )
                $results = Receive-Job -Job $Job | Select-Object $props
                $datagridview1.DataSource = ConvertTo-DataTable -InputObject $results
            }
            UpdateScript = {
                Param ($Job)
            }
        }
        Add-JobTracker @Job1
    }
    #endregion

    You also need to handle errors.

    Copying and guessing is not a good way to write code or learn PowerShell.  You need to understand what each line does.  Don't add "global" to everything just because it looks good.  Try to look up and learn what arguments are and what a parameter is.  Even though they may have the same name they are not the same variable.


    \_(ツ)_/

    Monday, June 3, 2019 2:03 PM