-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetup_Credentials.ps1
More file actions
152 lines (128 loc) · 5.65 KB
/
Setup_Credentials.ps1
File metadata and controls
152 lines (128 loc) · 5.65 KB
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
<#
.SYNOPSIS
Speichert Zugangsdaten sicher im Windows Credential Manager.
.DESCRIPTION
Einmalig ausführen, um Firebird- und SQL Server-Passwörter sicher zu hinterlegen.
Die Credentials sind an den Windows-Benutzer UND den Computer gebunden.
.NOTES
Nach Ausführung können die Passwörter aus config.json entfernt werden.
.LINK
https://github.com/gitnol/PSFirebirdToMSSQL
#>
# -----------------------------------------------------------------------------
# CREDENTIAL TARGETS (Namen unter denen die Secrets gespeichert werden)
# -----------------------------------------------------------------------------
$TargetFirebird = "SQLSync_Firebird"
$TargetMSSQL = "SQLSync_MSSQL"
# -----------------------------------------------------------------------------
# FUNKTIONEN
# -----------------------------------------------------------------------------
function Set-StoredCredential {
param(
[string]$Target,
[string]$Username,
[securestring]$Password
)
# Nutzt cmdkey.exe (in Windows eingebaut)
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password)
$PlainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
[System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($BSTR)
$Result = cmdkey /generic:$Target /user:$Username /pass:$PlainPassword
# Passwort aus Speicher löschen
$PlainPassword = $null
[System.GC]::Collect()
return $LASTEXITCODE -eq 0
}
function Test-StoredCredential {
param([string]$Target)
return [bool]((cmdkey /list) -match $Target)
}
# -----------------------------------------------------------------------------
# HAUPTLOGIK
# -----------------------------------------------------------------------------
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " SQLSync - Credential Setup" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "Die Zugangsdaten werden im Windows Credential Manager gespeichert."
Write-Host "Sie sind verschlüsselt und nur für DIESEN Benutzer auf DIESEM Computer abrufbar."
Write-Host ""
# --- FIREBIRD ---
Write-Host "--- Firebird Datenbank ---" -ForegroundColor Yellow
if (Test-StoredCredential -Target $TargetFirebird) {
Write-Host "Credential '$TargetFirebird' existiert bereits." -ForegroundColor DarkGray
$Overwrite = Read-Host "Überschreiben? [J/N]"
if ($Overwrite -ne "J") {
Write-Host "Übersprungen." -ForegroundColor DarkGray
}
else {
$FbUser = Read-Host "Firebird Benutzername (z.B. SYSDBA)"
$FbPass = Read-Host "Firebird Passwort" -AsSecureString
if (Set-StoredCredential -Target $TargetFirebird -Username $FbUser -Password $FbPass) {
Write-Host "Firebird Credential gespeichert." -ForegroundColor Green
}
else {
Write-Host "Fehler beim Speichern!" -ForegroundColor Red
}
}
}
else {
$FbUser = Read-Host "Firebird Benutzername (z.B. SYSDBA)"
$FbPass = Read-Host "Firebird Passwort" -AsSecureString
if (Set-StoredCredential -Target $TargetFirebird -Username $FbUser -Password $FbPass) {
Write-Host "Firebird Credential gespeichert." -ForegroundColor Green
}
else {
Write-Host "Fehler beim Speichern!" -ForegroundColor Red
}
}
Write-Host ""
# --- MSSQL (nur wenn SQL Auth verwendet wird) ---
Write-Host "--- Microsoft SQL Server ---" -ForegroundColor Yellow
Write-Host "Hinweis: Bei 'Integrated Security' (Windows Auth) wird kein Passwort benötigt."
$UseSqlAuth = Read-Host "SQL Server Authentifizierung einrichten? [J/N]"
if ($UseSqlAuth -eq "J") {
if (Test-StoredCredential -Target $TargetMSSQL) {
Write-Host "Credential '$TargetMSSQL' existiert bereits." -ForegroundColor DarkGray
$Overwrite = Read-Host "Überschreiben? [J/N]"
if ($Overwrite -ne "J") {
Write-Host "Übersprungen." -ForegroundColor DarkGray
}
else {
$SqlUser = Read-Host "SQL Server Benutzername"
$SqlPass = Read-Host "SQL Server Passwort" -AsSecureString
if (Set-StoredCredential -Target $TargetMSSQL -Username $SqlUser -Password $SqlPass) {
Write-Host "SQL Server Credential gespeichert." -ForegroundColor Green
}
else {
Write-Host "Fehler beim Speichern!" -ForegroundColor Red
}
}
}
else {
$SqlUser = Read-Host "SQL Server Benutzername"
$SqlPass = Read-Host "SQL Server Passwort" -AsSecureString
if (Set-StoredCredential -Target $TargetMSSQL -Username $SqlUser -Password $SqlPass) {
Write-Host "SQL Server Credential gespeichert." -ForegroundColor Green
}
else {
Write-Host "Fehler beim Speichern!" -ForegroundColor Red
}
}
}
else {
Write-Host "SQL Server Auth übersprungen (Windows Auth wird verwendet)." -ForegroundColor DarkGray
}
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " Setup abgeschlossen!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "Gespeicherte Credentials anzeigen:" -ForegroundColor Gray
Write-Host " cmdkey /list:SQLSync*" -ForegroundColor White
Write-Host ""
Write-Host "Credential löschen:" -ForegroundColor Gray
Write-Host " cmdkey /delete:SQLSync_Firebird" -ForegroundColor White
Write-Host " cmdkey /delete:SQLSync_MSSQL" -ForegroundColor White
Write-Host ""
Write-Host "WICHTIG: Entferne jetzt die Passwörter aus config.json!" -ForegroundColor Yellow