Solved

PowerShell Script to Get Remote Server Windows Update Information

Posted on 2014-10-30
3
320 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 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 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