Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Script that works similar to WSUS to count updates for domain

$
0
0

I have a script that works similar to WSUS that counts total Windows update installs. It allows a date range input, references a file that has a list of IPs, and outputs to a file that I specify location. Unreachable systems are annotated as such. I don't know where it came from as I took over an admin spot. Is there an easier or more accurate way to do this? I really just need an accurate total update count for that domain range with a specified date.

$ErrorActionPreference = "silentlycontinue"
# The dates between which you wish to query; manually set
$StartDate = "12/01/2014"
$EndDate = "01/01/2015"
# The location of the .txt files being used for input and output; manually set
set-location "FilePathHere"
# The name of the .txt file which contains the list of systems you wish to query; manually set
$ReadFile = ".\IPrangefile.txt"
# The name of the .txt file to which you wish to log the results of the query; manually set
$WriteFile = ".\output-domainName.txt"
# Needs to be set to 0 for an accurate end count
$TotalCount = 0
$Timestamp = get-date
"Query start time:  $Timestamp" >> $WriteFile
"Queried timeframe:  $StartDate - $EndDate" >> $WriteFile
# Logic loop for each row of text in the $ReadFile
foreach ($System in get-content $ReadFile)
  {
  # To reset $Log.count and catch failed queries
  $Log = "*"
  # PING $System
  if (test-connection $System)
    {
    # Check operating system
    $QueryString = Gwmi Win32_OperatingSystem -Comp $System
    $QueryString = $QueryString.Caption
    # Query system log for update events within specified timeframe
    $Log =
    Get-EventLog -ComputerName $System -LogName System -After $StartDate -Before $EndDate | where {$_.EventID -eq 19 -or $_.EventID -eq 4377 -or $_.EventID -eq 1022}
    # Failed to query system log
    if ($Log -eq "*")
      {
      write-host "$System has no data available"
      "$System has no data available" >> $WriteFile
      }
    # $System has none of the queried EventID's within the timeframe specified
    elseif ($Log.length -le 0)
      {
      write-host "$System has 0 updates - $QueryString"
      "$System has 0 updates - $QueryString" >> $WriteFile
      }
    # Update the total count
    else
      {
      $LogCount = $Log.count
      write-host "$System has $LogCount update(s) - $QueryString"
      "$System has $LogCount update(s) - $QueryString" >> $WriteFile
      $TotalCount = $TotalCount + $LogCount
      }
    }
  # could not PING
  else
    {
    Write-Host "$System is unreachable"
    "$System is unreachable" >> $WriteFile
    }
  }
write-host "$TotalCount update(s) total"
"$TotalCount update(s) total" >> $WriteFile

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles