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

This topic contains 8 replies, has 2 voices, and was last updated by Profile photo of Albert van Boerum Albert van Boerum 1 week, 6 days ago.

  • Author
    Posts
  • #58517
    Profile photo of Albert van Boerum
    Albert van Boerum
    Participant

    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

  • #58529
    Profile photo of Matt McNabb
    Matt McNabb
    Participant

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

    [int] $Numberofdays
    
  • #58535
    Profile photo of Matt McNabb
    Matt McNabb
    Participant

    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.

  • #58538
    Profile photo of Albert van Boerum
    Albert van Boerum
    Participant

    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
    
    • #58541
      Profile photo of Matt McNabb
      Matt McNabb
      Participant

      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
      
    • #58546
      Profile photo of Albert van Boerum
      Albert van Boerum
      Participant

      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
      
  • #58549
    Profile photo of Matt McNabb
    Matt McNabb
    Participant

    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 {}
    
    • #58553
      Profile photo of Albert van Boerum
      Albert van Boerum
      Participant

      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.

You must be logged in to reply to this topic.