private/Get-UnraidAuth.ps1
|
function Get-UnraidAuth { [CmdletBinding()] param( [Parameter()] [string]$ServerName, [Parameter()] [string]$Path ) process { if (!$Path) { $appDataPath = if ($IsWindows -or $PSVersionTable.PSVersion.Major -lt 6) { Join-Path $env:LOCALAPPDATA "PSUnraid" } elseif ($IsMacOS) { Join-Path $env:HOME "Library/Application Support/PSUnraid" } else { Join-Path $env:HOME ".config/PSUnraid" } $Path = Join-Path $appDataPath "auth.xml" } if (!(Test-Path $Path)) { Write-Warning "No saved authentication found at $Path" return $null } try { $authData = Import-Clixml -Path $Path if (!$ServerName) { # If multiple servers, need to prompt or show available $serverCount = $authData.Keys.Count if ($serverCount -eq 0) { Write-Warning "No saved authentication found." return $null } elseif ($serverCount -eq 1) { $serverKey = $authData.Keys | Select-Object -First 1 Write-Verbose "Only one server found, using: $serverKey" return $authData[$serverKey] } # This shouldn't really happen - since this function is private and it always called w -ServerName else { Write-Warning "Multiple servers found in auth file. Please specify -ServerName parameter." Write-Information "Available servers:" $authData.Keys | ForEach-Object { Write-Information " - $_" } return $null } } # Server name was specified if ($authData.ContainsKey($ServerName)) { Write-Verbose "Loaded authentication for $ServerName (Type: $($authData[$ServerName].AuthType))" return $authData[$ServerName] } else { Write-Warning "No saved authentication found for server '$ServerName'" Write-Information "Available servers:" $authData.Keys | ForEach-Object { Write-Information " - $_" } return $null } } catch { Write-Error "Failed to load authentication: $_" return $null } } } |