Announcement

Collapse
No announcement yet.

Need to modify Exchange Shell Command code to add a sort property

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need to modify Exchange Shell Command code to add a sort property

    Hi Everyone,

    I have this code below.

    Code:
    Get-Mailbox | select name , alias ,EmailAddresses | foreach {
    "Name: "+$_.name
    "Alias: "+$_.alias
    
    $_.EmailAddresses | foreach {
    if($_.SmtpAddress){
    if($_.IsPrimaryAddress){
    "Primary SmtpAddress: $($_.SmtpAddress)"
    } else {
    "SmtpAddress: $($_.SmtpAddress)"
    }
    }
    }
    
    write-output ""
    } > c:\smtp.txt
    I input the code into the Exchange 2007 shell command and it will extract all primary's & secondary's email addresses and output as a CSV file.

    This works great BUT the only thing I would like to add a sort property.

    So when it output into a CSV file the users names will be in alphabetical order.

    Is this possible?

  • #2
    Re: Need to modify Exchange Shell Command code to add a sort property

    Writing your own sort routines is a b***er (voice of experience)
    Since it is a CSV you can easily put it into Excel and sort it there (even a macro to load the CSV, sort and resave if you are getting fancy)
    Tom Jones
    MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
    PhD, MSc, FIAP, MIITT
    IT Trainer / Consultant
    Ossian Ltd
    Scotland

    ** Remember to give credit where credit is due and leave reputation points where appropriate **

    Comment


    • #3
      Re: Need to modify Exchange Shell Command code to add a sort property

      Originally posted by Shazam View Post
      So when it output into a CSV file the users names will be in alphabetical order.
      There are many ways to achieve your target. The simplest one is to take a look at sort-object commandlet. The Scripting Guys have some help too.

      -vP

      Comment


      • #4
        Re: Need to modify Exchange Shell Command code to add a sort property

        Originally posted by Ossian View Post
        Writing your own sort routines is a b***er (voice of experience)
        Huh? All you need is to use a custom type, implement System.IComparable and use Array.Sort(). In Powershell 2.0, it just works - if you happen to know C# and .Net anyway.

        Code:
        function main {
        
            add-type @'    
            public class Message : System.IComparable {
            
                public string subject, message;
                
                public Message(string s, string m) {
                    subject = s;
                    message = m;
                }
                
                public int CompareTo(System.Object o) {
                    
                    if( !(o is Message)) {
                        throw new System.ApplicationException(o + " is not a Message, so can't compare");
                    }
                    return subject.CompareTo( ((Message)o).subject );
                }
            }
            
        '@
        
            createMessages
        }
        
        function createMessages {
            $messages = @()
            $messages += new-object Message("subject 1", "content 1")
            $messages += new-object Message("subject 4", "content 4")
            $messages += new-object Message("subject 3", "subject 3")
            $messages += new-object Message("subject 2", "subject 2")
            
            $messages | % {$_}
            write-host
            [array]::Sort($messages)
            $messages | % {$_}
        }
        
        . main
        -vP

        Comment


        • #5
          Re: Need to modify Exchange Shell Command code to add a sort property

          Hi guys,


          It's not easy to sort it in Excel. Come to think I don't think you can do it.

          I left 2 attachments to give you more an visual way what I'm trying to achieve.

          The Results.txt file is after I run the code in the Exchange 2007 command shell.

          The Desire Results.txt file is how I would like the code to perform.

          Is this Possible?
          Attached Files

          Comment


          • #6
            Re: Need to modify Exchange Shell Command code to add a sort property

            Originally posted by Shazam View Post
            It's not easy to sort it in Excel. Come to think I don't think you can do it.
            Sorting would be easy if you got data arranged into columns, but in this case you got more like blocks or records. I suppose you could transpose the data in Excel and sort it. My solution would still be a custom IComparable class that is trivial to sort.

            Here is a quick draft. Not tested, though.

            Code:
            function main {
                add-type @'    
                public class ExchangeDetails : System.IComparable {
                
                    public string Name, Alias, EmailAddresses;
                    public ExchangeDetails(string name, string alias, string emailAddresses) {
                        Name = name;
                        Alias = alias;
                        EmailAddresses = emailAddresses;
                    }
                    
                    public int CompareTo(System.Object o) {
                        if( !(o is ExchangeDetails)) {
                            throw new System.ApplicationException(o + " is not an ExchangeDetails object, so can't compare");
                        }
                        return Name.CompareTo( ((ExchangeDetails)o).Name );
                    }
                    
                    public override string ToString() {
                        return string.Format("Name: {0}{1}Alias: {2}{1}{3}", Name, System.Environment.NewLine, EmailAddresses);
                    }
                }
            '@
            
                listMailboxes
            }
            
            function listMailboxes {
            
                $boxes = @()
                
                Get-Mailbox | select name, alias, EmailAddresses | % {
                
                    $name = "Name: " + $_.Name
                    $alias = "Alias: " + $_.Alias
                    $primary = [string]::Empty
                    $smtp = [string]::Empty
                    
                    $_.EmailAddresses | % {
                        if($_.SmtpAddresses) {
                            if($_.IsPrimaryAddress) {
                                $primary = "Primary Smtp address: " + $_.SmtpAddress
                            } else {
                                $smtp += "Smtp address: " + $_.SmtpAddress + [environment]::NewLine
                            }
                        }
                    }
                    $boxes += new-object($name, $address, ($primary + " " + $smtp) )
                
                }
                
                [array]::Sort($boxes)
                
                $boxes | % {
                    add-content -path myfile.log $_.ToString()
                }
            }
            
            . main
            -vP

            Comment


            • #7
              Re: Need to modify Exchange Shell Command code to add a sort property

              Hi vonPryz,


              Thank you for the reply. Pardon me I don't know about programming that much.

              Do I run your code in the Exchange 2007 command shell? Or do I run this in the Powershell 2.0? If so, is there link for instructions using Powershell 2.0?


              Thanks,

              Devin C.

              Comment


              • #8
                Re: Need to modify Exchange Shell Command code to add a sort property

                Originally posted by Shazam View Post
                Do I run your code in the Exchange 2007 command shell? Or do I run this in the Powershell 2.0? If so, is there link for instructions using Powershell 2.0?
                I haven't got access to Exchange 2k7 at the moment, so I got to guess. Launch Exchange shell and type get-host. This will output info about the Powershell version and stuff. If the version is 2.0, you are good to go. Othervise, you need to launch Powershell 2 and import Exchange commandlets.

                If your Exchange shell is version 2, launch notepad and save the example code in some directory as, say, List-Mailboxes.ps1. Then launch the shell and type ./List-Mailboxes.ps1 in the directory containing the script file. Mind you, Powershell doesn't include current direcotry in path, so you must work the Unix way and use ./ (or .\) to specify the current dir. If you got an error about script security, try running
                Code:
                set-executionpolicy remotesigned
                and try again.

                -vP

                Comment

                Working...
                X