diff --git a/Freigabe-Hinzufuegen.ps1 b/Freigabe-Hinzufuegen.ps1 index ebc0895..4389d05 100644 --- a/Freigabe-Hinzufuegen.ps1 +++ b/Freigabe-Hinzufuegen.ps1 @@ -26,43 +26,54 @@ function Get-Config { function Get-UNCPath { param([string]$LocalPath) + # DEBUG + $debugFile = "$env:TEMP\freigabe_unc_debug.txt" + "Input: $LocalPath" | Out-File $debugFile + # Wenn der Pfad bereits ein UNC-Pfad ist, direkt zurueckgeben if ($LocalPath -match '^\\\\') { + "Bereits UNC: $LocalPath" | Out-File $debugFile -Append return $LocalPath } $drive = Split-Path -Path $LocalPath -Qualifier $driveLetter = $drive.TrimEnd(':') + $relativePath = $LocalPath.Substring($drive.Length) + "Drive: $drive, Letter: $driveLetter, RelPath: $relativePath" | Out-File $debugFile -Append - $netUse = net use 2>$null | Where-Object { $_ -match "^\s*\w*\s+$driveLetter`:" } - - if ($netUse) { - $parts = $netUse -split '\s+' - foreach ($part in $parts) { - if ($part -match '^\\\\') { - $uncRoot = $part - $relativePath = $LocalPath.Substring($drive.Length) - return Join-Path $uncRoot $relativePath - } - } - } - - $psDrive = Get-PSDrive -Name $driveLetter -ErrorAction SilentlyContinue - if ($psDrive -and $psDrive.DisplayRoot) { - $relativePath = $LocalPath.Substring($drive.Length) - return Join-Path $psDrive.DisplayRoot $relativePath - } - + # Methode 1: WMI - zuverlaessigste Methode fuer Netzlaufwerke $wmiDrive = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID='$drive'" -ErrorAction SilentlyContinue + "WMI LogicalDisk ProviderName: $($wmiDrive.ProviderName)" | Out-File $debugFile -Append if ($wmiDrive -and $wmiDrive.ProviderName) { - $relativePath = $LocalPath.Substring($drive.Length) - return Join-Path $wmiDrive.ProviderName $relativePath + $result = Join-Path $wmiDrive.ProviderName $relativePath + "Result (WMI): $result" | Out-File $debugFile -Append + return $result } + # Methode 2: Get-PSDrive + $psDrive = Get-PSDrive -Name $driveLetter -ErrorAction SilentlyContinue + "PSDrive DisplayRoot: $($psDrive.DisplayRoot)" | Out-File $debugFile -Append + if ($psDrive -and $psDrive.DisplayRoot) { + $result = Join-Path $psDrive.DisplayRoot $relativePath + "Result (PSDrive): $result" | Out-File $debugFile -Append + return $result + } + + # Methode 3: Win32_MappedLogicalDisk (speziell fuer gemappte Laufwerke) + $mappedDrive = Get-WmiObject -Class Win32_MappedLogicalDisk -Filter "DeviceID='$drive'" -ErrorAction SilentlyContinue + "WMI MappedLogicalDisk ProviderName: $($mappedDrive.ProviderName)" | Out-File $debugFile -Append + if ($mappedDrive -and $mappedDrive.ProviderName) { + $result = Join-Path $mappedDrive.ProviderName $relativePath + "Result (MappedDisk): $result" | Out-File $debugFile -Append + return $result + } + + # Fallback: Lokales Laufwerk als Admin-Share $computerName = $env:COMPUTERNAME $adminShare = "\\$computerName\$driveLetter`$" - $relativePath = $LocalPath.Substring($drive.Length) - return Join-Path $adminShare $relativePath + $result = Join-Path $adminShare $relativePath + "Result (Fallback): $result" | Out-File $debugFile -Append + return $result } function Get-DomainUsers {