Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PowerShell Script to Get Remote Server Windows Update Information

Posted on 2014-10-30
3
Medium Priority
?
354 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 2000 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

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
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…
Screencast - Getting to Know the Pipeline

783 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