278 lines
8.8 KiB
PowerShell
278 lines
8.8 KiB
PowerShell
#Requires -Version 5.1
|
|
param(
|
|
[Parameter(Mandatory=$true)]
|
|
[string]$Path
|
|
)
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
Add-Type -AssemblyName System.Windows.Forms
|
|
Add-Type -AssemblyName System.Drawing
|
|
|
|
function Get-Config {
|
|
$configPaths = @(
|
|
"$env:APPDATA\FreigabeKontextmenue\config.json",
|
|
"$env:ProgramData\FreigabeKontextmenue\config.json"
|
|
)
|
|
|
|
foreach ($configPath in $configPaths) {
|
|
if (Test-Path $configPath) {
|
|
return Get-Content $configPath -Raw | ConvertFrom-Json
|
|
}
|
|
}
|
|
|
|
throw "Konfiguration nicht gefunden. Bitte zuerst installieren!"
|
|
}
|
|
|
|
function Get-FreigabeInfo {
|
|
param([string]$TargetPath)
|
|
|
|
$metaDirs = @(
|
|
"$env:ProgramData\FreigabeKontextmenue\meta",
|
|
"$env:APPDATA\FreigabeKontextmenue\meta"
|
|
)
|
|
|
|
$pathHash = [System.BitConverter]::ToString([System.Security.Cryptography.MD5]::Create().ComputeHash([System.Text.Encoding]::UTF8.GetBytes($TargetPath))).Replace("-","")
|
|
|
|
foreach ($metaDir in $metaDirs) {
|
|
$metaFile = Join-Path $metaDir "$pathHash.json"
|
|
if (Test-Path $metaFile) {
|
|
return Get-Content $metaFile -Raw | ConvertFrom-Json
|
|
}
|
|
}
|
|
|
|
return $null
|
|
}
|
|
|
|
function Save-FreigabeInfo {
|
|
param(
|
|
[string]$TargetPath,
|
|
[array]$Users
|
|
)
|
|
|
|
$metaDir = if (Test-Path "$env:ProgramData\FreigabeKontextmenue") {
|
|
"$env:ProgramData\FreigabeKontextmenue\meta"
|
|
} else {
|
|
"$env:APPDATA\FreigabeKontextmenue\meta"
|
|
}
|
|
|
|
$pathHash = [System.BitConverter]::ToString([System.Security.Cryptography.MD5]::Create().ComputeHash([System.Text.Encoding]::UTF8.GetBytes($TargetPath))).Replace("-","")
|
|
$metaFile = Join-Path $metaDir "$pathHash.json"
|
|
|
|
if ($Users.Count -eq 0) {
|
|
if (Test-Path $metaFile) {
|
|
Remove-Item $metaFile -Force
|
|
}
|
|
} else {
|
|
$meta = @{
|
|
TargetPath = $TargetPath
|
|
Users = $Users
|
|
}
|
|
$meta | ConvertTo-Json | Set-Content -Path $metaFile -Encoding UTF8
|
|
}
|
|
}
|
|
|
|
function Show-UserRemoveDialog {
|
|
param([array]$Users)
|
|
|
|
$form = New-Object System.Windows.Forms.Form
|
|
$form.Text = "Freigabe entfernen - Benutzer auswählen"
|
|
$form.Size = New-Object System.Drawing.Size(400, 400)
|
|
$form.StartPosition = "CenterScreen"
|
|
$form.FormBorderStyle = "FixedDialog"
|
|
$form.MaximizeBox = $false
|
|
|
|
$label = New-Object System.Windows.Forms.Label
|
|
$label.Text = "Freigabe entfernen für folgende Benutzer:"
|
|
$label.Location = New-Object System.Drawing.Point(10, 15)
|
|
$label.Size = New-Object System.Drawing.Size(360, 20)
|
|
$form.Controls.Add($label)
|
|
|
|
$checkedListBox = New-Object System.Windows.Forms.CheckedListBox
|
|
$checkedListBox.Location = New-Object System.Drawing.Point(10, 40)
|
|
$checkedListBox.Size = New-Object System.Drawing.Size(360, 260)
|
|
$checkedListBox.Font = New-Object System.Drawing.Font("Segoe UI", 10)
|
|
$checkedListBox.CheckOnClick = $true
|
|
$form.Controls.Add($checkedListBox)
|
|
|
|
foreach ($user in $Users) {
|
|
$checkedListBox.Items.Add($user) | Out-Null
|
|
}
|
|
|
|
$selectAllBtn = New-Object System.Windows.Forms.Button
|
|
$selectAllBtn.Text = "Alle auswählen"
|
|
$selectAllBtn.Location = New-Object System.Drawing.Point(10, 310)
|
|
$selectAllBtn.Size = New-Object System.Drawing.Size(100, 25)
|
|
$selectAllBtn.Add_Click({
|
|
for ($i = 0; $i -lt $checkedListBox.Items.Count; $i++) {
|
|
$checkedListBox.SetItemChecked($i, $true)
|
|
}
|
|
})
|
|
$form.Controls.Add($selectAllBtn)
|
|
|
|
$okBtn = New-Object System.Windows.Forms.Button
|
|
$okBtn.Text = "Entfernen"
|
|
$okBtn.Location = New-Object System.Drawing.Point(190, 310)
|
|
$okBtn.Size = New-Object System.Drawing.Size(85, 30)
|
|
$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(285, 310)
|
|
$cancelBtn.Size = New-Object System.Drawing.Size(85, 30)
|
|
$cancelBtn.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
|
|
$form.CancelButton = $cancelBtn
|
|
$form.Controls.Add($cancelBtn)
|
|
|
|
$script:selectedUsers = @()
|
|
|
|
if ($form.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
|
|
$script:selectedUsers = @($checkedListBox.CheckedItems)
|
|
}
|
|
|
|
return $script:selectedUsers
|
|
}
|
|
|
|
function Remove-FreigabePermission {
|
|
param(
|
|
[string]$TargetPath,
|
|
[string]$QualifiedUserName
|
|
)
|
|
|
|
try {
|
|
$acl = Get-Acl -Path $TargetPath
|
|
|
|
$rulesToRemove = $acl.Access | Where-Object {
|
|
$_.IdentityReference.Value -eq $QualifiedUserName
|
|
}
|
|
|
|
foreach ($rule in $rulesToRemove) {
|
|
$acl.RemoveAccessRule($rule) | Out-Null
|
|
}
|
|
|
|
Set-Acl -Path $TargetPath -AclObject $acl
|
|
return $true
|
|
} catch {
|
|
Write-Warning "Fehler beim Entfernen der Berechtigung für $QualifiedUserName : $_"
|
|
return $false
|
|
}
|
|
}
|
|
|
|
function Remove-UserShortcut {
|
|
param(
|
|
[string]$TargetPath,
|
|
[string]$QualifiedUserName,
|
|
[string]$BasePath
|
|
)
|
|
|
|
# SamAccountName aus QualifiedName extrahieren (für Ordnerpfad)
|
|
$samAccountName = if ($QualifiedUserName -match '\\') {
|
|
$QualifiedUserName.Split('\')[1]
|
|
} else {
|
|
$QualifiedUserName
|
|
}
|
|
|
|
$userFolder = Join-Path $BasePath $samAccountName
|
|
|
|
if (-not (Test-Path $userFolder)) {
|
|
return
|
|
}
|
|
|
|
$shortcuts = Get-ChildItem -Path $userFolder -Filter "*.lnk" -ErrorAction SilentlyContinue
|
|
$shell = New-Object -ComObject WScript.Shell
|
|
|
|
foreach ($shortcut in $shortcuts) {
|
|
try {
|
|
$lnk = $shell.CreateShortcut($shortcut.FullName)
|
|
$shortcutTarget = $lnk.TargetPath
|
|
|
|
$targetNormalized = $TargetPath.TrimEnd('\').ToLower()
|
|
$shortcutTargetNormalized = $shortcutTarget.TrimEnd('\').ToLower()
|
|
|
|
$targetName = Split-Path $TargetPath -Leaf
|
|
$shortcutTargetName = Split-Path $shortcutTarget -Leaf
|
|
|
|
if ($shortcutTargetNormalized -eq $targetNormalized -or
|
|
($targetName.ToLower() -eq $shortcutTargetName.ToLower() -and
|
|
$shortcutTarget -match [regex]::Escape($targetName))) {
|
|
Remove-Item -Path $shortcut.FullName -Force
|
|
}
|
|
} catch {
|
|
Write-Warning "Fehler beim Prüfen der Verknüpfung: $($shortcut.Name)"
|
|
}
|
|
}
|
|
|
|
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($shell) | Out-Null
|
|
|
|
$remainingItems = Get-ChildItem -Path $userFolder -ErrorAction SilentlyContinue
|
|
if ($remainingItems.Count -eq 0) {
|
|
Remove-Item -Path $userFolder -Force -ErrorAction SilentlyContinue
|
|
}
|
|
}
|
|
|
|
try {
|
|
$config = Get-Config
|
|
$basePath = $config.LinkBasePath
|
|
|
|
if (-not (Test-Path $Path)) {
|
|
throw "Der Pfad existiert nicht: $Path"
|
|
}
|
|
|
|
$freigabeInfo = Get-FreigabeInfo -TargetPath $Path
|
|
|
|
if (-not $freigabeInfo -or $freigabeInfo.Users.Count -eq 0) {
|
|
[System.Windows.Forms.MessageBox]::Show(
|
|
"Für dieses Element wurden keine Freigaben über das Kontextmenü erstellt.",
|
|
"Keine Freigaben",
|
|
[System.Windows.Forms.MessageBoxButtons]::OK,
|
|
[System.Windows.Forms.MessageBoxIcon]::Information
|
|
)
|
|
exit 0
|
|
}
|
|
|
|
$usersToRemove = Show-UserRemoveDialog -Users $freigabeInfo.Users
|
|
|
|
if ($usersToRemove.Count -eq 0) {
|
|
exit 0
|
|
}
|
|
|
|
$removedUsers = @()
|
|
$errors = @()
|
|
|
|
foreach ($qualifiedUserName in $usersToRemove) {
|
|
try {
|
|
Remove-FreigabePermission -TargetPath $Path -QualifiedUserName $qualifiedUserName
|
|
Remove-UserShortcut -TargetPath $Path -QualifiedUserName $qualifiedUserName -BasePath $basePath
|
|
$removedUsers += $qualifiedUserName
|
|
} catch {
|
|
$errors += "Fehler bei $qualifiedUserName : $_"
|
|
}
|
|
}
|
|
|
|
$remainingUsers = @($freigabeInfo.Users | Where-Object { $_ -notin $removedUsers })
|
|
Save-FreigabeInfo -TargetPath $Path -Users $remainingUsers
|
|
|
|
$message = "Freigaben entfernt für:`n" + ($removedUsers -join "`n")
|
|
|
|
if ($errors.Count -gt 0) {
|
|
$message += "`n`nFehler:`n" + ($errors -join "`n")
|
|
}
|
|
|
|
[System.Windows.Forms.MessageBox]::Show(
|
|
$message,
|
|
"Freigaben entfernt",
|
|
[System.Windows.Forms.MessageBoxButtons]::OK,
|
|
$(if ($errors.Count -gt 0) { [System.Windows.Forms.MessageBoxIcon]::Warning } else { [System.Windows.Forms.MessageBoxIcon]::Information })
|
|
)
|
|
|
|
} catch {
|
|
[System.Windows.Forms.MessageBox]::Show(
|
|
"Fehler: $($_.Exception.Message)",
|
|
"Fehler",
|
|
[System.Windows.Forms.MessageBoxButtons]::OK,
|
|
[System.Windows.Forms.MessageBoxIcon]::Error
|
|
)
|
|
exit 1
|
|
}
|