none
Delete empty lines and spaces in Powershell output file RRS feed

  • Question

  • Hi,

    the output file using PowerShell have empty lines and spaces.

    I read Trim function, but don't know how I can use it in the following script. 

    $MgmtServer = Get-SCOMManagementServer
    $Results = foreach ($MServer in $MgmtServer){ 
    Get-SCOMAgent -ManagementServer $MServer
    } 
    $Results |select -ExpandProperty Displayname  | Sort DisplayName | Format-Table DisplayName -HideTableheaders -Wrap -Auto |Set-Content C:\SCOM_Agents.txt

    Best regards

    Birdal



    • Edited by _Birdal Wednesday, October 16, 2019 7:24 AM
    Wednesday, October 16, 2019 7:24 AM

Answers

  • Hi Birdal,

    I won't recommend exporting toi a txt, but to a .csv instead:

    $Results | Export-Csv -NoTypeInformation -Path C:\SCOM_Agents.csv

    Can you please test this out, I think the ouput looks much better. You can then open it in Excel and it gets auto-formatted in to a nice table view. 

    Regards,


    (Please take a moment to "Vote as Helpful" and/or "Mark as Answer" where applicable. This helps the community, keeps the forums tidy, and recognizes useful contributions. Thanks!) Blog: https://blog.pohn.ch/ Twitter: @StoyanChalakov

    • Marked as answer by _Birdal Thursday, October 31, 2019 8:50 AM
    Thursday, October 17, 2019 2:04 PM
    Moderator
  • Hi Birdal,

    yes you can read all the agent DisplayNames from the .csv like that:

    $Computers = Import-Csv C:\SCOM_Agents.csv
    $AgentNames = $Computers.DisplayName

    All the names are then saved in the $AgentNames variable. 

    Please check if this gives you what you are looking for. I will try to give you a more elegant solution without dumpimng to csv first, tomorrow (I currently have no access to my test env. )

    Cheers,


    (Please take a moment to "Vote as Helpful" and/or "Mark as Answer" where applicable. This helps the community, keeps the forums tidy, and recognizes useful contributions. Thanks!) Blog: https://blog.pohn.ch/ Twitter: @StoyanChalakov



    Thursday, October 17, 2019 3:43 PM
    Moderator
  • Hi Birdal,

    this did the trick:

    $Computers = Import-Csv C:\Temp\SCOM_Agents.csv
    $AgentNames = $Computers.DisplayName 
    $AgentNames | Sort-Object | Set-Content C:\AgentNames.txt

    Please give me short feedback. Thanks,


    (Please take a moment to "Vote as Helpful" and/or "Mark as Answer" where applicable. This helps the community, keeps the forums tidy, and recognizes useful contributions. Thanks!) Blog: https://blog.pohn.ch/ Twitter: @StoyanChalakov

    • Marked as answer by _Birdal Friday, November 8, 2019 1:27 PM
    Friday, November 1, 2019 12:10 PM
    Moderator

All replies

  • Hi Birdal,

    I won't recommend exporting toi a txt, but to a .csv instead:

    $Results | Export-Csv -NoTypeInformation -Path C:\SCOM_Agents.csv

    Can you please test this out, I think the ouput looks much better. You can then open it in Excel and it gets auto-formatted in to a nice table view. 

    Regards,


    (Please take a moment to "Vote as Helpful" and/or "Mark as Answer" where applicable. This helps the community, keeps the forums tidy, and recognizes useful contributions. Thanks!) Blog: https://blog.pohn.ch/ Twitter: @StoyanChalakov

    • Marked as answer by _Birdal Thursday, October 31, 2019 8:50 AM
    Thursday, October 17, 2019 2:04 PM
    Moderator
  • Hi Stoyan,

    sure we can also use .csv output file.

    But my aim is that I filter the out so that only agent NetBIOS names should be listed in a text file so that I can use this text file to read these agents again to another script.

    Can I read agents using ($Computers = Get-Content "C:\SCOM_Agents.csv") ? !

    Actually I prefer to use .txt files.

    Best regards

    Birdal


    • Edited by _Birdal Thursday, October 17, 2019 2:43 PM
    Thursday, October 17, 2019 2:43 PM
  • Hi Birdal,

    yes you can read all the agent DisplayNames from the .csv like that:

    $Computers = Import-Csv C:\SCOM_Agents.csv
    $AgentNames = $Computers.DisplayName

    All the names are then saved in the $AgentNames variable. 

    Please check if this gives you what you are looking for. I will try to give you a more elegant solution without dumpimng to csv first, tomorrow (I currently have no access to my test env. )

    Cheers,


    (Please take a moment to "Vote as Helpful" and/or "Mark as Answer" where applicable. This helps the community, keeps the forums tidy, and recognizes useful contributions. Thanks!) Blog: https://blog.pohn.ch/ Twitter: @StoyanChalakov



    Thursday, October 17, 2019 3:43 PM
    Moderator
  • Hi Stoyan,

    Thank you.

    I will check it in this week and give you a feedback.

    Best regards

    Birdal

    Monday, October 21, 2019 1:42 PM
  • Hi Stoyan,

    I tested it. Yes I can import successful.

    I want to sort the list of agents related to "DisplayName", and tested this:

    $Computers = Import-Csv C:\SCOM_Agents.csv
    $AgentNames = $Computers.DisplayName | Sort-Object -Property $Computers.DisplayName | Set-Content C:\AgentNames.txt

    I tested also with SORT statement:

    Sort -Property DisplayName

    But sort does not work.  It is listed, but not sorted.

    Best regards

    Birdal

    Thursday, October 31, 2019 8:50 AM
  • Hi Birdal,

    I would be able to test a bit tomorrow and make you a suggestion. You will hear from me tomorrow.

    Regards,


    (Please take a moment to "Vote as Helpful" and/or "Mark as Answer" where applicable. This helps the community, keeps the forums tidy, and recognizes useful contributions. Thanks!) Blog: https://blog.pohn.ch/ Twitter: @StoyanChalakov

    Thursday, October 31, 2019 1:15 PM
    Moderator
  • Hi Birdal,

    this did the trick:

    $Computers = Import-Csv C:\Temp\SCOM_Agents.csv
    $AgentNames = $Computers.DisplayName 
    $AgentNames | Sort-Object | Set-Content C:\AgentNames.txt

    Please give me short feedback. Thanks,


    (Please take a moment to "Vote as Helpful" and/or "Mark as Answer" where applicable. This helps the community, keeps the forums tidy, and recognizes useful contributions. Thanks!) Blog: https://blog.pohn.ch/ Twitter: @StoyanChalakov

    • Marked as answer by _Birdal Friday, November 8, 2019 1:27 PM
    Friday, November 1, 2019 12:10 PM
    Moderator
  • Hi Stoyan,

    sorry for the late feedback. I could test your suggestion today. Yes, it worked perfectly!

    But why it is so tricky? I don't understand. OK, I am not a developer, perhaps I have no developer view :-).

    Thank you!

    Have a nice weekend.

    Birdal

    Friday, November 8, 2019 1:27 PM
  • The general tip is that any Format cmdlets should be last in the pipeline.  So always think Format-* = Format-Last.

    In your case (first script in thread) you should have replaced your format-Table cmdlet by a  Select -expandproperty DisplayName 

    Friday, November 8, 2019 2:23 PM