locked
Function for a message tracking is not producing output RRS feed

  • Question

  • Hello,

    I would like to create a function from a script that works. Eventually this function will be incorporated into a module. The following script for a message tracking works.

    $Exchangeservers = Get-TransportServer |where-Object {$_.name -ne "app01" -and $_.name -ne "app02"}
            foreach ($Server in $Exchangeservers) 
    {
                       Get-MessageTrackingLog -Server $Server.pscomputername -Start (get-date).addhours(-48)`
                       -sender angie@domain.com
    }
    the following does not work in that there is no output that displays on the screen.
    Function Get-MessageTrace 
    {
    <#
    .SYNOPSIS
    Rerieves message Tracking Information from either Sender or/and recipient and the start
    parameter is obligatory.
    .DESCRIPTION
    This script must be run from either powershell or ISe and it needs to have the Exchange Module installed. 
    These commands will use MessageTracking on all Transport Servers and will gather by date all the messages. 
    To speed things up please filter to the left. The more information you can supply this script, the faster
    the results will be and less processing required on the server.
    #>
    
        Param(
            [DateTime]$Start= (Get-date),
            $Sender,
            $Recipients,
            $MessageID
        )
        BEGIN {}
        PROCESS  {
            
    
            $Exchangeservers = Get-TransportServer |where-Object {$_.name -ne "app01" -and $_.name -ne "app02"}
                    foreach ($Server in $Exchangeservers)   {
                           $param = @{'Server'=$server;
                                      'Start' = $Start;
                                      'sender' = $Sender;
                                      'MessageID' = $MessageID
                                     }
                           
                           Get-MessageTrackingLog -Server $Server.pscomputername @param
                                                            }
                 }
    
            END {}
    }
    I need help please.
    • Edited by Tiri2014 Friday, April 6, 2018 8:55 PM
    Friday, April 6, 2018 8:55 PM

Answers

  • Your code is "Now"

    [DateTime]$Start= (Get-date),

    To add days to a date:

    $date.AddDays(100),  This adds 100 days to the date.

    The function was written to default to "Now".  If you want a different start date then you need to supply the date you want when you call the function.  If you did write the function you should know how to call it.

    The tracking log can get any range of dates you want using Start date and End date. The two dates can be derived as an offset from Now or as a starting date and a number of days in the interval.

    Example:

    $start = [datetime]::Now.AddDays(-1)

    $start = [datetime]::Now.AddMonths(-2)

    $yesterday =  [datetime]::AddDays(-1)
    $midnight_yesterday = $yesterday,AddDays(1)

    Test these out until you understand how datetime works.


    \_(ツ)_/



    • Edited by jrv Tuesday, April 17, 2018 9:18 PM
    • Marked as answer by Tiri2014 Wednesday, April 18, 2018 3:02 PM
    Tuesday, April 17, 2018 9:17 PM

All replies

  • We do not fix or rewrite script that you have found.  Please contact the author of the scripts for help or obtain the services of a consultant.

    Please carefully review the following links to set your expectation for posting in  technical forums.

    This Forum is for Scripting Question Rather than script requests

    Script Gallery.

    Script Center

    Learn PowerShell  

    Script requests

    From a Bill Stewart summary of useful forum links:


    \_(ツ)_/

    Friday, April 6, 2018 9:20 PM
  • Hello,

    I would like to create a function from a script that works. Eventually this function will be incorporated into a module. The following script for a message tracking works.

    $Exchangeservers = Get-TransportServer |where-Object {$_.name -ne "app01" -and $_.name -ne "app02"}
            foreach ($Server in $Exchangeservers) 
    {
                       Get-MessageTrackingLog -Server $Server.pscomputername -Start (get-date).addhours(-48)`
                       -sender angie@domain.com
    }
    the following does not work in that there is no output that displays on the screen.
    Function Get-MessageTrace 
    {
    <#
    .SYNOPSIS
    Rerieves message Tracking Information from either Sender or/and recipient and the start
    parameter is obligatory.
    .DESCRIPTION
    This script must be run from either powershell or ISe and it needs to have the Exchange Module installed. 
    These commands will use MessageTracking on all Transport Servers and will gather by date all the messages. 
    To speed things up please filter to the left. The more information you can supply this script, the faster
    the results will be and less processing required on the server.
    #>
    
        Param(
            [DateTime]$Start= (Get-date),
            $Sender,
            $Recipients,
            $MessageID
        )
        BEGIN {}
        PROCESS  {
            
    
            $Exchangeservers = Get-TransportServer |where-Object {$_.name -ne "app01" -and $_.name -ne "app02"}
                    foreach ($Server in $Exchangeservers)   {
                           $param = @{'Server'=$server;
                                      'Start' = $Start;
                                      'sender' = $Sender;
                                      'MessageID' = $MessageID
                                     }
                           
                           Get-MessageTrackingLog -Server $Server.pscomputername @param
                                                            }
                 }
    
            END {}
    }
    I need help please.
    If u run the script as-is, its very obvious why it doesnt generate any output.
    Friday, April 6, 2018 10:20 PM
  • what? I wrote this script and I need help with it.
    Monday, April 9, 2018 4:19 PM
  • ok...it is obvious to you but not to me. Please explain.

    Monday, April 9, 2018 4:20 PM
  • $start day is the time "Now" and there will likely be no messages in the last second or two.


    \_(ツ)_/

    Monday, April 9, 2018 4:24 PM
  • I want the system DateTime to addDays and NOT to give me the NOW time. I can do that with Exchange PS. How can i do it in the script?

    I do not want the (Get-Date) to be a moment in time. I want to be able to choose a range of time such as adddays(-5) etc... .


    • Edited by Tiri2014 Tuesday, April 17, 2018 9:10 PM
    Tuesday, April 17, 2018 9:06 PM
  • Your code is "Now"

    [DateTime]$Start= (Get-date),

    To add days to a date:

    $date.AddDays(100),  This adds 100 days to the date.

    The function was written to default to "Now".  If you want a different start date then you need to supply the date you want when you call the function.  If you did write the function you should know how to call it.

    The tracking log can get any range of dates you want using Start date and End date. The two dates can be derived as an offset from Now or as a starting date and a number of days in the interval.

    Example:

    $start = [datetime]::Now.AddDays(-1)

    $start = [datetime]::Now.AddMonths(-2)

    $yesterday =  [datetime]::AddDays(-1)
    $midnight_yesterday = $yesterday,AddDays(1)

    Test these out until you understand how datetime works.


    \_(ツ)_/



    • Edited by jrv Tuesday, April 17, 2018 9:18 PM
    • Marked as answer by Tiri2014 Wednesday, April 18, 2018 3:02 PM
    Tuesday, April 17, 2018 9:17 PM