Solved

PowerShell Script to Get Remote Server Windows Update Information

Posted on 2014-10-30
3
308 Views
Last Modified: 2014-11-07
I have issues trying to pike out the servers list and then get the information ported over to a csv file. The script runs fine on an individual server but will error out with the following when trying to run it from the servers.txt file.

Error:

At C:\scripts\Get-WindowsUpdate.ps1:2 char:27
+ function Get-WindowsUpdate }
+                           ~
Missing function body in function declaration.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : MissingFunctionBody

Full Script:

get-content .\servers.txt | foreach \{
function Get-WindowsUpdate }
{
  [CmdletBinding()]
  param
  (
    [String[]]
    $ComputerName,
    $Title = '*',
    $Description = '*',
    $Operation = '*'
  )
 
  $code = {
    param
    (
      $Title,
      $Description
    )


    $Type = @{
      name='Operation'
      expression={
   
    switch($_.operation)
    {
            1 {'Installed'}
            2 {'Uninstalled'}
            3 {'Other'}
    }
 }
}
   
   
    $Session = New-Object -ComObject 'Microsoft.Update.Session'
    $Searcher = $Session.CreateUpdateSearcher()
    $historyCount = $Searcher.GetTotalHistoryCount()
    $Searcher.QueryHistory(0, $historyCount) |
    Select-Object Title, Description, Date, $Type |
    Where-Object { $_.Title -like $Title } |
    Where-Object { $_.Description -like $Description } |
    Where-Object { $_.Operation -like $Operation }
  }

  $null = $PSBoundParameters.Remove('Title')
  $null = $PSBoundParameters.Remove('Description')
  $null = $PSBoundParameters.Remove('Operation')

  Invoke-Command -ScriptBlock $code @PSBoundParameters -ArgumentList $Title, $Description
}
0
Comment
Question by:robertarenson
  • 2
3 Comments
 
LVL 28

Expert Comment

by:becraig
ID: 40414295
I might be wrong but at first glance you are mixing two things

you do the for each and define the function in it but do not actually define the function:
here you go:

get-content .\servers.txt | foreach \{
Get-WindowsUpdate
}
function Get-WindowsUpdate
{
Whatever your function is here
}

Open in new window

0
 

Author Comment

by:robertarenson
ID: 40414321
Must be doing something wrong with the script. It now just lists all the server names but does not actually run the script itself.

get-content .\servers.txt
function Get-WindowsUpdate
{
  [CmdletBinding()]
  param
  (
    [String[]]
    $ComputerName,
    $Title = '*',
    $Description = '*',
    $Operation = '*'
  )
 
  $code = {
    param
    (
      $Title,
      $Description
    )


    $Type = @{
      name='Operation'
      expression={
   
    switch($_.operation)
    {
            1 {'Installed'}
            2 {'Uninstalled'}
            3 {'Other'}
    }
 }
}
   
   
    $Session = New-Object -ComObject 'Microsoft.Update.Session'
    $Searcher = $Session.CreateUpdateSearcher()
    $historyCount = $Searcher.GetTotalHistoryCount()
    $Searcher.QueryHistory(0, $historyCount) |
    Select-Object Title, Description, Date, $Type |
    Where-Object { $_.Title -like $Title } |
    Where-Object { $_.Description -like $Description } |
    Where-Object { $_.Operation -like $Operation }
  }

  $null = $PSBoundParameters.Remove('Title')
  $null = $PSBoundParameters.Remove('Description')
  $null = $PSBoundParameters.Remove('Operation')

  Invoke-Command -ScriptBlock $code @PSBoundParameters -ArgumentList $Title, $Description
}
0
 
LVL 28

Accepted Solution

by:
becraig earned 500 total points
ID: 40414336
Ok so I am guessing I was not very clear, you are doing two things here.

1. you are defining a function that is independent of anything else you want to do as it has it's own scope so the function has to be declared by name and have any required parameters defined.

eg.

function Get-WindowsUpdate
{
	[CmdletBinding()]
	param
	(
		[String[]]
		$ComputerName,
		$Title,
		$Description,
		$Operation
	)
	
	$Type = @{
		name = 'Operation'
		expression = {
			
			switch ($_.operation)
			{
				1 { 'Installed' }
				2 { 'Uninstalled' }
				3 { 'Other' }
			}
		}
	}
}

Open in new window


2. Now you want to call your function in your for each loop and pass any required parameters in.
e.g:
get-content .\servers.txt | foreach \{
$title = '*'; $description = '*', $operation = '*'
Get-WindowsUpdate $_ $title $description $operation
}

Open in new window

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now