header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Append a server name at the end of a text file
Last Post 16 Jun 2009 08:23 AM by tojo2000. 1 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
edm365f31User is Offline
Basic Member
Basic Member
Posts:100
Avatar

--
16 Jun 2009 07:51 AM
    I got to be missing something simple here (or do i need to use add-content here?)

    Tyring to append a server name to a new line at the end

    $server = 'tundra00'
    $path = 'c:\serverlist'

    out-file = "$path\servernames.txt" -inputobject $server -append -encoding ASCII -width 50

     note:
     if i don't use the -encoding parameter it enters the new name with spaces between them

    lexus00 t u  n d r a 0 0

    this is what happens it enters it next to the server name in the text??

    buick00
    acura01
    acura00
    zebra00
    lexus00tundra00

    I also want to re-sort the file after the append.
    Thnx



    tojo2000User is Offline
    New Member
    New Member
    Posts:45
    Avatar

    --
    16 Jun 2009 08:23 AM
    The problem is that there is no newline character at the end of the file, so it's just appending to the end of the line. There are two ways to go here. If it's just this one file that has this problem, you could just do this:

    $server = "`ntundra00"

    and then appending that to the file.

    If you just want to make sure that this doesn't happen, you can try sucking up the file into an array, adding $server to it, then outputting it to the file.

    $servers = Get-Content "$path\servernames.txt"
    $servers += $server
    $servers | out-file "$path\servernames.txt"


    The reason why you were seeing the spaces in between the letters is because your PowerShell is outputting 16-bit Unicode strings by default, so the output looks funky when injected into an 8-bit ascii-encoded file. In this case I didn't bother because I was overwriting the file anyway.

    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer