function Get-GpfsConfiguration { [CmdletBinding()] param( [Parameter()] [string]$Path = (Join-Path $env:USERPROFILE '.PSGpfsApi\config.json'), [Parameter()] [string]$Profile ) if (-not (Test-Path $Path)) { throw "Configuration file not found: $Path" } $config = Get-Content $Path -Raw | ConvertFrom-Json if ($Profile) { $profileObj = $config.Profiles.$Profile if (-not $profileObj) { throw "Profile '$Profile' not found in configuration." } return $profileObj } return $config } function Set-GpfsConfiguration { [CmdletBinding(SupportsShouldProcess)] param( [Parameter(Mandatory)] [string]$ProfileName, [Parameter(Mandatory)] [string]$Server, [Parameter()] [int]$Port = 443, [Parameter()] [ValidateSet('v2')] [string]$ApiVersion = 'v2', [Parameter()] [bool]$UseSsl = $true, [Parameter()] [bool]$SkipCertificateCheck = $false, [Parameter()] [int]$TimeoutSeconds = 30, [Parameter()] [string]$Username, [Parameter()] [string]$Path = (Join-Path $env:USERPROFILE '.PSGpfsApi\config.json'), [Parameter()] [switch]$SetActive ) $dir = Split-Path $Path -Parent if (-not (Test-Path $dir)) { New-Item -ItemType Directory -Path $dir -Force | Out-Null } if (Test-Path $Path) { $config = Get-Content $Path -Raw | ConvertFrom-Json } else { $config = [PSCustomObject]@{ Profiles = [PSCustomObject]@{} ActiveProfile = 'default' } } $profileData = [PSCustomObject]@{ Server = $Server Port = $Port ApiVersion = $ApiVersion UseSsl = $UseSsl SkipCertificateCheck = $SkipCertificateCheck TimeoutSeconds = $TimeoutSeconds Username = $Username } $config.Profiles | Add-Member -NotePropertyName $ProfileName -NotePropertyValue $profileData -Force if ($SetActive) { $config.ActiveProfile = $ProfileName } if ($PSCmdlet.ShouldProcess($Path, 'Write configuration')) { $config | ConvertTo-Json -Depth 10 | Set-Content $Path -Encoding UTF8 Write-Verbose "Configuration saved: $Path (profile: $ProfileName)" } } function Import-GpfsConfiguration { [CmdletBinding()] param( [Parameter()] [string]$Path = (Join-Path $env:USERPROFILE '.PSGpfsApi\config.json'), [Parameter()] [string]$Profile, [Parameter(Mandatory)] [PSCredential]$Credential, [Parameter()] [switch]$SkipCertificateCheck ) if (-not (Test-Path $Path)) { throw "Configuration file not found: $Path" } $config = Get-Content $Path -Raw | ConvertFrom-Json $profileName = if ($Profile) { $Profile } else { $config.ActiveProfile } $profileObj = $config.Profiles.$profileName if (-not $profileObj) { throw "Profile '$profileName' not found in configuration." } $connectParams = @{ Server = $profileObj.Server Port = $profileObj.Port ApiVersion = $profileObj.ApiVersion TimeoutSeconds = $profileObj.TimeoutSeconds Credential = $Credential } if ($profileObj.UseSsl) { $connectParams['UseSsl'] = $true } if ($profileObj.SkipCertificateCheck -or $SkipCertificateCheck) { $connectParams['SkipCertificateCheck'] = $true } Connect-GpfsServer @connectParams } function Export-GpfsConfiguration { [CmdletBinding(SupportsShouldProcess)] param( [Parameter(Mandatory)] [string]$Path ) $defaultPath = Join-Path $env:USERPROFILE '.PSGpfsApi\config.json' if (-not (Test-Path $defaultPath)) { throw "No configuration found at default path: $defaultPath" } if ($PSCmdlet.ShouldProcess($Path, 'Export configuration')) { Copy-Item -Path $defaultPath -Destination $Path -Force Write-Verbose "Configuration exported to: $Path" } } function New-GpfsConfigurationProfile { [CmdletBinding()] param( [Parameter(Mandatory)] [string]$Name, [Parameter(Mandatory)] [string]$Server, [Parameter()] [int]$Port = 443, [Parameter()] [string]$Username, [Parameter()] [bool]$SkipCertificateCheck = $false, [Parameter()] [switch]$SetActive ) $setParams = @{ ProfileName = $Name Server = $Server Port = $Port SkipCertificateCheck = $SkipCertificateCheck SetActive = $SetActive } if ($Username) { $setParams['Username'] = $Username } Set-GpfsConfiguration @setParams } function Select-GpfsConfigurationProfile { [CmdletBinding(SupportsShouldProcess)] param( [Parameter(Mandatory, Position = 0)] [string]$Profile, [Parameter()] [string]$Path = (Join-Path $env:USERPROFILE '.PSGpfsApi\config.json') ) if (-not (Test-Path $Path)) { throw "Configuration file not found: $Path" } $config = Get-Content $Path -Raw | ConvertFrom-Json if (-not $config.Profiles.$Profile) { throw "Profile '$Profile' not found in configuration." } if ($PSCmdlet.ShouldProcess($Profile, 'Set active profile')) { $config.ActiveProfile = $Profile $config | ConvertTo-Json -Depth 10 | Set-Content $Path -Encoding UTF8 Write-Verbose "Active profile set to: $Profile" } }