Compare-HotFix.ps1


<#PSScriptInfo
 
.VERSION 1.0
 
.GUID a1737d3f-2ffe-4ca6-9d4e-5c6ec44c810f
 
.DESCRIPTION Allow to compare the installed HotFixes between 2 computers.
 
.AUTHOR Dordouf
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
.PRIVATEDATA
 
#>

 

<#
 
 .SYNOPSIS
     Allow to compare the installed HotFixes between 2 computers?
   
  .DESCRIPTION
     Allow to compare the installed HotFixes between 2 computers?
   
   
  .NOTES
     File Name : Compare-HotFix.ps1
     Author : Jordan CHERKI, cherkijordan@yahoo.fr
     Date : 2018/22/10
     Version : 1.0.0
   
  .EXAMPLE
     Compare-HotFix -SourceComputerName $Server1 -DestinationComputerName $Server2
   
   
  .PARAMETER
     SourceComputerName (Mandatory)
     DestinationComputerName (Mandatory)
#>


[CmdletBinding()]
Param
(
    [Parameter(Mandatory=$true)]
    [string]$SourceComputerName,
    [Parameter(Mandatory=$true)]
    [string]$DestinationComputerName
)

#---------------------------------------------------------------------------------------
# Function TEST PING
# Allow to verify if a computer is reachable
#---------------------------------------------------------------------------------------
Function Test-Ping
{
    Param
    (
        [Parameter(Mandatory=$true)]
        [string]$ComputerName
    )

    If (Test-Connection -ComputerName $ComputerName -Count 1 -Quiet)
    {
        return $true
    }
    Else
    {
        return $false
    }
}
#---------------------------------------------------------------------------------------


#---------------------------------------------------------------------------------------
# Function GET UPDATE
# Allow to get updates from remote computer
#---------------------------------------------------------------------------------------
Function Get-Update
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true)]
        [string]$ComputerName    
    )

    Try
    {
        # Getting Updates
        $Updates = Get-WmiObject -Class Win32_QuickFixEngineering -ComputerName $ComputerName -ErrorAction Stop

        # Formatting updates in an arry
        $tab = @()
        ForEach ($Update in $Updates)
        {
            $obj = New-Object -TypeName PSObject
            Add-Member -InputObject $obj -MemberType NoteProperty -Name "Update" -Value $Update.HotFixID
            $tab += $obj
        } # End ForEach Updates

        return $tab
    } # End Try
    Catch
    {
        return $_.Exception.Message
    } # End Catch
}
#---------------------------------------------------------------------------------------


#---------------------------------------------------------------------------------------
# Function COMPARE UPDATE
# Allow to compare updates between 2 computers
#---------------------------------------------------------------------------------------
Function Compare-Update
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true)]
        $SourceUpdates,
        [Parameter(Mandatory=$true)]
        $DestinationUpdates
    )

    $updateComp = Compare-Object -ReferenceObject $SourceUpdates.Update -DifferenceObject $DestinationUpdates.Update

    $tab = @()

    ForEach ($update in $updateComp)
    {
        # => : KB present in DestinationUpdates but not in SourceUpdates
        # <= : KB present in SourceUpdates but not in DestinationUpdates
        $obj = New-Object -TypeName PSObject

        If ($update.SideIndicator -eq "=>")
        {
            Add-Member -InputObject $obj -MemberType NoteProperty -Name "ComputerName" -Value $SourceComputerName
            Add-Member -InputObject $obj -MemberType NoteProperty -Name "ComparisonStatus" -Value "$($update.InputObject) Not Present"

            $tab += $obj
        }
        Else
        {
            Add-Member -InputObject $obj -MemberType NoteProperty -Name "ComputerName" -Value $DestinationComputerName
            Add-Member -InputObject $obj -MemberType NoteProperty -Name "ComparisonStatus" -Value "$($update.InputObject) Not Present"

            $tab += $obj
        }
    } # End ForEach Update

    return $tab
}
#---------------------------------------------------------------------------------------



#---------------------------------------------------------------------------------------
# Main
#---------------------------------------------------------------------------------------

$Servers = @($SourceComputerName,$DestinationComputerName)

ForEach ($Server in $Servers)
{
    If (!(Test-Ping -ComputerName $Server))
    {
        Write-Host "$Server is unreachable !" -ForegroundColor Red
        return
    }
}

# Getting Updates
$sourceUpdates      = Get-Update -ComputerName $SourceComputerName
$destinationUpdates = Get-Update -ComputerName $DestinationComputerName

# Displaying updates error
If ((($sourceUpdates.GetType()).BaseType).Name -eq "Object")
{
    Write-Host "Error - $sourceUpdates" -ForegroundColor Red
    return
}
If ((($destinationUpdates.GetType()).BaseType).Name -eq "Object")
{
    Write-Host "Error - $destinationUpdates" -ForegroundColor Red
    return
}

# Comparison Updates
$comparisonResult = Compare-Update -SourceUpdates $sourceUpdates -DestinationUpdates $destinationUpdates
If ($comparisonResult.count -eq 0)
{
    $comparisonResult = @()
    ForEach ($Server in $Servers)
    {
        $obj = New-Object -TypeName PSObject
        Add-Member -InputObject $obj -MemberType NoteProperty -Name "ComputerName" -Value $Server
        Add-Member -InputObject $obj -MemberType NoteProperty -Name "ComparisonStatus" -Value "No KB difference"

        $comparisonResult += $obj
    } # End ForEach $servers
} # End If $comparisonResult = 0

# Display comparison result
$comparisonResult | Format-Table -AutoSize