locked
Adding the server name to output RRS feed

  • Question

  • Hello, 

    Sorry if this seems pretty basic, but I haven't found a way to make this work.

    I'm looking for a while to add the server's name to an output and can't figure out how to do this without giving errors.

    Below is the script I am running - this displays patches installed on a server.

    clear-content Z:\Patchinfo\WindowsUpdates.csv
    $Session = New-Object -ComObject "Microsoft.Update.Session" 
    $Searcher = $Session.CreateUpdateSearcher() 
    $historyCount = $Searcher.GetTotalHistoryCount() 
    $Searcher.QueryHistory(0, $historyCount) | Select-Object Date, @{name="Operation"; expression={switch($_.operation){ 1 {"Installation"}; 2 {"Uninstallation"}; 3 {"Other"}}}}, @{name="Status"; expression={switch($_.resultcode){ 1 {"In Progress"}; 2 {"Succeeded"}; 3 {"Succeeded With Errors"}; 4 {"Failed"}; 5 {"Aborted"} }}}, Title | Export-Csv -NoType "Z:\Patchinfo\WindowsUpdates.csv"

    Is there any-way the server name can be added to the output?  It doesn't matter if the name is repeated on each line, I just need to have that information in there.

    Thx

    Thursday, January 11, 2018 4:56 PM

Answers

  • Hello,

    You can copy/paste this code:

    clear-content Z:\Patchinfo\WindowsUpdates.csv
    $Session = New-Object -ComObject "Microsoft.Update.Session" 
    $Searcher = $Session.CreateUpdateSearcher() 
    $historyCount = $Searcher.GetTotalHistoryCount() 
    $ComputerName = [system.environment]::MachineName
    $Searcher.QueryHistory(0, $historyCount) | Select-Object @{name="ComputerName"; expression={$ComputerName}}, Date, @{name="Operation"; expression={switch($_.operation){ 1 {"Installation"}; 2 {"Uninstallation"}; 3 {"Other"}}}},
     @{name="Status"; expression={switch($_.resultcode){ 1 {"In Progress"}; 2 {"Succeeded"}; 3 {"Succeeded With Errors"}; 4 {"Failed"}; 5 {"Aborted"} }}}, Title | Export-Csv -NoType "Z:\Patchinfo\WindowsUpdates.csv"


    I added that to your code:

    $ComputerName = [system.environment]::MachineName
    -> for getting the computer name
    @{name="ComputerName"; expression={$ComputerName}}

    -> to select the computer name and add a column ComputerName in the CSV


    • Proposed as answer by Romain Wager Thursday, January 11, 2018 5:47 PM
    • Edited by Romain Wager Thursday, January 11, 2018 5:49 PM
    • Marked as answer by LosingSleep Friday, January 12, 2018 12:05 AM
    Thursday, January 11, 2018 5:45 PM

All replies

  • Just add it as a new computed column


    \_(ツ)_/

    Thursday, January 11, 2018 5:24 PM
  • Like so:

    $server = $env:COMPUTERNAME $Searcher.QueryHistory(0, $historyCount) |
    Select-Object @{name="Computername"; expression={$Server}}, Date, ..., Title


    Thursday, January 11, 2018 5:37 PM
  • Hello,

    You can copy/paste this code:

    clear-content Z:\Patchinfo\WindowsUpdates.csv
    $Session = New-Object -ComObject "Microsoft.Update.Session" 
    $Searcher = $Session.CreateUpdateSearcher() 
    $historyCount = $Searcher.GetTotalHistoryCount() 
    $ComputerName = [system.environment]::MachineName
    $Searcher.QueryHistory(0, $historyCount) | Select-Object @{name="ComputerName"; expression={$ComputerName}}, Date, @{name="Operation"; expression={switch($_.operation){ 1 {"Installation"}; 2 {"Uninstallation"}; 3 {"Other"}}}},
     @{name="Status"; expression={switch($_.resultcode){ 1 {"In Progress"}; 2 {"Succeeded"}; 3 {"Succeeded With Errors"}; 4 {"Failed"}; 5 {"Aborted"} }}}, Title | Export-Csv -NoType "Z:\Patchinfo\WindowsUpdates.csv"


    I added that to your code:

    $ComputerName = [system.environment]::MachineName
    -> for getting the computer name
    @{name="ComputerName"; expression={$ComputerName}}

    -> to select the computer name and add a column ComputerName in the CSV


    • Proposed as answer by Romain Wager Thursday, January 11, 2018 5:47 PM
    • Edited by Romain Wager Thursday, January 11, 2018 5:49 PM
    • Marked as answer by LosingSleep Friday, January 12, 2018 12:05 AM
    Thursday, January 11, 2018 5:45 PM
  • Please use code tool on edit bar

    $Session = New-Object -ComObject Microsoft.Update.Session
    $Searcher = $Session.CreateUpdateSearcher()
    $historyCount = $Searcher.GetTotalHistoryCount()
    $Searcher.QueryHistory(0, $historyCount) | 
        Select-Object Date, 
                @{ n = 'Operation'; e = { 
                    switch ($_.operation) {
                            1 { 'Installation' }
                            2 { 'Uninstallation' }
                            3 { 'Other' } 
                    }
                }}, 
                @{ n = 'Status'; e = { 
                    switch ($_.resultcode) { 
                        1 { 'In Progress' }
                        2 { 'Succeeded' }
                        3 { 'Succeeded With Errors' }
                        4 { 'Failed' }
                        5 { 'Aborted' } 
                    } 
                }},
                Title,
                @{n='System';e={$env:COMPUTERNAME}}| 
        Export-Csv -NoTypeInformation Z:\Patchinfo\WindowsUpdates.csv


    \_(ツ)_/

    Thursday, January 11, 2018 5:46 PM
  • I appreciate everyone's response. Thank you very much!
    Thursday, January 11, 2018 11:56 PM