GetMultipleFilesVersion.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<#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
 
 
#>