45 lines
4.2 KiB
PowerShell
45 lines
4.2 KiB
PowerShell
# NAM LDAP cutover, vd03 — Phase 0 preflight + backups (read-only except the backup folder). Values of secrets are never printed.
|
|
$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue'
|
|
$ts = Get-Date -Format 'yyyyMMddTHHmmss'
|
|
$bk = "E:\ApiInstall\_ldapcut-$ts"; New-Item -ItemType Directory -Force $bk | Out-Null
|
|
"backup dir $bk"
|
|
# 1. DC reach
|
|
foreach ($h in 'uswap-vdc-nam1.nam.zimmer.com','uswap-vdc-nam2.nam.zimmer.com') { foreach ($p in 636,389) {
|
|
$c = New-Object Net.Sockets.TcpClient; $ok = $c.ConnectAsync($h,$p).Wait(4000); $c.Dispose(); "reach $h`:$p $ok" } }
|
|
# 2. TLS trust
|
|
$root = @(Get-ChildItem Cert:\LocalMachine\Root | Where-Object { $_.Subject -like '*zimmer-rootca*' })
|
|
"zimmer-rootca in LocalMachine\Root: $($root.Count) $(($root | % { $_.Thumbprint + ' exp ' + $_.NotAfter.ToString('yyyy-MM-dd') }) -join '; ')"
|
|
foreach ($h in 'uswap-vdc-nam1.nam.zimmer.com','uswap-vdc-nam2.nam.zimmer.com') {
|
|
try { $t = New-Object Net.Sockets.TcpClient($h,636); $script:err = $null
|
|
$s = New-Object Net.Security.SslStream($t.GetStream(), $false, { param($a,$b,$c,$e) $script:err = $e; $true })
|
|
$s.AuthenticateAsClient($h); "tls $h policyErrors=$script:err proto=$($s.SslProtocol)"; $s.Dispose(); $t.Dispose() } catch { "tls $h FAILED $($_.Exception.Message)" } }
|
|
# 3. services + env (allow-listed values only) + reg export
|
|
$svcs = Get-CimInstance Win32_Service | Where-Object { $_.PathName -match 'ApiInstall\\(HistorianGateway|MxGateway|OtOpcUa|ScadaBridge)' }
|
|
$show = '(?i)ENVIRONMENT$|DisableLogin|AllowOutside|__Ldap__(Enabled|Server|Port|Transport|AllowInsecure|SearchBase|ServiceAccountDn|UserNameAttribute|BindMode|FallbackServers)|GroupToRole|^Secrets__(SqlitePath|MasterKey__Source|MasterKey__FilePath|MasterKey__EnvVarName|Replication__Enabled)|RequiredGroup|Thumbprint'
|
|
foreach ($s in $svcs) {
|
|
"== service $($s.Name) state=$($s.State) path=$($s.PathName)"
|
|
& reg.exe export "HKLM\SYSTEM\CurrentControlSet\Services\$($s.Name)" "$bk\reg-$($s.Name).reg" /y | Out-Null
|
|
$k = "HKLM:\SYSTEM\CurrentControlSet\Services\$($s.Name)"
|
|
$lines = @((Get-ItemProperty $k -ErrorAction SilentlyContinue).Environment) + @((Get-ItemProperty "$k\Parameters" -ErrorAction SilentlyContinue).AppEnvironmentExtra) | Where-Object { $_ }
|
|
foreach ($l in $lines) { $i = $l.IndexOf('='); if ($i -lt 1) { continue }; $n = $l.Substring(0,$i)
|
|
if ($n -match $show) { " env $l" } else { " env $n=<hidden len $($l.Length-$i-1)>" } }
|
|
}
|
|
# 4. config file copies
|
|
foreach ($p in 'HistorianGateway\Server\appsettings*.json','MxGateway\appsettings.json','MxGateway\Server\appsettings*.json','OtOpcUa\appsettings*.json','ScadaBridge\app\appsettings*.json') {
|
|
foreach ($f in Get-ChildItem "E:\ApiInstall\$p") { $d = "$bk\" + ($f.FullName.Substring(15) -replace '\\','_'); Copy-Item $f.FullName $d; "copied $($f.FullName) sha256 $((Get-FileHash $d).Hash.Substring(0,16))" } }
|
|
# 5. OtOpcUa DB overlay rows
|
|
$oto = Get-CimInstance Win32_Service | Where-Object { $_.PathName -match 'ApiInstall\\OtOpcUa\\' } | Select-Object -First 1
|
|
$env1 = @((Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Services\$($oto.Name)").Environment)
|
|
$cs = ($env1 | Where-Object { $_ -match '^(ConnectionStrings__ConfigDb|.*ConfigDb.*|.*ConnectionString.*)=' } | Select-Object -First 1)
|
|
if ($cs) { $cs = $cs.Substring($cs.IndexOf('=')+1)
|
|
try { $c = New-Object System.Data.SqlClient.SqlConnection $cs; $c.Open(); $cmd = $c.CreateCommand()
|
|
$cmd.CommandText = "SELECT COUNT(*) FROM sys.tables WHERE name='LdapGroupRoleMapping'"; if ($cmd.ExecuteScalar() -gt 0) {
|
|
$cmd.CommandText = "SELECT * FROM dbo.LdapGroupRoleMapping"; $r = $cmd.ExecuteReader(); $n=0
|
|
while ($r.Read()) { $n++; " otopcua overlay row: " + ((0..($r.FieldCount-1) | % { $r.GetName($_) + '=' + $r.GetValue($_) }) -join ' ') }; "otopcua overlay rows: $n" } else { "otopcua overlay table absent" }
|
|
$c.Close() } catch { "otopcua overlay query failed: $($_.Exception.Message)" } } else { "otopcua: no config connstr env found" }
|
|
# 6. secret CLI
|
|
Get-ChildItem 'E:\ApiInstall\_tools\secret' | Select-Object -First 8 | % { "tool $($_.Name) $($_.Length)" }
|
|
$exe = Get-ChildItem 'E:\ApiInstall\_tools\secret' -Recurse -Filter 'secret.exe' | Select-Object -First 1
|
|
if ($exe) { "secret.exe $($exe.FullName) ver $($exe.VersionInfo.ProductVersion)" }
|
|
"PREFLIGHT DONE $bk"
|