-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge-MainSwitch.psm1
More file actions
1234 lines (1040 loc) · 43.8 KB
/
Copy pathMerge-MainSwitch.psm1
File metadata and controls
1234 lines (1040 loc) · 43.8 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# DOTS formatting comment
<#
.SYNOPSIS
Content-aware merge tool for Main-Switch.ps1 across multiple admins.
.DESCRIPTION
Parses Main-Switch.ps1 into Header, individual switch-case blocks, and Footer.
Compares two versions (local and central) using three-way merge with a stored
base file to detect which side changed. Auto-merges non-conflicting changes
and prompts for conflict resolution when both sides modified the same case.
Written by Skyler Werner
Date: 2026/03/18
Version 1.0.0
#>
# ============================================================================
# Private: Resolve default paths from Paths.txt
# ============================================================================
function Get-DefaultScriptPath {
[CmdletBinding()]
param()
$pathsFile = Join-Path $env:APPDATA 'Patching\Paths.txt'
if (-not (Test-Path $pathsFile)) { return $null }
$lines = Get-Content -Path $pathsFile -Encoding Default
foreach ($line in $lines) {
if ($line -match '^\s*ScriptPath\s*:\s*(.+)$') {
$resolved = $Matches[1].Trim()
if (Test-Path $resolved) { return $resolved }
}
}
return $null
}
# ============================================================================
# Private: Parser -- ConvertTo-ParsedMainSwitch
# ============================================================================
function ConvertTo-ParsedMainSwitch {
<#
.SYNOPSIS
Parses a Main-Switch.ps1 file into Header, Cases (ordered dict), and Footer.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$Path
)
if (-not (Test-Path -LiteralPath $Path)) {
throw "File not found: $Path"
}
# Read all lines preserving encoding
$allLines = Get-Content -Path $Path -Encoding Default
# --- Find header end: the "switch ($targetSoftware) {" line ---
$switchOpenIndex = -1
for ($i = 0; $i -lt $allLines.Count; $i++) {
if ($allLines[$i] -match '^\s*switch\s*\(\$targetSoftware\)\s*\{') {
$switchOpenIndex = $i
break
}
}
if ($switchOpenIndex -lt 0) {
throw "Could not find 'switch (`$targetSoftware) {' in $Path"
}
# --- Find footer start: the "} # End switch" line ---
$switchCloseIndex = -1
for ($i = $allLines.Count - 1; $i -ge 0; $i--) {
if ($allLines[$i] -match '^\s*\}\s*#\s*End\s+switch') {
$switchCloseIndex = $i
break
}
}
if ($switchCloseIndex -lt 0) {
throw "Could not find '} # End switch' in $Path"
}
# Header = lines 0 through switchOpenIndex (inclusive)
$header = ($allLines[0..$switchOpenIndex]) -join "`r`n"
# Footer = lines switchCloseIndex through end
$footer = ($allLines[$switchCloseIndex..($allLines.Count - 1)]) -join "`r`n"
# Switch body = lines between header and footer
$bodyStart = $switchOpenIndex + 1
$bodyEnd = $switchCloseIndex - 1
if ($bodyEnd -lt $bodyStart) {
# Empty switch body
$cases = [ordered]@{}
return [PSCustomObject]@{
Header = $header
Cases = $cases
Footer = $footer
}
}
$bodyLines = $allLines[$bodyStart..$bodyEnd]
# --- Parse body into case blocks ---
# Find all case-start line indices within the body
$casePattern = '^\s*"([^"]+)"\s*\{'
$caseStarts = [System.Collections.ArrayList]::new()
for ($i = 0; $i -lt $bodyLines.Count; $i++) {
if ($bodyLines[$i] -match $casePattern) {
[void]$caseStarts.Add(@{
Index = $i
Key = $Matches[1]
})
}
}
$cases = [ordered]@{}
if ($caseStarts.Count -eq 0) {
# No cases found -- store entire body as a special entry
$cases['__BODY__'] = ($bodyLines) -join "`r`n"
}
else {
# Text before the first case (region markers, blank lines)
if ($caseStarts[0].Index -gt 0) {
$prefixLines = $bodyLines[0..($caseStarts[0].Index - 1)]
}
else {
$prefixLines = @()
}
for ($c = 0; $c -lt $caseStarts.Count; $c++) {
$caseKey = $caseStarts[$c].Key
$startIdx = $caseStarts[$c].Index
# End index is the line before the next case's prefix starts,
# or end of body for the last case
if ($c -lt $caseStarts.Count - 1) {
$endIdx = $caseStarts[$c + 1].Index - 1
}
else {
$endIdx = $bodyLines.Count - 1
}
# The case block includes its prefix (comments/regions before it)
# For the first case, prefix is the text before it
# For subsequent cases, prefix is text after previous case's closing }
$caseText = ($prefixLines + $bodyLines[$startIdx..$endIdx]) -join "`r`n"
# Handle duplicate keys (unlikely but safe)
$finalKey = $caseKey
$suffix = 1
while ($cases.Contains($finalKey)) {
$suffix++
$finalKey = "${caseKey}_DUP${suffix}"
Write-Warning "Duplicate case key '$caseKey' found -- renamed to '$finalKey'"
}
$cases[$finalKey] = $caseText
# Calculate prefix for the NEXT case: lines between this case's
# content end and the next case's start line
if ($c -lt $caseStarts.Count - 1) {
$nextStartIdx = $caseStarts[$c + 1].Index
# Find where the current case's content actually ends
# We need to find lines between endIdx+1 and nextStartIdx-1
# But endIdx is already nextStartIdx-1, so prefix is empty
# Actually, let's re-examine: endIdx = nextStartIdx - 1
# So the NEXT case's prefix is the gap lines between cases
# Since we set endIdx = nextStart - 1, those gap lines are
# included in the current case's block. The next case has no prefix.
$prefixLines = @()
}
}
}
return [PSCustomObject]@{
Header = $header
Cases = $cases
Footer = $footer
}
}
# ============================================================================
# Private: Reconstructor -- ConvertFrom-ParsedMainSwitch
# ============================================================================
function ConvertFrom-ParsedMainSwitch {
<#
.SYNOPSIS
Reassembles a parsed Main-Switch object back into file content.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[PSCustomObject]$Parsed
)
$parts = [System.Collections.ArrayList]::new()
[void]$parts.Add($Parsed.Header)
foreach ($key in $Parsed.Cases.Keys) {
[void]$parts.Add($Parsed.Cases[$key])
}
[void]$parts.Add($Parsed.Footer)
return (($parts -join "`r`n") + "`r`n")
}
# ============================================================================
# Private: Normalize text for comparison
# ============================================================================
function Get-NormalizedText {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$Text
)
# Normalize line endings and trim trailing whitespace per line
$lines = $Text -split "`r?`n"
$trimmed = foreach ($line in $lines) { $line.TrimEnd() }
return ($trimmed -join "`n")
}
# ============================================================================
# Private: Three-way comparison engine
# ============================================================================
function Compare-ParsedFiles {
<#
.SYNOPSIS
Compares local and central parsed files using optional base for 3-way merge.
.OUTPUTS
PSCustomObject with HeaderStatus, FooterStatus, CaseResults (ordered dict)
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[PSCustomObject]$Local,
[Parameter(Mandatory)]
[PSCustomObject]$Central,
[Parameter()]
[PSCustomObject]$Base
)
$hasBase = $null -ne $Base
# --- Helper: determine status for a section ---
# Returns: Identical, LocalChanged, CentralChanged, Conflict
$getStatus = {
param($localText, $centralText, $baseText, $hasBase)
$localNorm = Get-NormalizedText -Text $localText
$centralNorm = Get-NormalizedText -Text $centralText
if ($localNorm -eq $centralNorm) { return 'Identical' }
if ($hasBase -and $null -ne $baseText) {
$baseNorm = Get-NormalizedText -Text $baseText
$localChanged = $localNorm -ne $baseNorm
$centralChanged = $centralNorm -ne $baseNorm
if ($localChanged -and -not $centralChanged) { return 'LocalChanged' }
if (-not $localChanged -and $centralChanged) { return 'CentralChanged' }
return 'Conflict'
}
# No base -- any difference is a conflict
return 'Conflict'
}
# Header comparison
$headerStatus = & $getStatus $Local.Header $Central.Header $(if ($hasBase) { $Base.Header } else { $null }) $hasBase
# Footer comparison
$footerStatus = & $getStatus $Local.Footer $Central.Footer $(if ($hasBase) { $Base.Footer } else { $null }) $hasBase
# Case comparison
$caseResults = [ordered]@{}
$allKeys = [System.Collections.ArrayList]::new()
foreach ($key in $Local.Cases.Keys) {
if (-not $allKeys.Contains($key)) { [void]$allKeys.Add($key) }
}
foreach ($key in $Central.Cases.Keys) {
if (-not $allKeys.Contains($key)) { [void]$allKeys.Add($key) }
}
foreach ($key in $allKeys) {
$inLocal = $Local.Cases.Contains($key)
$inCentral = $Central.Cases.Contains($key)
$inBase = $hasBase -and $Base.Cases.Contains($key)
if ($inLocal -and $inCentral) {
# Both have it
$baseText = if ($inBase) { $Base.Cases[$key] } else { $null }
$status = & $getStatus $Local.Cases[$key] $Central.Cases[$key] $baseText $hasBase
}
elseif ($inLocal -and -not $inCentral) {
if ($inBase) {
# Was in base, removed from central -- central deleted it
$status = 'CentralDeleted'
}
else {
$status = 'LocalOnly'
}
}
elseif (-not $inLocal -and $inCentral) {
if ($inBase) {
# Was in base, removed from local -- local deleted it
$status = 'LocalDeleted'
}
else {
$status = 'CentralOnly'
}
}
else {
continue # Should not happen
}
$caseResults[$key] = [PSCustomObject]@{
Key = $key
Status = $status
LocalText = if ($inLocal) { $Local.Cases[$key] } else { $null }
CentralText = if ($inCentral) { $Central.Cases[$key] } else { $null }
}
}
return [PSCustomObject]@{
HeaderStatus = $headerStatus
FooterStatus = $footerStatus
CaseResults = $caseResults
}
}
# ============================================================================
# Private: Interactive conflict resolution
# ============================================================================
function Resolve-MergeConflicts {
<#
.SYNOPSIS
Prompts admin to resolve each conflict interactively.
.OUTPUTS
Ordered dictionary of resolved case texts keyed by case name.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[System.Collections.Specialized.OrderedDictionary]$Conflicts
)
$resolved = [ordered]@{}
foreach ($key in $Conflicts.Keys) {
$conflict = $Conflicts[$key]
Write-Host ''
Write-Host ('=' * 70) -ForegroundColor Yellow
Write-Host " CONFLICT: `"$key`"" -ForegroundColor Yellow
Write-Host ('=' * 70) -ForegroundColor Yellow
Write-Host ''
# Show side-by-side diff of the lines that differ
$localLines = ($conflict.LocalText -split "`r?`n")
$centralLines = ($conflict.CentralText -split "`r?`n")
# Find differing lines
$maxLines = [Math]::Max($localLines.Count, $centralLines.Count)
$hasDiffs = $false
for ($i = 0; $i -lt $maxLines; $i++) {
$lLine = if ($i -lt $localLines.Count) { $localLines[$i] } else { '' }
$cLine = if ($i -lt $centralLines.Count) { $centralLines[$i] } else { '' }
if ($lLine.TrimEnd() -ne $cLine.TrimEnd()) {
if (-not $hasDiffs) {
Write-Host ' Differences:' -ForegroundColor White
$hasDiffs = $true
}
Write-Host " [LOCAL] $lLine" -ForegroundColor Green
Write-Host " [CENTRAL] $cLine" -ForegroundColor Cyan
Write-Host ''
}
}
if (-not $hasDiffs) {
Write-Host ' (Whitespace-only differences)' -ForegroundColor DarkGray
}
# Prompt for resolution
$choice = ''
while ($choice -notin @('L','C','S')) {
Write-Host ' Choose: [L]ocal [C]entral [S]kip (keep local, resolve later)' -ForegroundColor Yellow -NoNewline
$choice = (Read-Host ' ').Trim().ToUpper()
if ($choice -eq '') { $choice = 'S' }
}
switch ($choice) {
'L' {
$resolved[$key] = $conflict.LocalText
Write-Host " -> Keeping LOCAL version of `"$key`"" -ForegroundColor Green
}
'C' {
$resolved[$key] = $conflict.CentralText
Write-Host " -> Taking CENTRAL version of `"$key`"" -ForegroundColor Cyan
}
'S' {
$resolved[$key] = $conflict.LocalText
Write-Host " -> Skipped -- keeping LOCAL version (resolve manually later)" -ForegroundColor DarkYellow
}
}
}
return $resolved
}
# ============================================================================
# Private: Merge engine
# ============================================================================
function Merge-ParsedFiles {
<#
.SYNOPSIS
Merges two parsed files based on comparison results.
.DESCRIPTION
Returns a merged parsed object and summary statistics.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[PSCustomObject]$Local,
[Parameter(Mandatory)]
[PSCustomObject]$Central,
[Parameter(Mandatory)]
[PSCustomObject]$Comparison,
[Parameter()]
[string]$MergeDirection = 'Pull' # 'Pull' = central into local, 'Push' = local into central
)
# Start with a copy of the target side
if ($MergeDirection -eq 'Pull') {
$mergedHeader = $Local.Header
$mergedFooter = $Local.Footer
$mergedCases = [ordered]@{}
foreach ($key in $Local.Cases.Keys) {
$mergedCases[$key] = $Local.Cases[$key]
}
}
else {
$mergedHeader = $Central.Header
$mergedFooter = $Central.Footer
$mergedCases = [ordered]@{}
foreach ($key in $Central.Cases.Keys) {
$mergedCases[$key] = $Central.Cases[$key]
}
}
# Stats
$stats = @{
Identical = 0
AutoMerged = 0
NewEntries = 0
Conflicts = 0
Skipped = 0
Deleted = 0
}
# Collect conflicts for interactive resolution
$conflicts = [ordered]@{}
# Track new entries to append
$newEntries = [ordered]@{}
foreach ($key in $Comparison.CaseResults.Keys) {
$result = $Comparison.CaseResults[$key]
switch ($result.Status) {
'Identical' {
$stats.Identical++
}
'LocalChanged' {
if ($MergeDirection -eq 'Pull') {
# Pulling: local changed, keep local (already there)
$stats.Identical++
}
else {
# Pushing: local changed, update central
$mergedCases[$key] = $result.LocalText
$stats.AutoMerged++
}
}
'CentralChanged' {
if ($MergeDirection -eq 'Pull') {
# Pulling: central changed, take central
$mergedCases[$key] = $result.CentralText
$stats.AutoMerged++
}
else {
# Pushing: central changed, keep central (already there)
$stats.Identical++
}
}
'LocalOnly' {
if ($MergeDirection -eq 'Pull') {
# Pulling: local has a new entry, keep it
$stats.Identical++
}
else {
# Pushing: local has a new entry, add to central
$newEntries[$key] = $result.LocalText
$stats.NewEntries++
}
}
'CentralOnly' {
if ($MergeDirection -eq 'Pull') {
# Pulling: central has a new entry, add to local
$newEntries[$key] = $result.CentralText
$stats.NewEntries++
}
else {
# Pushing: central has an entry we dont have, keep it
$stats.Identical++
}
}
'CentralDeleted' {
if ($MergeDirection -eq 'Pull') {
# Central removed it -- remove from local
$mergedCases.Remove($key)
$stats.Deleted++
}
else {
$stats.Identical++
}
}
'LocalDeleted' {
if ($MergeDirection -eq 'Pull') {
$stats.Identical++
}
else {
# Local removed it -- remove from central
$mergedCases.Remove($key)
$stats.Deleted++
}
}
'Conflict' {
$conflicts[$key] = $result
$stats.Conflicts++
}
}
}
# --- Handle header/footer ---
if ($MergeDirection -eq 'Pull') {
if ($Comparison.HeaderStatus -eq 'CentralChanged') {
$mergedHeader = $Central.Header
}
elseif ($Comparison.HeaderStatus -eq 'Conflict') {
Write-Host ''
Write-Host ' HEADER CONFLICT -- the header section differs on both sides.' -ForegroundColor Yellow
Write-Host ' Review manually after merge completes.' -ForegroundColor Yellow
}
if ($Comparison.FooterStatus -eq 'CentralChanged') {
$mergedFooter = $Central.Footer
}
elseif ($Comparison.FooterStatus -eq 'Conflict') {
Write-Host ''
Write-Host ' FOOTER CONFLICT -- the footer/parameters section differs on both sides.' -ForegroundColor Yellow
Write-Host ' Review manually after merge completes.' -ForegroundColor Yellow
}
}
else {
if ($Comparison.HeaderStatus -eq 'LocalChanged') {
$mergedHeader = $Local.Header
}
elseif ($Comparison.HeaderStatus -eq 'Conflict') {
Write-Host ''
Write-Host ' HEADER CONFLICT -- the header section differs on both sides.' -ForegroundColor Yellow
Write-Host ' Review manually after merge completes.' -ForegroundColor Yellow
}
if ($Comparison.FooterStatus -eq 'LocalChanged') {
$mergedFooter = $Local.Footer
}
elseif ($Comparison.FooterStatus -eq 'Conflict') {
Write-Host ''
Write-Host ' FOOTER CONFLICT -- the footer/parameters section differs on both sides.' -ForegroundColor Yellow
Write-Host ' Review manually after merge completes.' -ForegroundColor Yellow
}
}
# --- Resolve conflicts interactively ---
if ($conflicts.Count -gt 0) {
$resolved = Resolve-MergeConflicts -Conflicts $conflicts
foreach ($key in $resolved.Keys) {
$mergedCases[$key] = $resolved[$key]
}
}
# --- Append new entries at end of cases ---
if ($newEntries.Count -gt 0) {
$sourceLabel = if ($MergeDirection -eq 'Pull') { 'central' } else { 'local' }
foreach ($key in $newEntries.Keys) {
$mergedCases[$key] = $newEntries[$key]
}
}
$merged = [PSCustomObject]@{
Header = $mergedHeader
Cases = $mergedCases
Footer = $mergedFooter
}
return [PSCustomObject]@{
Merged = $merged
Stats = $stats
}
}
# ============================================================================
# Private: Write file with ASCII validation
# ============================================================================
function Write-MainSwitchFile {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$Path,
[Parameter(Mandatory)]
[string]$Content
)
# Validate ASCII-only
$bytes = [System.Text.Encoding]::UTF8.GetBytes($Content)
foreach ($b in $bytes) {
if ($b -gt 127) {
Write-Warning "Non-ASCII byte detected (0x$($b.ToString('X2'))). File may contain Unicode characters."
Write-Warning "Proceeding with write, but verify the file in PowerShell ISE."
break
}
}
# Write with Default encoding (Windows-1252 on PS 5.1)
Set-Content -Path $Path -Value $Content -Encoding Default -NoNewline
}
# ============================================================================
# Public: Compare-MainSwitch (read-only diff)
# ============================================================================
function Compare-MainSwitch {
<#
.SYNOPSIS
Compares local and central Main-Switch.ps1 without making changes.
.DESCRIPTION
Parses both files, compares them case-by-case, and displays a color-coded
report showing what would change if you ran Receive-MainSwitch.
.PARAMETER LocalPath
Path to your local Main-Switch.ps1. Defaults to the path from Paths.txt.
.PARAMETER CentralPath
Path to the shared central Main-Switch.ps1. Defaults to M:\Share\VMT\Scripts\Main-Switch.ps1.
.EXAMPLE
Compare-MainSwitch
.EXAMPLE
Compare-MainSwitch -LocalPath "C:\MyScripts\Main-Switch.ps1" -CentralPath "\\Server\Share\Main-Switch.ps1"
#>
[CmdletBinding()]
param(
[Parameter()]
[string]$LocalPath,
[Parameter()]
[string]$CentralPath = 'M:\Share\VMT\Scripts\Main-Switch\Main-Switch.ps1'
)
# Resolve local path default
if (-not $LocalPath) {
$scriptDir = Get-DefaultScriptPath
if ($scriptDir) {
$LocalPath = Join-Path $scriptDir 'Main-Switch.ps1'
}
else {
throw 'Could not resolve local script path. Provide -LocalPath or run Setup.ps1 first.'
}
}
if (-not (Test-Path -LiteralPath $LocalPath)) {
throw "Local file not found: $LocalPath"
}
if (-not (Test-Path -LiteralPath $CentralPath)) {
throw "Central file not found: $CentralPath"
}
$local = ConvertTo-ParsedMainSwitch -Path $LocalPath
$central = ConvertTo-ParsedMainSwitch -Path $CentralPath
# Check for base file
$basePath = "$LocalPath.base"
$base = if (Test-Path -LiteralPath $basePath) {
ConvertTo-ParsedMainSwitch -Path $basePath
} else { $null }
$comparison = Compare-ParsedFiles -Local $local -Central $central -Base $base
# --- Display results ---
Write-Host ''
Write-Host ('-' * 60) -ForegroundColor White
Write-Host ' Main-Switch.ps1 Comparison Report' -ForegroundColor White
Write-Host ('-' * 60) -ForegroundColor White
if (-not $base) {
Write-Host ' NOTE: No base file found. This is likely the first comparison.' -ForegroundColor DarkGray
Write-Host ' All differences will show as conflicts until first sync.' -ForegroundColor DarkGray
}
Write-Host ''
# Header/Footer
if ($comparison.HeaderStatus -ne 'Identical') {
$color = switch ($comparison.HeaderStatus) {
'LocalChanged' { 'Green' }
'CentralChanged' { 'Cyan' }
'Conflict' { 'Yellow' }
}
Write-Host " HEADER: $($comparison.HeaderStatus)" -ForegroundColor $color
}
if ($comparison.FooterStatus -ne 'Identical') {
$color = switch ($comparison.FooterStatus) {
'LocalChanged' { 'Green' }
'CentralChanged' { 'Cyan' }
'Conflict' { 'Yellow' }
}
Write-Host " FOOTER: $($comparison.FooterStatus)" -ForegroundColor $color
}
# Cases
$counts = @{ Identical = 0; LocalChanged = 0; CentralChanged = 0; Conflict = 0; LocalOnly = 0; CentralOnly = 0; CentralDeleted = 0; LocalDeleted = 0 }
foreach ($key in $comparison.CaseResults.Keys) {
$result = $comparison.CaseResults[$key]
$counts[$result.Status]++
if ($result.Status -eq 'Identical') { continue }
$color = switch ($result.Status) {
'LocalChanged' { 'Green' }
'CentralChanged' { 'Cyan' }
'Conflict' { 'Yellow' }
'LocalOnly' { 'Green' }
'CentralOnly' { 'Cyan' }
'CentralDeleted' { 'Magenta' }
'LocalDeleted' { 'Magenta' }
}
$label = switch ($result.Status) {
'LocalChanged' { 'LOCAL changed' }
'CentralChanged' { 'CENTRAL changed' }
'Conflict' { 'CONFLICT (both changed)' }
'LocalOnly' { 'NEW (local only)' }
'CentralOnly' { 'NEW (central only)' }
'CentralDeleted' { 'DELETED from central' }
'LocalDeleted' { 'DELETED from local' }
}
Write-Host " $($key.PadRight(30)) $label" -ForegroundColor $color
}
# Summary
Write-Host ''
Write-Host ('-' * 60) -ForegroundColor White
Write-Host " Identical: $($counts.Identical)" -ForegroundColor DarkGray
Write-Host " Local changed: $($counts.LocalChanged)" -ForegroundColor Green
Write-Host " Central changed: $($counts.CentralChanged)" -ForegroundColor Cyan
Write-Host " Conflicts: $($counts.Conflict)" -ForegroundColor Yellow
Write-Host " New (local only): $($counts.LocalOnly)" -ForegroundColor Green
Write-Host " New (central): $($counts.CentralOnly)" -ForegroundColor Cyan
if ($counts.CentralDeleted -gt 0) {
Write-Host " Deleted (central):$($counts.CentralDeleted)" -ForegroundColor Magenta
}
if ($counts.LocalDeleted -gt 0) {
Write-Host " Deleted (local): $($counts.LocalDeleted)" -ForegroundColor Magenta
}
Write-Host ('-' * 60) -ForegroundColor White
Write-Host ''
}
# ============================================================================
# Public: Receive-MainSwitch (pull)
# ============================================================================
function Receive-MainSwitch {
<#
.SYNOPSIS
Pulls changes from the central Main-Switch.ps1 into your local copy.
.DESCRIPTION
Parses both files, compares them using three-way merge, auto-merges
non-conflicting changes, and prompts for conflict resolution.
Creates a timestamped backup before writing.
.PARAMETER LocalPath
Path to your local Main-Switch.ps1. Defaults to the path from Paths.txt.
.PARAMETER CentralPath
Path to the shared central Main-Switch.ps1. Defaults to M:\Share\VMT\Scripts\Main-Switch.ps1.
.PARAMETER NoBackup
Skip creating a backup of the local file before writing.
.PARAMETER RefreshBase
Overwrite the local .base file with the current central file and
exit without merging. Use this to recover from a stale base after
another admin ran Submit-MainSwitch -Initialize -Force. Your local
file is not touched.
.EXAMPLE
Receive-MainSwitch
.EXAMPLE
Receive-MainSwitch -CentralPath "\\OtherServer\Share\Main-Switch.ps1"
.EXAMPLE
Receive-MainSwitch -RefreshBase
#>
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter()]
[string]$LocalPath,
[Parameter()]
[string]$CentralPath = 'M:\Share\VMT\Scripts\Main-Switch\Main-Switch.ps1',
[Parameter()]
[switch]$NoBackup,
[Parameter()]
[switch]$RefreshBase
)
# Resolve local path default
if (-not $LocalPath) {
$scriptDir = Get-DefaultScriptPath
if ($scriptDir) {
$LocalPath = Join-Path $scriptDir 'Main-Switch.ps1'
}
else {
throw 'Could not resolve local script path. Provide -LocalPath or run Setup.ps1 first.'
}
}
if (-not (Test-Path -LiteralPath $LocalPath)) {
throw "Local file not found: $LocalPath"
}
# --- RefreshBase mode: overwrite local base with central, no merge ---
if ($RefreshBase) {
if (-not (Test-Path -LiteralPath $CentralPath)) {
throw "Central file not found: $CentralPath"
}
$basePath = "$LocalPath.base"
Write-Host ''
Write-Host ('-' * 60) -ForegroundColor White
Write-Host ' Receive-MainSwitch -RefreshBase: Syncing base to central' -ForegroundColor White
Write-Host ('-' * 60) -ForegroundColor White
Write-Host ''
Write-Host " Central: $CentralPath" -ForegroundColor Cyan
Write-Host " Base: $basePath" -ForegroundColor Cyan
Write-Host ''
if ($PSCmdlet.ShouldProcess($basePath, 'Overwrite base file with central')) {
Copy-Item -LiteralPath $CentralPath -Destination $basePath -Force
Write-Host " Base file refreshed: $basePath" -ForegroundColor Green
Write-Host ' Local file unchanged. Submit-MainSwitch should no longer be blocked.' -ForegroundColor DarkGray
}
Write-Host ''
return
}
# Try reading central with error handling for file locks
try {
if (-not (Test-Path -LiteralPath $CentralPath)) {
throw "Central file not found: $CentralPath"
}
$central = ConvertTo-ParsedMainSwitch -Path $CentralPath
}
catch [System.IO.IOException] {
throw "Central file is locked by another user. Try again later. Details: $($_.Exception.Message)"
}
$local = ConvertTo-ParsedMainSwitch -Path $LocalPath
# Check for base file
$basePath = "$LocalPath.base"
$base = if (Test-Path -LiteralPath $basePath) {
ConvertTo-ParsedMainSwitch -Path $basePath
} else { $null }
Write-Host ''
Write-Host ('-' * 60) -ForegroundColor White
Write-Host ' Receive-MainSwitch: Merging central changes into local' -ForegroundColor White
Write-Host ('-' * 60) -ForegroundColor White
if (-not $base) {
Write-Host ' First sync -- no base file found. All differences will be flagged as conflicts.' -ForegroundColor DarkYellow
Write-Host ' After this sync, a base file will be created for future three-way merges.' -ForegroundColor DarkGray
}
Write-Host ''
$comparison = Compare-ParsedFiles -Local $local -Central $central -Base $base
$mergeResult = Merge-ParsedFiles -Local $local -Central $central -Comparison $comparison -MergeDirection 'Pull'
$stats = $mergeResult.Stats
$totalChanges = $stats.AutoMerged + $stats.NewEntries + $stats.Conflicts + $stats.Deleted
if ($totalChanges -eq 0) {
Write-Host ' Already up to date -- no changes needed.' -ForegroundColor Green
Write-Host ''
# Still create base if it does not exist
if (-not (Test-Path -LiteralPath $basePath)) {
if ($PSCmdlet.ShouldProcess($basePath, 'Create base file for future syncs')) {
Copy-Item -LiteralPath $CentralPath -Destination $basePath -Force
Write-Host " Base file created: $basePath" -ForegroundColor DarkGray
}
}
elseif ($null -ne $base) {
# Self-heal: local matches central but base may be stale (for example,
# another admin ran Submit-MainSwitch -Initialize -Force and overwrote
# central). Without this, Submit-MainSwitch would stay blocked because
# its publish check compares base vs central as whole files.
$centralNorm = Get-NormalizedText -Text (ConvertFrom-ParsedMainSwitch -Parsed $central)
$baseNorm = Get-NormalizedText -Text (ConvertFrom-ParsedMainSwitch -Parsed $base)
if ($centralNorm -ne $baseNorm) {
if ($PSCmdlet.ShouldProcess($basePath, 'Refresh stale base file to match central')) {
Copy-Item -LiteralPath $CentralPath -Destination $basePath -Force
Write-Host " Base file refreshed (was stale vs central): $basePath" -ForegroundColor DarkGray
}
}
}
return
}
# Display summary
Write-Host ''
Write-Host ('-' * 60) -ForegroundColor White
Write-Host ' Merge Summary:' -ForegroundColor White
Write-Host " Auto-merged: $($stats.AutoMerged) case(s)" -ForegroundColor Cyan
Write-Host " New entries: $($stats.NewEntries) case(s)" -ForegroundColor Cyan
Write-Host " Conflicts: $($stats.Conflicts) case(s)" -ForegroundColor Yellow
Write-Host " Deleted: $($stats.Deleted) case(s)" -ForegroundColor Magenta
Write-Host ('-' * 60) -ForegroundColor White
Write-Host ''
# Write merged file
$mergedContent = ConvertFrom-ParsedMainSwitch -Parsed $mergeResult.Merged
if ($PSCmdlet.ShouldProcess($LocalPath, 'Write merged Main-Switch.ps1')) {
# Backup
if (-not $NoBackup) {
$timestamp = Get-Date -Format 'yyyy-MM-dd_HHmmss'
$backupDir = Join-Path (Split-Path $LocalPath -Parent) 'Patching\SwitchBackups'
$backupName = "Main-Switch_$timestamp.bak"
$backupPath = Join-Path $backupDir $backupName
New-Item -ItemType Directory -Force -Path $backupDir | Out-Null
Copy-Item -LiteralPath $LocalPath -Destination $backupPath -Force
Write-Host " Backup created: $backupPath" -ForegroundColor DarkGray
}
Write-MainSwitchFile -Path $LocalPath -Content $mergedContent
# Update base file to match central
Copy-Item -LiteralPath $CentralPath -Destination $basePath -Force
Write-Host " Local file updated: $LocalPath" -ForegroundColor Green