none
Possible datatype issue? RRS feed

  • Question

  • I'm running into a problem with a powershell script and I think it may be datatype related but am not sure. I would appreciate any suggestions for fixing this.

    The following line of code when run againt my Office 365 server produces the expected output, a collection of objects

    emails sent in the last 2 days that are still pending.

    get-messagetrace -StartDate (get-date).AddDays(-2) -EndDate (get-date).AddDays(1) -Status "Pending"


    However, the following lines of code:

    $PendMessages = get-messagetrace -StartDate (get-date).AddDays(-2) -EndDate (get-date).AddDays(1) -Status "Pending"
    Write-Host $PendMessages

    produce the following output:

    Microsoft.Exchange.Management.FfoReporting.MessageTrace Microsoft.Exchange.Management.FfoReporting.MessageTrace

    For reference, the object attributes of this command are:

    get-messagetrace | gm


       TypeName: Deserialized.Microsoft.Exchange.Management.FfoReporting.MessageTrace

    Name             MemberType   Definition                                                                              
    ----             ----------   ----------                                                                              
    ToString         Method       string ToString(), string ToString(string format, System.IFormatProvider formatProvider)
    PSComputerName   NoteProperty System.String PSComputerName=pod51043psh.outlook.com                                    
    RunspaceId       NoteProperty System.Guid RunspaceId=af38c0b2-665f-44b1-b00b-6d6dc7322536                             
    EndDate          Property     System.DateTime {get;set;}                                                              
    FromIP           Property     System.String {get;set;}                                                                
    Index            Property     System.Int32 {get;set;}                                                                 
    MessageId        Property     System.String {get;set;}                                                                
    MessageTraceId   Property     System.Guid {get;set;}                                                                  
    Organization     Property     System.String {get;set;}                                                                
    Received         Property     System.DateTime {get;set;}                                                              
    RecipientAddress Property     System.String {get;set;}                                                                
    SenderAddress    Property     System.String {get;set;}                                                                
    Size             Property     System.Int32 {get;set;}                                                                 
    StartDate        Property     System.DateTime {get;set;}                                                              
    Status           Property     System.String {get;set;}                                                                
    Subject          Property     System.String {get;set;}                                                                
    ToIP             Property      {get;set;}                          

    Sunday, March 2, 2014 8:30 AM

Answers

  • And what is the issue.  The CmdLet returns MessageTrace objects. When you write it to the console yu get the object class name.  hat did you expect to get?

    To get properties you need to specifiy them:

    Write-Host $PendMessages.SenderAddress


    ¯\_(ツ)_/¯

    Sunday, March 2, 2014 11:17 AM

All replies

  • And what is the issue.  The CmdLet returns MessageTrace objects. When you write it to the console yu get the object class name.  hat did you expect to get?

    To get properties you need to specifiy them:

    Write-Host $PendMessages.SenderAddress


    ¯\_(ツ)_/¯

    Sunday, March 2, 2014 11:17 AM
  • example;

    PS C:\scripts> $x=get-process spoolsv
    PS C:\scripts> write-host $x
    System.Diagnostics.Process (spoolsv)
    PS C:\scripts> Write-Output $x

    Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
    -------  ------    -----      ----- -----   ------     -- -----------
        410      21     3900       5752    71            1360 spoolsv


    ¯\_(ツ)_/¯


    • Edited by jrv Sunday, March 2, 2014 11:21 AM
    Sunday, March 2, 2014 11:20 AM
  • Thank you, sir. My issue was a misunderstanding of what write-host is supposed to do to a variable containing a group of objects. Too accustomed to PERL, I guess, where what you see in on screen is the actual output of the command regardless of its destination.

    Sunday, March 2, 2014 2:22 PM
  • Thank you, sir. My issue was a misunderstanding of what write-host is supposed to do to a variable containing a group of objects. Too accustomed to PERL, I guess, where what you see in on screen is the actual output of the command regardless of its destination.

    NO - not really.  It is necessary to study the PowerShell documentation because it is not like Perl or any other scripting system.  Guessing at things will only get you boxed into a corner.

    Get one of the many excellent books for PowerShell beginners and read it carefully AND do all of the examples until you fully understand what is being presented.

    Write-Host and Write-Output AND immediate evaluation are all very different things and play to the fundamental underlying architecture and syntactical/behavioral considerations of the developers of PowerShell.  There are about two dozen critical features that cannot be learned by accident or by asking questions in forums.  Once you acquire this information you will find that PowerShell is very easy and mostly self teaching.  It anticipates how code needs to run in most situations and provides fundamental facilities to assist in running the request.  Things like the "pipeline" which is not exactly like other pipelines although the concept does come from the old Unix pipeline.

    You could , at least, start here:http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx

    It is not as good as many of the books but is better than nothing.


    ¯\_(ツ)_/¯

    Sunday, March 2, 2014 2:39 PM