none
Powershell ISE and Powershell return different results from Exchange 2013 RRS feed

  • Question

  • Not sure if this is an Exchange issue, seems to be PS. I run a script weekly that looks at the Message Tracking logs for emails sent to specific email address from our Exchange 2007 and 2013 servers.

    It does this from a remote server specifically set-up for scripting and runs in 2 parts, 1) loads the Exchange Management tools and queries 2007 2) remote PS connection to 2013 and queries 2013. For some reason, we get NO results from 2013.

    So, I load the script into ISE and run it. No 2013 results. However, if I use the exact code for the remote PS sessions in Powershell cmd window (Not through ISE) I get the three results I expect.

    Is there a difference in the way ISE and cmd run on remote PS sessions? Why would one server, using the same commands as the same user remoting to the same server get different results? Please help, I feel like I'm losing my mind.

    Scripting Server. Windows 2008R2; PSVersion 4.0; PSRemoting Protocol 2.2

    EMail server. Windows 2012R2; PSVersion 4.0; PSRemoting Protocol 2.2

    Monday, July 13, 2015 2:06 PM

Answers

  • ISE. Nil results

    Powershell command line. 1000s results.

    What exact commands did you use from the time you opened the ISE until you got NIL results.  You should only have three lines.


    \_(ツ)_/

    • Marked as answer by NewbieNik73 Thursday, July 16, 2015 11:53 AM
    Wednesday, July 15, 2015 3:19 PM

