Solved

PowerShell Script to Get Remote Server Windows Update Information

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
A brief introduction to what I consider to be the best editor for PowerShell.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

22 Experts available now in Live!

Get 1:1 Help Now