none
How to add "Get-content" in this script RRS feed

  • Question

  • Hi all

    I am new in PowerShell. I have this script and it works fine but I want to use the script on remote machines. 
    For that I want to add "get-content" to the script. But I do not know how to do. 
    For example : $ computers = get-content C:\scriptgenius\computerslist.txt or Get-Content C:\temp\serverlist.txt
    Finally you see what I want to do.
    Please help :)

    The script is below :

    $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,  Description | Export-Csv -NoType "$Env:systemdrive\Desktop\Windows Updates1.csv"
    $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,  Description | Export-Csv -NoType "$Env:systemdrive\Temp\$env:computername$(get-date -f ddMMyyyy)Windows Updates1.csv"



    • Edited by proadama Saturday, December 1, 2018 9:38 PM
    Saturday, December 1, 2018 9:38 PM

Answers


  • The answer has been given above.  You need to use the remotely connected COM object or you need to use PowerShell remoting.

    Please understand that this forum is not here to provide custom scripts on request.  You must be a trained Windows tech and have learned basic PowerShell.  If you are not a tech then you can contact a consultant.

    The Gallery has a number of scripts that do what you ask and the PowerShellGalleryhas modules that can do what you ask. The links have been posted above.  Start by doing the tutorial on PowerShell which will help you to understand the posted answers.

    Here is one of the better modules.


    Find-Module pswindowsupdate
    Install-Module pswindowsupdate


    \_(ツ)_/

    • Marked as answer by proadama Monday, December 3, 2018 8:37 PM
    Sunday, December 2, 2018 7:55 PM

All replies

  • First you must post code that is correctly formatted and use the code posting tool provided.

    Note that this forum does not customize scripts you have found.  YOu must be someone writing the script or have new user questions about scripting.

    To learn how to use any CmdLet just read the help for that command.

    help Get-Content -online

    To learn the basics of PowerShell start here:

    1. Microsoft Virtual Academy - Getting Started with Microsoft PowerShell
    2. PowerShell Documentation
    3. PowerShell Style Guidelines


    \_(ツ)_/


    • Edited by jrv Saturday, December 1, 2018 9:59 PM
    Saturday, December 1, 2018 9:58 PM
  • Here is  the correct way to format and post code in this forum.  I have also modified the script so that it is less business and more function.

    $csvfile = '{0}\Temp\{1}-{2:yyyyMMdd}-WindowsUpdates1.csv' -f $Env:systemdrive, $env:COMPUTERNAME, [datetime]::Now
    $operation = 'Installation', 'Uninstallation', 'Other'
    $status = 'In Progress', 'Succeeded', 'Succeeded With Errors', 'Failed', 'Aborted'
    $properties = @(
    	'Date',
    	@{ n = 'Operation'; e = { $operation[$_.operation] } },
    	@{ n = 'Status'; e = { $status[$_.resultcode] } },
    	'Title',
    	'Description'
    )
    
    $Session = New-Object -ComObject Microsoft.Update.Session
    $Searcher = $Session.CreateUpdateSearcher()
    $historyCount = $Searcher.GetTotalHistoryCount()
    $Searcher.QueryHistory(0, $historyCount) |
    	Select-Object $properties |
    	Export-Csv $csvfile -NoType

    Not that this script cannot be run remotely.  You will have to use PS remoting (WinRM).

    In the Gallery are scripts that an do what you ask. Script Gallery.


    \_(ツ)_/

    Saturday, December 1, 2018 10:15 PM
  • Even if you did get your list of computers and put them in a foreach loop your current version of the script is only accessing the update session of the local computer. You will need to either use remoting via Invoke-Command (but it requires enabling remoting in advance on desired machines if not enabled already) or if enabling remoting isn't feasible then you'll need to replace this:

    $Session = New-Object -ComObject "Microsoft.Update.Session"

    with this:

    $Session = [activator]::CreateInstance([type]::GetTypeFromProgID("Microsoft.Update.Session",$Computername))

    Where $Computername is the name of the computer for the current iteration of the foreach loop you'll need to set up. Either way you should keep in mind that Microsoft.Update.Session is very limited when used on remote computers. I think your history query is the only thing allowed to be done remotely.

    There is more that needs fixing in this script depending on which approach you take but I think you'll manage it.

    Sunday, December 2, 2018 5:27 AM
  • Hi all

    Thank to people who reply me. I am just surprise that I cannot run this script remotely.

    Is there anything to add get-content in this script or just run it remotely against many computers ?


    Adama


    • Edited by proadama Sunday, December 2, 2018 7:40 PM
    Sunday, December 2, 2018 7:40 PM

  • The answer has been given above.  You need to use the remotely connected COM object or you need to use PowerShell remoting.

    Please understand that this forum is not here to provide custom scripts on request.  You must be a trained Windows tech and have learned basic PowerShell.  If you are not a tech then you can contact a consultant.

    The Gallery has a number of scripts that do what you ask and the PowerShellGalleryhas modules that can do what you ask. The links have been posted above.  Start by doing the tutorial on PowerShell which will help you to understand the posted answers.

    Here is one of the better modules.


    Find-Module pswindowsupdate
    Install-Module pswindowsupdate


    \_(ツ)_/

    • Marked as answer by proadama Monday, December 3, 2018 8:37 PM
    Sunday, December 2, 2018 7:55 PM