#Requires -Version 5.1 <# .SYNOPSIS Installiert/Deinstalliert Kontextmenü-Einträge für Datei-/Ordnerfreigaben .DESCRIPTION Erstellt Kontextmenü-Einträge "Freigeben für Mitarbeiter" und "Freigabe entfernen" .PARAMETER Install Installiert die Kontextmenü-Einträge .PARAMETER Uninstall Entfernt die Kontextmenü-Einträge (Freigaben bleiben erhalten) .PARAMETER AllUsers Installation für alle Benutzer (erfordert Admin-Rechte) .PARAMETER CurrentUser Installation nur für aktuellen Benutzer .PARAMETER LinkBasePath Stammverzeichnis für Benutzer-Verknüpfungsordner #> param( [switch]$Install, [switch]$Uninstall, [switch]$AllUsers, [switch]$CurrentUser, [string]$LinkBasePath ) $ErrorActionPreference = "Stop" function Test-Administrator { $currentUser = [Security.Principal.WindowsIdentity]::GetCurrent() $principal = New-Object Security.Principal.WindowsPrincipal($currentUser) return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) } function Get-ScriptDirectory { return $PSScriptRoot } function Show-Menu { Clear-Host Write-Host "========================================" -ForegroundColor Cyan Write-Host " Freigabe-Kontextmen$([char]0x00FC) Installation" -ForegroundColor Cyan Write-Host "========================================" -ForegroundColor Cyan Write-Host "" Write-Host "[1] Installieren f$([char]0x00FC)r aktuellen Benutzer" Write-Host "[2] Installieren f$([char]0x00FC)r alle Benutzer (Admin erforderlich)" Write-Host "[3] Deinstallieren f$([char]0x00FC)r aktuellen Benutzer" Write-Host "[4] Deinstallieren f$([char]0x00FC)r alle Benutzer (Admin erforderlich)" Write-Host "[Q] Beenden" Write-Host "" } function Get-LinkBasePath { Add-Type -AssemblyName System.Windows.Forms $form = New-Object System.Windows.Forms.Form $form.Text = "Stammverzeichnis f$([char]0x00FC)r Verkn$([char]0x00FC)pfungen" $form.Size = New-Object System.Drawing.Size(500, 180) $form.StartPosition = "CenterScreen" $form.FormBorderStyle = "FixedDialog" $form.MaximizeBox = $false $label = New-Object System.Windows.Forms.Label $label.Text = "Stammverzeichnis f$([char]0x00FC)r Benutzer-Verkn$([char]0x00FC)pfungsordner:" $label.Location = New-Object System.Drawing.Point(10, 20) $label.Size = New-Object System.Drawing.Size(460, 20) $form.Controls.Add($label) $textBox = New-Object System.Windows.Forms.TextBox $textBox.Location = New-Object System.Drawing.Point(10, 45) $textBox.Size = New-Object System.Drawing.Size(380, 20) $textBox.Text = "C:\Freigaben\Benutzer" $form.Controls.Add($textBox) $browseBtn = New-Object System.Windows.Forms.Button $browseBtn.Text = "..." $browseBtn.Location = New-Object System.Drawing.Point(395, 43) $browseBtn.Size = New-Object System.Drawing.Size(75, 25) $browseBtn.Add_Click({ $folderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog $folderBrowser.Description = "Stammverzeichnis ausw$([char]0x00E4)hlen" if ($folderBrowser.ShowDialog() -eq "OK") { $textBox.Text = $folderBrowser.SelectedPath } }) $form.Controls.Add($browseBtn) $okBtn = New-Object System.Windows.Forms.Button $okBtn.Text = "OK" $okBtn.Location = New-Object System.Drawing.Point(310, 100) $okBtn.Size = New-Object System.Drawing.Size(75, 25) $okBtn.DialogResult = [System.Windows.Forms.DialogResult]::OK $form.AcceptButton = $okBtn $form.Controls.Add($okBtn) $cancelBtn = New-Object System.Windows.Forms.Button $cancelBtn.Text = "Abbrechen" $cancelBtn.Location = New-Object System.Drawing.Point(395, 100) $cancelBtn.Size = New-Object System.Drawing.Size(75, 25) $cancelBtn.DialogResult = [System.Windows.Forms.DialogResult]::Cancel $form.CancelButton = $cancelBtn $form.Controls.Add($cancelBtn) $result = $form.ShowDialog() if ($result -eq [System.Windows.Forms.DialogResult]::OK) { return $textBox.Text } return $null } function Install-ContextMenu { param( [bool]$ForAllUsers, [string]$BasePath ) $scriptDir = Get-ScriptDirectory $freigabeScript = Join-Path $scriptDir "Freigabe-Hinzufuegen.ps1" $entfernenScript = Join-Path $scriptDir "Freigabe-Entfernen.ps1" if ($ForAllUsers) { if (-not (Test-Administrator)) { throw "Admin-Rechte erforderlich f$([char]0x00FC)r Installation f$([char]0x00FC)r alle Benutzer!" } $regBase = "HKLM:\SOFTWARE\Classes" $configPath = "$env:ProgramData\FreigabeKontextmenue" } else { $regBase = "HKCU:\SOFTWARE\Classes" $configPath = "$env:APPDATA\FreigabeKontextmenue" } if (-not (Test-Path $configPath)) { New-Item -Path $configPath -ItemType Directory -Force | Out-Null } $config = @{ LinkBasePath = $BasePath InstalledFor = if ($ForAllUsers) { "AllUsers" } else { "CurrentUser" } InstallDate = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss") ScriptDirectory = $scriptDir } $config | ConvertTo-Json | Set-Content -Path (Join-Path $configPath "config.json") -Encoding UTF8 $shellExTypes = @( "*\shell", "Directory\shell", "Directory\Background\shell" ) foreach ($shellType in $shellExTypes) { $freigabeKey = "$regBase\$shellType\FreigabeFuerMitarbeiter" $entfernenKey = "$regBase\$shellType\FreigabeEntfernen" if (-not (Test-Path $freigabeKey)) { New-Item -Path $freigabeKey -Force | Out-Null } Set-ItemProperty -Path $freigabeKey -Name "(Default)" -Value "Freigeben f$([char]0x00FC)r Mitarbeiter" Set-ItemProperty -Path $freigabeKey -Name "Icon" -Value "imageres.dll,232" $freigabeCmd = "$freigabeKey\command" if (-not (Test-Path $freigabeCmd)) { New-Item -Path $freigabeCmd -Force | Out-Null } if ($shellType -eq "Directory\Background\shell") { Set-ItemProperty -Path $freigabeCmd -Name "(Default)" -Value "powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -File `"$freigabeScript`" -Path `"%V`"" } else { Set-ItemProperty -Path $freigabeCmd -Name "(Default)" -Value "powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -File `"$freigabeScript`" -Path `"%1`"" } if (-not (Test-Path $entfernenKey)) { New-Item -Path $entfernenKey -Force | Out-Null } Set-ItemProperty -Path $entfernenKey -Name "(Default)" -Value "Freigabe f$([char]0x00FC)r Mitarbeiter entfernen" Set-ItemProperty -Path $entfernenKey -Name "Icon" -Value "imageres.dll,229" $entfernenCmd = "$entfernenKey\command" if (-not (Test-Path $entfernenCmd)) { New-Item -Path $entfernenCmd -Force | Out-Null } if ($shellType -eq "Directory\Background\shell") { Set-ItemProperty -Path $entfernenCmd -Name "(Default)" -Value "powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -File `"$entfernenScript`" -Path `"%V`"" } else { Set-ItemProperty -Path $entfernenCmd -Name "(Default)" -Value "powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -File `"$entfernenScript`" -Path `"%1`"" } } if (-not (Test-Path $BasePath)) { New-Item -Path $BasePath -ItemType Directory -Force | Out-Null Write-Host "Stammverzeichnis erstellt: $BasePath" -ForegroundColor Green } Write-Host "Installation erfolgreich abgeschlossen!" -ForegroundColor Green } function Uninstall-ContextMenu { param( [bool]$ForAllUsers ) if ($ForAllUsers) { if (-not (Test-Administrator)) { throw "Admin-Rechte erforderlich f$([char]0x00FC)r Deinstallation f$([char]0x00FC)r alle Benutzer!" } $regBase = "HKLM:\SOFTWARE\Classes" $configPath = "$env:ProgramData\FreigabeKontextmenue" } else { $regBase = "HKCU:\SOFTWARE\Classes" $configPath = "$env:APPDATA\FreigabeKontextmenue" } $shellExTypes = @( "*\shell", "Directory\shell", "Directory\Background\shell" ) foreach ($shellType in $shellExTypes) { $freigabeKey = "$regBase\$shellType\FreigabeFuerMitarbeiter" $entfernenKey = "$regBase\$shellType\FreigabeEntfernen" if (Test-Path $freigabeKey) { Remove-Item -Path $freigabeKey -Recurse -Force } if (Test-Path $entfernenKey) { Remove-Item -Path $entfernenKey -Recurse -Force } } if (Test-Path $configPath) { Remove-Item -Path $configPath -Recurse -Force } Write-Host "Deinstallation erfolgreich!" -ForegroundColor Green Write-Host "Hinweis: Bestehende Freigaben und Verkn$([char]0x00FC)pfungen wurden NICHT entfernt." -ForegroundColor Yellow } if ($Install -or $Uninstall) { if ($Install) { $forAll = $AllUsers -and -not $CurrentUser if (-not $LinkBasePath) { $LinkBasePath = Get-LinkBasePath if (-not $LinkBasePath) { Write-Host "Installation abgebrochen." -ForegroundColor Yellow exit 1 } } Install-ContextMenu -ForAllUsers $forAll -BasePath $LinkBasePath } if ($Uninstall) { $forAll = $AllUsers -and -not $CurrentUser Uninstall-ContextMenu -ForAllUsers $forAll } } else { do { Show-Menu $choice = Read-Host "Auswahl" switch ($choice.ToUpper()) { "1" { $basePath = Get-LinkBasePath if ($basePath) { Install-ContextMenu -ForAllUsers $false -BasePath $basePath } pause } "2" { if (-not (Test-Administrator)) { Write-Host "Bitte als Administrator ausf$([char]0x00FC)hren!" -ForegroundColor Red } else { $basePath = Get-LinkBasePath if ($basePath) { Install-ContextMenu -ForAllUsers $true -BasePath $basePath } } pause } "3" { Uninstall-ContextMenu -ForAllUsers $false pause } "4" { if (-not (Test-Administrator)) { Write-Host "Bitte als Administrator ausf$([char]0x00FC)hren!" -ForegroundColor Red } else { Uninstall-ContextMenu -ForAllUsers $true } pause } } } while ($choice.ToUpper() -ne "Q") }