2 problems in a screipt for retreiving emails from a exchange server

Hello,

I use a script to display certain emails from my Exchange 2013 server. I’m still building on the script (and I am new at it) but I already ran into some errors. Ik hope somebody can assist me.

the command I ran is:
Get-Emails -Recipients receipient@company.com -Sender sender@company.com -Mailboxservers server001,server002 -Numberofdays -45

The code is:

Function Get-Emails {
    [CmdletBinding()]
    param(
        [Parameter (Mandatory=$True,
                    HelpMessage="Fill in the Recipients E-mail address")]
        [string[]] $Recipients,
        
        [Parameter (Mandatory=$True)]
        [String[]] $Sender,

        [String[]] $Mailboxservers,

        [string[]] $Numberofdays

    )
    BEGIN {
        $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://srbhwcas001/PowerShell/ -Authentication Kerberos
          Import-PSSession $Session -AllowClobber
    }

    PROCESS {
        foreach ($MailboxServer in $MailboxServers) {
            $output = Get-MessageTrackingLog -recipients $Recipients -sender $Sender -Server $MailboxServer -Start (get-date).AddDays($numberofdays) |select Timestamp,RecipientStatus,SourceContext,MessageSubject,Sender,Recipient 
            }
    }
   

    END {
        $output |Out-GridView
    }

}

The problems are:

  1. The numberofdays parameter does not work. I receive the error: Cannot convert argument “value”, with value: “System.String”, for “AddDays” to type “System.Double”: “Cannot convert the “System.String” value of type “System.String” to type “System.Double”.”

  2. I have two mailservers which I want to concatenated in my out-gridview as one list. The script only displays the results from the last entered server.

Hop to hear.
Albert

The problem is with the $NumberOfDays parameter declaration. Try this instead:

[int] $Numberofdays

For the second problem - you’re overwriting the contents of $Output in your foreach loop, which is why you only see the results from the last mailbox server entered. Rather than save the output in a variable, just let it out:

Function Get-Emails
{
    [CmdletBinding()]
    param
    (
        [Parameter (Mandatory=$True,
                    HelpMessage="Fill in the Recipients E-mail address")]
        [string[]] $Recipients,
        
        [Parameter (Mandatory=$True)]
        [String[]] $Sender,

        [String[]] $Mailboxservers,

        [int] $Numberofdays

    )

    BEGIN
    {
        $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://srbhwcas001/PowerShell/ -Authentication Kerberos
          Import-PSSession $Session -AllowClobber
    }

    PROCESS
    {
        foreach ($MailboxServer in $MailboxServers)
        {
            Get-MessageTrackingLog -recipients $Recipients -sender $Sender -Server $MailboxServer -Start (get-date).AddDays($numberofdays) |select Timestamp,RecipientStatus,SourceContext,MessageSubject,Sender,Recipient 
        }
    }
}

I removed the entire end block because it’s not really needed. If you really want Out-GridView in your function, you could do something like this to keep it in:

PROCESS
{
    $MailboxServers | Foreach-Object -ScriptBlock {
        Get-MessageTrackingLog -recipients $Recipients -sender $Sender -Server $_-Start (get-date).AddDays($numberofdays) | select Timestamp,RecipientStatus,SourceContext,MessageSubject,Sender,Recipient 
    } | Out-GridView
}

however, I don’t recommend this in a function. Your function should just output data and you can use Out-Gridview at the command line if you need to view the data in a GUI window.

Thanks for the quick reply Matt.

I have changed the parameter but receive the following error now:

PS C:\scripts> Get-Emails -Recipients xxx@xxx.nl -Sender xxx@xx.nl -Mailboxservers "srbhwmbx001","srbhwmbx002" -Numberofdays -45
           
Cannot convert argument "value", with value: "System.Int32[]", for "AddDays" to type "System.Double": "Cannot convert the "System.Int32[]" value of type "System.Int32[]" to type "System.Double"."
At C:\scripts\Get-Emails.ps1:23 char:13
+             $output = Get-MessageTrackingLog -recipients $Recipients  ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
 
Cannot convert argument "value", with value: "System.Int32[]", for "AddDays" to type "System.Double": "Cannot convert the "System.Int32[]" value of type "System.Int32[]" to type "System.Double"."
At C:\scripts\Get-Emails.ps1:23 char:13
+             $output = Get-MessageTrackingLog -recipients $Recipients  ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

Thanks, I will try this.

It looks like you’ve configured the parameter to accept an array of integers, rather than a single integer. Instead of this:

[int[]] $Numberofdays

Use this:

[int] $Numberofdays

I understand Matt. We are getting somewhere now.

It looks like the new process block cannot use the parameters now.

the script is:

Function Get-Emails {
    [CmdletBinding()]
    param(
        [Parameter (Mandatory=$True,
                    HelpMessage="Fill in the Recipients E-mail address")]
        [string[]] $Recipients,
        
        [Parameter (Mandatory=$True)]
        [String[]] $Sender,

        [String[]] $Mailboxservers,

        [Int] $Numberofdays

    )
    BEGIN {
        $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://srbhwcas001/PowerShell/ -Authentication Kerberos
          Import-PSSession $Session -AllowClobber
    }

   
    PROCESS {
        $MailboxServers | Foreach-Object -ScriptBlock {
         Get-MessageTrackingLog -recipients $Recipients -sender $Sender -Server $_ -Start (get-date).AddDays($numberofdays) | select Timestamp,RecipientStatus,SourceContext,MessageSubject,Sender,Recipient 
        }
    }

}

the message is:

ForEach-Object : Parameter set cannot be resolved using the specified named parameters.
At C:\scripts\Get-Emails.ps1:23 char:27
+         $MailboxServers | Foreach-Object -ScriptBlock {
+                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : MetadataError: (:) [ForEach-Object], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.ForEachObjectCommand

Sorry, that’s a typo on my part. There is no -ScriptBlock parameter for Foreach-Object - it’s -Process:

Foreach-Object -Process {}

Or you can just leave the parameter name out and pass the scriptblock:

Foreach-Object {}

Great! Thanks. The script works now.

Now only the out-gridview (is now displayed in a single column) but I will first try to sort this out myself.

thanks again.