# version.ps1
# Liest Dateiversionen aus und schreibt nach .\bin\version.log (wie WMIC caption,version)

Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"

# Zielordner/-datei wie im Batch
$binDir  = Join-Path -Path (Get-Location) -ChildPath "bin"
$logFile = Join-Path -Path $binDir -ChildPath "version2.log"

# Ordner anlegen, falls nicht vorhanden
if (-not (Test-Path -LiteralPath $binDir)) {
    New-Item -Path $binDir -ItemType Directory | Out-Null
}

# Liste der Dateien aus deinem Batch (WMIC Name='C:\\...') -> in PowerShell als normale Pfade
$files = @(
    'C:\Program Files (x86)\Common Files\Macrovision Shared\FlexNet Publisher\FNPLicensingService.exe',
    'C:\Program Files\Common Files\Macrovision Shared\FlexNet Publisher\FNPLicensingService64.exe',
    'C:\Program Files (x86)\Common Files\Leica Geosystems\License-Server\LGS.exe',
    'C:\Program Files (x86)\Common Files\Leica Geosystems\License-Server\lmgrd.exe',
    'C:\Program Files (x86)\Common Files\Leica Geosystems\License-Server\FNPCommsSoap.dll',
    'C:\Program Files (x86)\Leica Geosystems\CLM-Administration\clmadmin.exe',
    'C:\Program Files (x86)\Leica Geosystems\CLM-Administration\clmadminsvr.exe',
    'C:\Program Files (x86)\Leica Geosystems\CLM-Administration\clmrdsvr.exe',
    'C:\Program Files (x86)\Leica Geosystems\CLM\CLM.exe',
    'C:\Program Files\Leica Geosystems\Cyclone\Cyclone.exe',
    'C:\Program Files\Leica Geosystems\JetStream\JetStream.exe',
    'C:\Program Files\Leica Geosystems\JetStreamAdmin\JetStreamAdmin.exe',
    'C:\Program Files\Leica Geosystems\JetStreamViewer\JetStreamViewer.exe',
    'C:\Program Files\Leica Geosystems\Cyclone REGISTER 360\Register360.exe',
    'C:\Program Files\Leica Geosystems\Cyclone REGISTER 360 (BLK Edition)\Register360-BLK.exe',
    'C:\Program Files\Leica Geosystems\Cyclone 3DR\3DR.exe',
    'C:\Program Files\Leica Geosystems\BLK360 Data Manager\Blk360DataManager.exe',
    'C:\Program Files\Leica Geosystems\Cyclone ENTERPRISE\JetStreamSrv.exe',
    'C:\Program Files\Leica Geosystems\Cyclone ENTERPRISE\node.exe'
)

# Ausgabeformat ähnlich WMIC (Caption + Version)
# WMIC spuckt oft Spaltenüberschriften aus; wir machen eine saubere, gut parsbare Ausgabe.
"Caption`tVersion" | Set-Content -LiteralPath $logFile -Encoding UTF8

foreach ($path in $files) {
    if (Test-Path -LiteralPath $path) {
        try {
            $item = Get-Item -LiteralPath $path
            $ver  = $item.VersionInfo.FileVersion

            # Fallback, falls FileVersion leer ist (kommt selten vor)
            if ([string]::IsNullOrWhiteSpace($ver)) {
                $ver = $item.VersionInfo.ProductVersion
            }
            if ([string]::IsNullOrWhiteSpace($ver)) {
                $ver = "<no version info>"
            }

            "$($item.FullName)`t$ver" | Add-Content -LiteralPath $logFile -Encoding UTF8
        }
        catch {
            "$path`t<error: $($_.Exception.Message)>" | Add-Content -LiteralPath $logFile -Encoding UTF8
        }
    }
    else {
        "$path`t<not found>" | Add-Content -LiteralPath $logFile -Encoding UTF8
    }
}