param( [Parameter(Mandatory = $true)] [string] $SdfPath, [Parameter(Mandatory = $true)] [string] $OutFile, [string] $Password = '', [string] $SqlCeDll = '' ) $ErrorActionPreference = 'Stop' function Find-SqlCeDll([string] $preferred) { if ($preferred -and (Test-Path -LiteralPath $preferred)) { return $preferred } $candidates = @( 'C:\Program Files\Microsoft SQL Server Compact Edition\v4.0\Desktop\System.Data.SqlServerCe.dll', 'C:\Program Files (x86)\Microsoft SQL Server Compact Edition\v4.0\Desktop\System.Data.SqlServerCe.dll' ) foreach ($path in $candidates) { if (Test-Path -LiteralPath $path) { return $path } } throw 'Nenalezena knihovna System.Data.SqlServerCe.dll (SQL Server Compact 4.0).' } function New-ConnectionString([string] $path, [string] $password) { $cs = "Data Source=$path;Max Database Size=4091" if ($password -ne '') { $cs += ";Password=$password" } return $cs } function Read-TableOptional($connection, [string] $sql) { try { return @(Read-Table $connection $sql) } catch { return @() } } function Read-Table($connection, [string] $sql) { $cmd = $connection.CreateCommand() $cmd.CommandText = $sql $adapter = New-Object System.Data.SqlServerCe.SqlCeDataAdapter($cmd) $table = New-Object System.Data.DataTable [void] $adapter.Fill($table) $rows = New-Object System.Collections.Generic.List[object] foreach ($row in $table.Rows) { $item = [ordered] @{} foreach ($col in $table.Columns) { $value = $row[$col] if ($value -is [DBNull]) { $item[$col.ColumnName] = $null } elseif ($value -is [datetime]) { $item[$col.ColumnName] = ([datetime] $value).ToString('o') } else { $item[$col.ColumnName] = $value } } $rows.Add([pscustomobject] $item) } return $rows } function Open-Sdf([string] $path, [string] $password) { $cs = New-ConnectionString $path $password try { $conn = New-Object System.Data.SqlServerCe.SqlCeConnection($cs) $conn.Open() return $conn } catch { $message = $_.Exception.Message if ($message -notmatch 'earlier version|Upgrade') { throw } $engine = New-Object System.Data.SqlServerCe.SqlCeEngine($cs) $engine.Upgrade() $conn = New-Object System.Data.SqlServerCe.SqlCeConnection($cs) $conn.Open() return $conn } } if (-not (Test-Path -LiteralPath $SdfPath)) { throw "SDF soubor neexistuje: $SdfPath" } Add-Type -Path (Find-SqlCeDll $SqlCeDll) $workDir = Split-Path -Parent $OutFile if (-not (Test-Path -LiteralPath $workDir)) { New-Item -ItemType Directory -Path $workDir | Out-Null } $workSdf = Join-Path $workDir 'source.sdf' Copy-Item -LiteralPath $SdfPath -Destination $workSdf -Force $passwords = @() if ($Password -ne '') { $passwords += $Password } $passwords += '' $connection = $null $lastError = $null foreach ($pw in $passwords) { try { $connection = Open-Sdf $workSdf $pw break } catch { $lastError = $_ $connection = $null } } if (-not $connection) { throw "Nepodařilo se otevřít SDF databázi. $($lastError.Exception.Message)" } try { $payload = [ordered] @{ uzivatel = @(Read-Table $connection 'SELECT * FROM UZIVATEL ORDER BY ID') zakaznik = @(Read-Table $connection 'SELECT * FROM ZAKAZNIK ORDER BY ID') faktura = @(Read-Table $connection 'SELECT * FROM FAKTURA ORDER BY ID') polozka = @(Read-Table $connection 'SELECT * FROM POLOZKA ORDER BY ID') platba = @(Read-TableOptional $connection 'SELECT * FROM PLATBA ORDER BY ID') konfigurace = @(Read-TableOptional $connection 'SELECT ID, DATA FROM KONFIGURACE ORDER BY ID') } $json = $payload | ConvertTo-Json -Depth 6 -Compress:$false [System.IO.File]::WriteAllText($OutFile, $json, [System.Text.UTF8Encoding]::new($false)) } finally { if ($connection -and $connection.State -eq 'Open') { $connection.Close() } }