From 2490f3e2af849c3aa55c7bc034991d79a9a3c3cb Mon Sep 17 00:00:00 2001 From: Marco Wende Date: Wed, 10 Jun 2026 21:57:55 +0200 Subject: [PATCH] Add DiagnosticViewModel.ps1 --- MVVM/ViewModels/DiagnosticViewModel.ps1 | 113 ++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 MVVM/ViewModels/DiagnosticViewModel.ps1 diff --git a/MVVM/ViewModels/DiagnosticViewModel.ps1 b/MVVM/ViewModels/DiagnosticViewModel.ps1 new file mode 100644 index 0000000..983c487 --- /dev/null +++ b/MVVM/ViewModels/DiagnosticViewModel.ps1 @@ -0,0 +1,113 @@ +class DiagnosticViewModel : FirefoxAddinManager.ObservableBase { + + [System.Collections.ObjectModel.ObservableCollection[PolicyCheckModel]] $PolicyChecks + [System.Windows.Input.ICommand] $RunCheckCommand + [System.Windows.Input.ICommand] $FixSelectedCommand + [System.Windows.Input.ICommand] $FixAllErrorsCommand + [System.Windows.Input.ICommand] $OpenAboutPoliciesCommand + [System.Windows.Input.ICommand] $ImportRegCommand + + hidden [PolicyCheckModel] $_selectedCheck = $null + hidden [string] $_statusMessage = "" + hidden [bool] $_isIntuneManaged = $false + + [PolicyCheckModel] $SelectedCheck { + get { return $this._selectedCheck } + set { $this._selectedCheck = $value; $this.OnPropertyChanged("SelectedCheck") } + } + + [string] $StatusMessage { + get { return $this._statusMessage } + set { $this._statusMessage = $value; $this.OnPropertyChanged("StatusMessage") } + } + + [bool] $IsIntuneManaged { + get { return $this._isIntuneManaged } + set { $this._isIntuneManaged = $value; $this.OnPropertyChanged("IsIntuneManaged") } + } + + DiagnosticViewModel() { + $this.PolicyChecks = [System.Collections.ObjectModel.ObservableCollection[PolicyCheckModel]]::new() + $this.IsIntuneManaged = [FirefoxService]::IsIntuneManaged() + $this._InitCommands() + } + + hidden [void] _InitCommands() { + $vm = $this + + $this.RunCheckCommand = [FirefoxAddinManager.RelayCommand]::new({ + $vm.PolicyChecks.Clear() + [PolicyService]::RunChecks() | ForEach-Object { $vm.PolicyChecks.Add($_) } + $errors = ($vm.PolicyChecks | Where-Object { $_.Status -eq "FEHLER" }).Count + $vm.StatusMessage = if ($errors -gt 0) { "$errors Fehler gefunden." } else { "Keine Fehler gefunden." } + }, { $true }) + + $this.FixSelectedCommand = [FirefoxAddinManager.RelayCommand]::new({ + if (-not $vm.SelectedCheck) { return } + if (-not [FirefoxService]::IsAdmin()) { + [System.Windows.MessageBox]::Show("Administratorrechte erforderlich.", "Kein Admin", "OK", "Warning") + return + } + [PolicyService]::FixError($vm.SelectedCheck) + $vm.RunCheckCommand.Execute($null) + $vm.StatusMessage = "Korrektur angewendet. Firefox neu starten." + }, { return $null -ne $this.SelectedCheck -and $this.SelectedCheck.Status -eq "FEHLER" }) + + $this.FixAllErrorsCommand = [FirefoxAddinManager.RelayCommand]::new({ + if (-not [FirefoxService]::IsAdmin()) { + [System.Windows.MessageBox]::Show("Administratorrechte erforderlich.", "Kein Admin", "OK", "Warning") + return + } + $errors = $vm.PolicyChecks | Where-Object { $_.Status -eq "FEHLER" } + foreach ($e in $errors) { [PolicyService]::FixError($e) } + $vm.RunCheckCommand.Execute($null) + $vm.StatusMessage = "$($errors.Count) Fehler behoben. Firefox neu starten." + }, { return ($this.PolicyChecks | Where-Object { $_.Status -eq "FEHLER" }).Count -gt 0 }) + + $this.OpenAboutPoliciesCommand = [FirefoxAddinManager.RelayCommand]::new({ + try { Start-Process "firefox" "about:policies" } + catch { [System.Windows.MessageBox]::Show("Firefox konnte nicht gestartet werden.", "Fehler", "OK", "Error") } + }, { $true }) + + $this.ImportRegCommand = [FirefoxAddinManager.RelayCommand]::new({ + if (-not [FirefoxService]::IsAdmin()) { + [System.Windows.MessageBox]::Show("Administratorrechte erforderlich.", "Kein Admin", "OK", "Warning") + return + } + $dlg = New-Object Microsoft.Win32.OpenFileDialog + $dlg.Filter = "Registry-Datei (*.reg)|*.reg" + if (-not $dlg.ShowDialog()) { return } + + $lines = Get-Content $dlg.FileName -Encoding Unicode -ErrorAction SilentlyContinue + if (-not $lines) { $lines = Get-Content $dlg.FileName -Encoding UTF8 -ErrorAction SilentlyContinue } + + $currentKey = $null; $applied = 0; $skipped = 0 + foreach ($line in $lines) { + $line = $line.Trim() + if ($line -eq "" -or $line.StartsWith(";") -or $line.StartsWith("Windows")) { continue } + if ($line -match '^\[(.+)\]$') { + $rawKey = $Matches[1] + if ($rawKey -match 'SOFTWARE\\Policies\\Mozilla\\Firefox') { + $currentKey = $rawKey -replace '^HKEY_LOCAL_MACHINE','HKLM:' -replace '^HKEY_CURRENT_USER','HKCU:' + } else { $currentKey = $null; $skipped++ } + continue + } + if (-not $currentKey) { continue } + if ($line -match '^"(.+)"=dword:(.+)$') { + if (-not (Test-Path $currentKey)) { New-Item -Path $currentKey -Force | Out-Null } + Set-ItemProperty $currentKey -Name $Matches[1] -Value ([Convert]::ToInt32($Matches[2],16)) -Type DWord + $applied++ + } elseif ($line -match '^"(.+)"="(.*)"$') { + if (-not (Test-Path $currentKey)) { New-Item -Path $currentKey -Force | Out-Null } + Set-ItemProperty $currentKey -Name $Matches[1] -Value ($Matches[2] -replace '\\"','"') -Type String + $applied++ + } elseif ($line -match '^"-(.+)"$') { + Remove-ItemProperty $currentKey -Name $Matches[1] -ErrorAction SilentlyContinue + $applied++ + } + } + $vm.RunCheckCommand.Execute($null) + $vm.StatusMessage = "REG-Import: $applied Werte gesetzt, $skipped Keys uebersprungen. Firefox neu starten." + }, { $true }) + } +}