Solved

PowerShell Script to Get Remote Server Windows Update Information

Posted on 2014-10-30
3
328 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 29

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 29

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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
A procedure for exporting installed hotfix details of remote computers using powershell
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

734 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