PowerShell output format - List all SQL Servers - Remove blank line

Answered PowerShell output format - List all SQL Servers - Remove blank line

  • Tuesday, August 21, 2012 12:50 PM
     
      Has Code

    I’m trying to create a list of my SQL servers.

    All I want is the ServerName.

    When I run the following PowerShell, I get a list of my SQL-Servers.

    [System.Data.Sql.SqlDataSourceEnumerator]::Instance.GetDataSources() 

    Result:

    ServerName

    InstanceName

    IsClustered

    Version

    ----------

    ------------

    -----------

    -------

    Server-1

    No

    Server-2

    No

    Server-3

    No

    Server-4

    No

    If I remove the HeaderTable “|FT ServerName -HideTableHeaders”  in my query. It leaves a Blank Line, at the top.

    [System.Data.Sql.SqlDataSourceEnumerator]::Instance.GetDataSources() | FT ServerName -HideTableHeaders

    ### BLANK LINE ###

    Server-1

    Server-2

    Server-3

    Server-4

    How do I remove the Blank Line, at the top of my result.?


    Best Regards, Sebastian Davidge IT Specialist



All Replies

  • Tuesday, August 21, 2012 1:07 PM
    Moderator
     
     Answered Has Code
    [System.Data.Sql.SqlDataSourceEnumerator]::Instance.GetDataSources() |
      select -expand ServerName

    Will get you just the server names.

    If you really want FT output without the header or the blank line:

    ([System.Data.Sql.SqlDataSourceEnumerator]::Instance.GetDataSources() | FT ServerName,OtherProperty -HideTableHeaders -auto | Out-String).trim()


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

  • Tuesday, August 21, 2012 1:18 PM
    Moderator
     
     

    Hi,

    I think the confusion is that PowerShell is outputting objects, not text. As Rob (mjolinor) said, just output the ServerName property of the objects.

    Bill

  • Tuesday, August 21, 2012 4:46 PM
     
     

    Or use the easy method:

    [System.Data.Sql.SqlDataSourceEnumerator]::Instance.GetDataSources() | %{$_.servernames}


    ¯\_(ツ)_/¯