GetMultipleFilesVersion.ps1

<#PSScriptInfo
 
.VERSION 0.68
 
.GUID 5dc7a402-50a9-4d13-98c6-7cd4df659408
 
.AUTHOR racarb
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS Files FilesVersions Versions Remote
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
.PRIVATEDATA
 
.DESCRIPTION
 Script capable to get multiple files versions from multiple remote servers
 
 Get-MultipleFilesVersion -Computersgroup $listofcomputers -Filefullpaths $Filefullpaths
 
 
#>


Function global:Get-FileInfo {
    [CmdletBinding()]
    param ([string[]]$Filepaths)
                        
    $returnobject = New-Object -TypeName PSObject
    foreach ($Filepath in $Filepaths) {
                        
                        
        $filename = Split-Path $Filepath -Leaf
        try {
            $object = (Get-Item -path $Filepath -ErrorAction Stop).VersionInfo 
            $Fileversion = "$($object.FileMajorPart).$($object.FileMinorPart).$($object.ProductBuildPart).$($object.ProductPrivatePart)"
        }
        catch { $Fileversion = "notfound" }
                        
        $returnobject | Add-Member -MemberType NoteProperty -Name $filename -Value $Fileversion
                        
    }
    $returnobject
}

Function global:Get-MultipleFilesVersion {
    Param (
        [Parameter(Mandatory = $true)]
        [string[]]$Computersgroup,
        [Parameter(Mandatory = $true)]
        [string[]]$Filefullpaths
    )
    $global:results = @()

    Foreach ($server in $Computersgroup) {
        $UNCable = $false
        $WINRMable = $false
        $Resolved = $false
        $Resolvedresults = $null

        #Reset properties to $null to get well displayed
        $FileLeafnames = $Filefullpaths | ForEach-Object { ($_ -split "\\")[-1] }
        $Fileversions = New-Object -TypeName PSObject
        foreach ($PropertyToAdd in $FileLeafnames) {
            $Fileversions | Add-Member -MemberType NoteProperty -Name $PropertyToAdd -Value $null
        }


        Write-Host "Testing computer $server... " -NoNewline

        #Test resolution
        try { $Resolvedresults = Resolve-DnsName $server -ErrorAction Stop }
        catch { }
        if ($null -ne $Resolvedresults) {
            $Resolved = $true
            $ip = $Resolvedresults.IPAddress | Where-Object { $_ -notmatch "\p{L}" } | Select-Object -First 1

            #try with UNC
            #Test port 445
            $tcp = New-Object System.Net.Sockets.TcpClient
            $resulttcp = $tcp.BeginConnect($ip, 445, $null, $null)
            if ($resulttcp.AsyncWaitHandle.WaitOne( ([timespan]::FromSeconds(3)))) { $UNCable = $true }
            else { $UNCable = $false; Write-Host "UNCFailed." -ForegroundColor Red -NoNewline }
                     
            if ($UNCable -eq $true) {
                Write-Host "UNCReached." -ForegroundColor Green -NoNewline
                $WINRMable = "pass"
                $FilefullpathsUNC = $Filefullpaths | ForEach-Object { "\\$server\$($_ -replace ":\\","$\")" }
                $Fileversions = Get-FileInfo -Filepaths $FilefullpathsUNC
            }
            if ($UNCable -ne "pass") {
                #tryWINRM
                if (Test-WSMan -ComputerName $server -ErrorAction SilentlyContinue) {
                    if ((Invoke-Command -ComputerName $server -ErrorAction SilentlyContinue { $true }) -eq $true) { $WINRMable = $true }
                    else { $WINRMable = $false }
                }
                else { $WINRMable = $false; Write-Host "WINRMFailed" -ForegroundColor Red -NoNewline }
                if ($WINRMable -eq $true) {
                    Write-Host "WINRMReached" -ForegroundColor Green -NoNewline
                    $FunctionObject = Get-Item Function:\Get-FileInfo
                    $Fileversions = Invoke-Command -ComputerName $server {
                        #Defining the Function remotelly:
                        New-Item -Path 'Function:\Get-FileInfo' -Value $using:FunctionObject.ScriptBlock | Out-Null
                        Get-FileInfo -Filepaths $using:Filefullpaths
                    }
                }
            }
        }
        else {
            $Resolved = $false
            Write-Host "Can't resolve" -ForegroundColor Red -NoNewline
        }
                        
        $obj = New-Object -TypeName PSObject -Property ([ordered]@{
                Server    = $server
                Resolved  = $Resolved
                UNCable   = $UNCable
                WINRMable = $WINRMable

            })
        $FileLeafnames = $Filefullpaths | ForEach-Object { ($_ -split "\\")[-1] }
        $PropertiesToAdd = $Fileversions.PSObject.Properties | Where-Object { $_.Name -in ($FileLeafnames) }
        Foreach ($PropertyToAdd in $PropertiesToAdd) {
            $obj | Add-Member -MemberType NoteProperty -Name $PropertyToAdd.Name -Value $PropertyToAdd.Value
        }
     
        $global:results += $obj
        Write-Host
    }

    $global:results

    Write-Host
    Write-Host "The results where saved in the global variable '`$results'. In order to see them again in diferent formats, use the variable again.
    p.e.: `$results | ft "
 
}


#https://portal.msrc.microsoft.com/en-US/security-guidance/advisory/CVE-2019-0708
#Windows Server 2008 R2 for x64-based Systems Service Pack 1 - kb4499164 Monthly Rollup termdd.sys 6.1.7601.24441
#Windows Server 2008 for x64-based Systems Service Pack 2 - kb4499149 Monthly Rollup termdd.sys 6.0.6003.20514

#INPUT SECTION#
#File(s) to test. EXAMPLE of Array of paths:
$global:Filefullpaths = @(
    "C:\Windows\System32\drivers\termdd.sys",
    "C:\Windows\System32\drivers\cdrom.sys",
    "C:\Windows\System32\drivers\storport.sys",
    "C:\Windows\System32\drivers\tcpip.sys"
)

<#
#ComputerName selections samples
$computers = Get-ADComputer -Filter '(operatingSystem -like "*2008 R2*") -or (operatingSystem -like "*2008 Standard*") -or (operatingSystem -like "*2008 Enterprise*")'
 
$computers = Get-ADComputer -Filter * -Properties OperatingSystem, OperatingSystemServicePack, OperatingSystemHotfix
$2008R2 = $computers | Where-Object OperatingSystem -Match "2008 R2"
$2008R2 | Sort-Object -Property OperatingSystem | Select-Object DNSHostName, OperatingSystem, OperatingSystemServicePack, OperatingSystemHotfix
 
$2008 = $computers | Where-Object { ($_.OperatingSystem -Match "2008 Standard") -or ($_.OperatingSystem -Match "2008 Enterprise") }
$2008 | Sort-Object -Property OperatingSystem | Select-Object DNSHostName, OperatingSystem, OperatingSystemServicePack, OperatingSystemHotfix
 
 
#Launch funtions from a group of PCs from a variable
 
$global:2008R2Results = Get-MultipleFilesVersion -Computersgroup $2008R2.Name -Filefullpaths $Filefullpaths
$global:2008Results = Get-MultipleFilesVersion -Computersgroup $2008.Name -Filefullpaths $Filefullpaths
 
 
 
#Results exported to excel
2008R2Results | Export-Csv -Path c:\temp\2008R2Results.csv -NoTypeInformation -Force
$2008Results | Export-Csv -Path c:\temp\2008Results.csv -NoTypeInformation -Force
 
 
#Results as objects
$2008R2Results | Sort-Object -Descending
$2008Results | Sort-Object -Descending
 
#Results formatted as table
$2008R2Results | Sort-Object -Descending |ft
$2008Results | Sort-Object -Descending | ft
 
#Results OugridView
$2008R2Results | Out-GridView
$2008Results | Out-GridView
 
 
#>