-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindowsRemoteBuild-RemoteRegistry.ps1
44 lines (37 loc) · 1.58 KB
/
WindowsRemoteBuild-RemoteRegistry.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
$remoteComputers = @(
"Computer1",
"192.168.1.10"
)
$outputFile = "C:\AllComputers-NoWRM.txt"
foreach ($remoteComputer in $remoteComputers) {
$outputObject = $null
# Try to establish a remote registry connection
try {
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $remoteComputer)
$regKey = $reg.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion")
if ($regKey) {
$currentBuild = $regKey.GetValue("CurrentBuild")
$ubr = $regKey.GetValue("UBR")
if ($currentBuild -and $ubr) {
$osVersion = "$currentBuild.$ubr"
# Create a custom object with computer name and OSVersion
$outputObject = [PSCustomObject]@{
ComputerName = $remoteComputer
OSVersion = $osVersion
}
}
}
$regKey.Close()
$reg.Close()
} catch {
Write-Host "Failed to establish a remote registry connection to $remoteComputer."
}
if ($outputObject) {
# Append the output to the text file
if (-not (Test-Path -Path $outputFile)) {
$outputObject | Select-Object ComputerName, OSVersion | ConvertTo-Csv -NoTypeInformation | ForEach-Object { $_ -replace '"' } | Out-File -FilePath $outputFile -Encoding UTF8
} else {
$outputObject | Select-Object ComputerName, OSVersion | ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1 | ForEach-Object { $_ -replace '"' } | Out-File -FilePath $outputFile -Append -Encoding UTF8
}
}
}