All replies

  • You may be missing updates to exchange tools.  Post in Exchange 20 forum for the latest updates to your Exhange tools version.


    \_(ツ)_/

    Monday, July 13, 2015 2:49 PM
  • Can't be that as the commands are all run from the same machine, remoting into the same machine.

    Basically, I open the script in Notepad. I open Powershell and Powershell_ISE. I copy the script block from notepad and paste it command by command into Powershell, I get the expected results. I Paste it into the command window in ISE, it returns nothing with no errors.

    If I schedule the script to run, it returns nothing. I assume the scheduled script doesn't run in ISE?

    The concern is why the same commands, run by the same user on the same machine, remoting to the same server using the same credentials is returning different results.

    • Edited by NewbieNik73 Tuesday, July 14, 2015 9:21 AM exlanation
    Tuesday, July 14, 2015 9:16 AM
  • The scheduled task is a slightly different question, that may be down to accout permissions (ie. can operate as batch job).

    The simplest thing to check on is to check that you're not doing something inadvertently in your standard Shell session that is making things work that is being ommitted in the ISE/Scheduled task sections.

    Are you running the basic PowerShell shell, do you have any modules or profile settings, are you sure you didn't inadvertently open one of the sessions as an Admin and so on. Somehow, somewhere, there is an assumption that is wrong. Find that assumption and you'll fix the problem.


    Tuesday, July 14, 2015 9:44 AM
  • Sorry Alex, I've gone through everything I possibly can and I'm not convinced it's anything I'm doing wrong.

    Here is the score. The server I am running the commands on is a script server, the user I am using has admin rights to the server and is allowed to run batch jobs (It runs 15 to 20 an hour, some to exchange, some to AD plus others) Running the tasks doesn't error, they all complete successfully.

    Both PS command and ISE are being opened from the start menu with default configs. I have not amended the profiles in any way. Neither are being run as admin.

    I set two variables, a start date and end date.

    I connect to a remote PS session on the primary mail server using a specified account with correct RBAC controls to the Exchange system and an encrypted password file (That the user I'm using created and has sole access to. Using other users to run this command fails at this step, as designed. I have also tried manually getting credentials, but get the same issue)

    I run a Get-messsagetrackinglog command using the dates specified earlier. PS command returns multiple items, ISE returns a $NULL

    I have tried the same command without start and end date (ie get ALL logs) and ISE returns $NULL whilst PS command line returns 1000s of results.

    The ONLY difference is the Session ID and Session names when I run get-pssession on each.

    Tuesday, July 14, 2015 9:57 AM
  • One thing to check:

    [System.Threading.Thread]::CurrentThread.ApartmentState

    Also be sure exchange is patched with current update.


    \_(ツ)_/

    Tuesday, July 14, 2015 11:36 AM
  • Drat, thought you had it jrv, but both are set to STA.

    Exchange is fully up to date as of last Tuesday.

    My fingers hurt from scratching my head.

    /......

    However.

    Would this report on the Apartmentstate of the server I'm running the commands on or the Exchange server I'm PSSessioning into?

    • Edited by NewbieNik73 Tuesday, July 14, 2015 2:35 PM question update
    Tuesday, July 14, 2015 2:34 PM
  • When accessing Exchange remotely how are you specifying the connection?

    $session=New-PsSession ...
    Import-Session $session

    ???


    \_(ツ)_/

    Tuesday, July 14, 2015 2:37 PM
  • forget that..STA is on PS command, ISE on Exchange server.

    Still stuck.


    Tuesday, July 14, 2015 2:37 PM
  • $cred = Get-Credential
    $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionURI "http://mailserver/PowerShell" -Authentication Kerberos -Credential $cred
    Import-PSSession $Session

    Wednesday, July 15, 2015 1:43 PM
  • What about the failing command?  If you just type that in after the import what happens and what is the exact command?


    \_(ツ)_/

    Wednesday, July 15, 2015 1:53 PM
  • OK, so here's the bulk of my script:-

    ##################################

    # Load 2007 PS tools
    add-pssnapin microsoft.exchange.management.powershell.admin

    # Set Script parameters
    $end = (get-date).ToString("dd/MM/yyyy HH:mm:ss")
    $start = (get-date).AddDays(-7).ToString("dd/MM/yyyy HH:mm:ss")
    $2013end = (get-date).ToString("MM/dd/yyyy HH:mm:ss")
    $2013start = (get-date).AddDays(-7).ToString("MM/dd/yyyy HH:mm:ss")
    $results =@()
    $table = @()
    $recipients = @()
    $2007servers = "EX01","EX02","EX03"
    $2013Servers = "MB01","MB02","MB03","MB04"

    # Query Exchange 2007 logs direct from this server
    foreach ($server in $servers)
        {
        $logs = get-messagetrackinglog -Server $server -recipients someemail@somedomain.org -EventID SEND -Start $start -End $end
        foreach ($log in $logs)
            {
            $time = $log.timestamp
            $sender = $log.sender
            $subject = $log.messagesubject
            $recipient = $log.recipients -join ', '
            $Item = New-Object PSObject
            $item | add-member Noteproperty TimeStamp $time
            $item | add-member Noteproperty Recipient $recipient
            $item | add-member Noteproperty Sender $sender
            $item | add-member Noteproperty Subject $subject
            $table += $item
            }
        }

    # Set up remote connection to Exchange 2013
    $username = "Script User"
    $password = gc "\\folder location\EncryptedCred.txt" | ConvertTo-SecureString
    $cred = new-object System.Management.Automation.PSCredential($username, $password)
    $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionURI "http://mailserver/PowerShell" -Authentication Kerberos -Credential $cred
    Import-PSSession $Session

    # Use Remote session to query Exchange 2013 logs
    foreach ($Server in $2013Servers)
        {
        $logs = get-messagetrackinglog -Server $server -recipients someemail@somedomain.org -EventID SEND -Start $2013start -End $2013end
        foreach ($log in $logs)
            {
            $time = $log.timestamp
            $sender = $log.sender
            $subject = $log.messagesubject
            $recipient = $log.recipients -join ', '
            $Item = New-Object PSObject
            $item | add-member Noteproperty TimeStamp $time
            $item | add-member Noteproperty Recipient $recipient
            $item | add-member Noteproperty Sender $sender
            $item | add-member Noteproperty Subject $subject
            $table += $item
            }
        }

    # Sort results and save as CSV file
    $table| select -unique timestamp, recipient, sender, subject | sort Timestamp | Export-Csv "\\ResultsFolder\Report.csv"

    # Disconnect Remote PS Session
    get-pssession | remove-pssession

    ##############################################

    Basically the 2013 part returns no results. I have amended this to use US and GB date formats.

    I then run it all line by line and exclude the 2007 stuff. First I run the Parameters section in ISE, then the Exchange 2013 section (I have also amended this to a manual input of credential as mentioned in earlier post) then the output. In ISE I get no results, but also no errors.

    If I copy those sections and paste them into the powershell command line on the same server, as the same user then the output "$table| select -unique timestamp, recipient, sender, subject | sort Timestamp" has hundreds of entries.

    I have excluded the PSSession disconnection part too and run further commands including PSversion checks and get the same, COmmand returns results, ISE returns nothing.

    Edit # I do get all the 2007 results OK. Thats's why in the tests, I exclude the 2007 portions of the script.#


    • Edited by NewbieNik73 Wednesday, July 15, 2015 2:37 PM
    Wednesday, July 15, 2015 2:28 PM
  • Don't want the whole script.  What happens when you just run this at a prompt;

    get-messagetrackinglog -Server $server -recipients someemail@somedomain.org -EventID SEND

    Set $server to the server at issue.


    \_(ツ)_/

    Wednesday, July 15, 2015 3:13 PM
  • ISE. Nil results

    Powershell command line. 1000s results.

    Wednesday, July 15, 2015 3:17 PM
  • Be aware that the start and end dates are NOT strings.  You are sending strings.  Remove that code and send dates.

    To get full day date we would doo this:

    $end=[datetime]::Today #points to today at midnight 00:00
    $start=$end.AddDays(-7)  # last seven days

    Do not convert to strings or strange things can happen.


    \_(ツ)_/

    Wednesday, July 15, 2015 3:17 PM
  • ISE. Nil results

    Powershell command line. 1000s results.

    What exact commands did you use from the time you opened the ISE until you got NIL results.  You should only have three lines.


    \_(ツ)_/

    • Marked as answer by NewbieNik73 Thursday, July 16, 2015 11:53 AM
    Wednesday, July 15, 2015 3:19 PM
  • jrv,

    You are awesome. Although you didn't get the answer you did push me in the right direction. Basically, if you load the 2007 snapin then you cannot query 2013 correctly as it cannot read the log headers in the right order (Recipients come back as the return codes...hence blank result)

    SO, I had to change the order to query 2013 first, then 2007 but also had to disconnect the remote PSsession before interrogating 2007 else we get the reverse effect.

    The problem is, if you have loaded the module into a session, it will be active whilst that session runs, ruining any results from then on.

    I thank you all for your patience and advice. Thumbs up!

    Nik

    Also, thanks for the date tip. Will adopt that from now on.
    • Edited by NewbieNik73 Thursday, July 16, 2015 11:54 AM
    Thursday, July 16, 2015 11:53 AM
  • I hoped that by having you simplify it would lead in the right direction as I had no idea what was in your scripts.

    Always reduce things to the simplest set of lines that will create the problem.  In most cases this will lead to the cause.


    \_(ツ)_/


    • Edited by jrv Thursday, July 16, 2015 1:22 PM
    Thursday, July 16, 2015 1:22 PM