ConvertTo-DataTable with Message Tracking Logs

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 4 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
Shelltastic
Posts: 65
Last visit: Mon Feb 19, 2024 11:31 am

ConvertTo-DataTable with Message Tracking Logs

Post by Shelltastic »

I am trying to use ConvertTo-DataTable for my datagridview. I need to be able to sort the columns upon mouse click, this is the reason I have gone the route of ConvertTo-DataTable. Below is my working code, assume I have all the proper functions in an effort to keep the code short and understandable.. The $DT variable does not get filled for some reason, when I debug the program I notice it is blank..

Code: Select all

$Job1 = @{
Name	  = 'Job1'
JobScript = {
	Param ($global: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-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 $SenderAddress -Recipients $RecipientAddress -start $StartDate -end $EndDate -ResultSize Unlimited
		$Table += $result
	}
				
	$Table
				
	for ($i = 0; $i -lt 50; $i++) { Start-Sleep -Milliseconds 100 }
				
}
ArgumentList = ($global:usercred, $SenderAddress, $RecipientAddress, $StartDate, $EndDate)
CompletedScript = {
	Param ($Job)
	$results = Receive-Job -Job $Job
	$results2 = $results | select TimeStamp,EventId,Source,Sender,Recipients,MessageSubject
				
	$DT = ConvertTo-DataTable -InputObject $results2
				
	Update-DataGridView -DataGridView $datagridview1 -Item $DT
}
UpdateScript = {
	Param ($Job)
				
}
}
		
Add-JobTracker @Job1


jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: ConvertTo-DataTable with Message Tracking Logs

Post by jvierra »

This will give you the cause of the error and fixes the Param statement. It is also a more readable format.

Code: Select all


$Job1 = @{
    ArgumentList = @(
        $global:usercred,
        $SenderAddress,
        $RecipientAddress,
        $StartDate,
        $EndDate
    )
    
    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-Module (Import-PSSession $s -AllowClobber -DisableNameChecking) -DisableNameChecking
        
        Get-MailboxServer | 
            Where-Object { $_.name -like 'server*' } |
            ForEach-Object{
                Get-MessageTrackingLog -Server $server.Name -Sender $SenderAddress -Recipients $RecipientAddress -start $StartDate -end $EndDate -ResultSize Unlimited
            }
    }
    
    CompletedScript = {
        Param ($Job)
        if($Job.State -eq 'Completed'){
            $dt = Receive-Job -Job $Job | 
                Select-Object TimeStamp, EventId, Source, Sender, Recipients, MessageSubject |
                ConvertTo-DataTable
            Write-Host ('Number of records ' + $dt.Count)
            $datagridview1.DataSource = $dt
        }else{
            [System.Windows.Forms.MessageBox]::Show("Job: $($Job.State)")
        }
    }
    
    UpdateScript = {
        Param ($Job)
        
    }
}

Add-JobTracker @Job1
User avatar
Shelltastic
Posts: 65
Last visit: Mon Feb 19, 2024 11:31 am

Re: ConvertTo-DataTable with Message Tracking Logs

Post by Shelltastic »

Thanks for that reply jvierra. Looks like I am missing something, below is a screenshot of the code working in debug mode...


https://1drv.ms/u/s!AgBAbImA7NSluBvOfw6HqIDDu1zT


The $results2 variable is still blank when I hover over it, although, my datagridview does still populate in my form with the message tracking logs, not sure where it's getting the data from if the $results2 variable is showing as blank in debug mode though, unless maybe it shows as blank by design?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: ConvertTo-DataTable with Message Tracking Logs

Post by jvierra »

You didn't use the code I posted. You must do it my way and you must check for errors.
User avatar
Shelltastic
Posts: 65
Last visit: Mon Feb 19, 2024 11:31 am

Re: ConvertTo-DataTable with Message Tracking Logs

Post by Shelltastic »

Fair enough. I copied and pasted your code in, I had to add in the "Name = 'Job1' line btw. So now it runs as my previous code did, appears to give me the same result. I have attached a screen shot at the below link of my program with the loaded results.

It appears that it is ignoring my select statement, if you take a look at the columns in the datagrid, there are some listed that I do not even have in the select statement, not sure where they are coming from.

https://1drv.ms/u/s!AgBAbImA7NSluBxg6eAVKGc90qGB
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: ConvertTo-DataTable with Message Tracking Logs

Post by jvierra »

"Name" is optional. The job will be named automatically by the system.

Without your code there is not way to know what is happening.

It is clear from the result that you didn't use my code but likely copied and pasted then altered it or you copied it wrong.
User avatar
Shelltastic
Posts: 65
Last visit: Mon Feb 19, 2024 11:31 am

Re: ConvertTo-DataTable with Message Tracking Logs

Post by Shelltastic »

When I ran it without the "Name=Job1" line, it would continuously prompt me for the name, and if I left it blank, it would error out, so no, it was not optional. Here is the code I am using, line for line what you pasted above except added in "Name=Job1"

Code: Select all

$Job1 = @{
ArgumentList = @(
	$global:usercred,
	$SenderAddress,
	$RecipientAddress,
	$StartDate,
	$EndDate
)
Name = 'Job1'
JobScript    = {
	Param (
		$global: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-Module (Import-PSSession $s -AllowClobber -DisableNameChecking) -DisableNameChecking
				
	Get-MailboxServer | Where-Object { $_.name -like 'servers*' } |
    ForEach-Object{
	Get-MessageTrackingLog -Server $server.Name -Sender $SenderAddress -Recipients $RecipientAddress -start $StartDate -end $EndDate -ResultSize Unlimited
	}
}
CompletedScript = {
	Param ($Job)
	if ($Job.State -eq 'Completed')
	{
		$dt = Receive-Job -Job $Job |
		Select-Object TimeStamp, EventId, Source, Sender, Recipients, MessageSubject | ConvertTo-DataTable
		Write-Host ('Number of records ' + $dt.Count)
		$datagridview1.DataSource = $dt
	}
	else
	{
		[System.Windows.Forms.MessageBox]::Show("Job: $($Job.State)")
	}
}
			
UpdateScript = {
	Param ($Job)
				
}
}
		
Add-JobTracker @Job1
User avatar
Shelltastic
Posts: 65
Last visit: Mon Feb 19, 2024 11:31 am

Re: ConvertTo-DataTable with Message Tracking Logs

Post by Shelltastic »

Disregard my previous post. I had an additional block code firing off in an "If" statement that was impacting this. My original code would have worked before I even posted here. Your solution does work though jvierra, thanks again.
This topic is 4 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked