locked
Powershell code to print/break lines after each value read from text file RRS feed

  • Question

  • I am trying to create a script that can get all the servers patches installations in our environment. The script I am running is giving me continuous output without printing any success messages after each server read from text file. I have fairly written a basic script! doh.

    I wanted some code to be inserted in my script to either print each server details with a break or Success message before printing another server continuously in CLI. Or to print each server in each separate text file. Please find the below Code:

    $Computers = gc ServerListFile.txt

    Get-hotfix -computer $Computers

    Please Give me some input at least, to try and get it done.

    Saturday, February 8, 2014 10:32 AM

Answers

  • Got the Script... Credit to Vasili Syrakis @ Stackoverflow

    $computers = gc ServerListFile.txt
    ForEach ($computer in $computers) {
    
        # You could even put the computer name at the beginning of the hotfixes
        "Hotfixes for $($computer)" | Out-File hotfixes.log -a -en ASCII
    
        # Get the hotfixes and output to text file
        Get-Hotfix -computer $computer | Out-File hotfixes.log -a -en ASCII
    
        # Add a new line after each computer's hotfixes
        "`n" | Out-File hotfixes.log -a -en ASCII
    }

    • Marked as answer by chaladi Saturday, February 8, 2014 11:01 AM
    Saturday, February 8, 2014 10:59 AM

All replies

  • Got the Script... Credit to Vasili Syrakis @ Stackoverflow

    $computers = gc ServerListFile.txt
    ForEach ($computer in $computers) {
    
        # You could even put the computer name at the beginning of the hotfixes
        "Hotfixes for $($computer)" | Out-File hotfixes.log -a -en ASCII
    
        # Get the hotfixes and output to text file
        Get-Hotfix -computer $computer | Out-File hotfixes.log -a -en ASCII
    
        # Add a new line after each computer's hotfixes
        "`n" | Out-File hotfixes.log -a -en ASCII
    }

    • Marked as answer by chaladi Saturday, February 8, 2014 11:01 AM
    Saturday, February 8, 2014 10:59 AM
  • Got the Script... Credit to Vasili Syrakis @ Stackoverflow

    $computers = gc ServerListFile.txt
    ForEach ($computer in $computers) {
    
        # You could even put the computer name at the beginning of the hotfixes
        "Hotfixes for $($computer)" | Out-File hotfixes.log -a -en ASCII
    
        # Get the hotfixes and output to text file
        Get-Hotfix -computer $computer | Out-File hotfixes.log -a -en ASCII
    
        # Add a new line after each computer's hotfixes
        "`n" | Out-File hotfixes.log -a -en ASCII
    }

    Saturday, February 8, 2014 11:01 AM
  • If you really want to use PowerShell then you need to learn to use PowerShell to format output.  This places a group header on each set of results based on the GroupBy choices.

    This is how PowerShell does what you are trying to do:

    Get-Hotfix -computer $computers |
         Format-Table -GroupBy CSName -Property hotfixid, description, InstalledOn

    It takes only the one line.  Use help to look up the CmdLets and see how they are designed to solve problems like this.  Once you learn the basics of PowerShell you will find that problems like this are very easy to solve.


    ¯\_(ツ)_/¯


    • Edited by jrv Saturday, February 8, 2014 2:32 PM
    Saturday, February 8, 2014 2:30 PM
  • To en-file the above

    Get-Hotfix -computer $computers |
         Format-Table -GroupBy CSName -Property hotfixid, description, InstalledOn |
         Out-String | Out-File <fomefile>

    It still only takes one line.


    ¯\_(ツ)_/¯

    Saturday, February 8, 2014 2:31 PM