Announcement

Collapse
No announcement yet.

Export accepted domains in PowerShell

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

  • Export accepted domains in PowerShell

    I'm trying to export just the DomainName of get-accepteddomain in Exchange 2010 Sp1.

    I've tried
    Code:
    Get-AcceptedDomain | Export-CSV -NoTypeInformation -Path "C:\accepted.txt" -Delimiter " "
    but it gives more information than needed. I've tried
    Code:
    Get-AcceptedDomain | FL DomainName | Export-CSV -NoTypeInformation -Path "C:\accepted.txt" -Delimiter " "
    but it just gives me a bunch of weird type IDs that I can't do anything with.

    I'm somewhat of a PowerShell rookie, so I'm going blindly into this. Ideally, the file, when all is said and done, will read like so:
    domain1.com domain2.com domain3.com etc.com

    Basically, what I'm entirely trying to accomplish, is a scheduled task that will run a PowerShell script to export the domains into a text file in the above mentioned format, that is in a share. A CentOS postfix server will copy the file from that share and put it in /etc/postfix/relay_domains so that postfix will accept the domains in the list. I have everything working except the PowerShell script. Which makes everything else useless.

    Any input would be greatly appreciated! Thanks in advance!
    Last edited by alexmetal; 20th February 2011, 08:35.

  • #2
    Re: Export accepted domains in PowerShell

    Its clearly the FL that is doing the damage -- removing it gives the correct information.
    FT has the same issue (lived in hope here...)

    However, the redirect command (remember that?) gave better results:
    get-accepteddomain|fl domainname > C:\list.txt
    You will still need to format that output to strip off "DomainName :" -- FT may be better
    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: Export accepted domains in PowerShell

      I was pulling my hair out on this one! Thank you so much! I didn't even think to use the redirect command. Haha. FT did produce better results, by the way. Thank you for that too.

      Now I just need to figure out a way to strip or comment out "DomainName" and "--------"..

      Would it be best to put the output in a variable first, then use $var.Replace("DomainName","")?

      Comment


      • #4
        Re: Export accepted domains in PowerShell

        This is almost there -- strips out the lines at the start:
        get-accepteddomain|ft domainname >c:\list.txt

        $f = ${c:\list.txt}
        $f[0]=$null
        $f[1]=$null
        $f[2]=$null

        ${c:\list.txt} = $f
        Just trying to replace the newline with a space
        Last edited by Ossian; 20th February 2011, 08:59.
        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


        • #5
          Re: Export accepted domains in PowerShell

          I just tried this:
          Code:
          Get-Content C:\list.txt | ForEach-Object {$_ -replace "DomainName", "----------"} | Set-Content C:\list.txt
          But it's erroring saying that it can't do the Set-Content because the file in use by another process, I would assume the Get-Content.

          EDIT: I'll take replacing the first three lines, that's essentially what I was trying to accomplish with the above. Thank you again! Now all I need to do is see if postfix cares about blank spaces at the top of the list.

          Comment


          • #6
            Re: Export accepted domains in PowerShell

            Try mine above (save it as a PS1 script)
            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


            • #7
              Re: Export accepted domains in PowerShell

              I just did, it worked perfectly! Thank you so very much! I've been stressing about this all day, because it seemed like it should be so simple to do.. Turns out I was making it even more complicated than it needed to be. You're a life saver!

              Comment


              • #8
                Re: Export accepted domains in PowerShell

                Did you manage to strip out the newline and make it one line of text -- I havent so far
                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


                • #9
                  Re: Export accepted domains in PowerShell

                  I haven't yet, no. Or at least not without it reading "domain.comdomain2.comdomain2.com"
                  But I think PostFix will run postmap just fine if each domain is on a new line, so it might not be a big deal. I'm trying that right now.

                  Comment


                  • #10
                    Re: Export accepted domains in PowerShell

                    Postfix doesn't seem to mind, everything looks good.
                    Thank you again for all of your help!

                    Comment


                    • #11
                      Re: Export accepted domains in PowerShell

                      Originally posted by Ossian View Post
                      Did you manage to strip out the newline and make it one line of text
                      Use a stringbuilder to catenate rows. Like so,

                      Code:
                      # Create a new StringBuilder
                      $sb = new-object text.stringbuilder
                      
                      # Get data from domain list file that has all the domains on separate rows like
                      # contoso.com
                      # adventureworks.com
                      # tailspintoys.com
                      # 
                      # Iterate contents with foreach loop and append formatted into sb
                      cat .\Domains.txt | % { [void] $sb.append("{0} " -f $_)}
                      
                      # Print data:
                      # contoso.com adventureworks.com tailspintoys.com
                      $sb.ToString()
                      So what's the deal with
                      Code:
                      [void] $sb.append("{0} " -f $_)
                      ? [void] makes the Append() method quiet, otherwise it would print status messages. Append itself appends (duh) data into existing buffer. "{0} " -f $_ is a format string. The current object on pipeline gets a space padding on its end.

                      -vP

                      Comment


                      • #12
                        Re: Export accepted domains in PowerShell

                        Excellent! Thank you for that! This will make things a lot cleaner in Postfix. I've noticed it takes a bit longer for it to postmap a relay_domains list that has each domain on a new line.

                        Comment

                        Working...
                        X