80 lines
2.3 KiB
PowerShell
80 lines
2.3 KiB
PowerShell
function Connect-GpfsServer {
|
|
[CmdletBinding(DefaultParameterSetName = 'Credential')]
|
|
param(
|
|
[Parameter(Mandatory, Position = 0)]
|
|
[string]$Server,
|
|
|
|
[Parameter()]
|
|
[int]$Port = 443,
|
|
|
|
[Parameter()]
|
|
[ValidateSet('v2')]
|
|
[string]$ApiVersion = 'v2',
|
|
|
|
[Parameter(Mandatory, ParameterSetName = 'Credential')]
|
|
[PSCredential]$Credential,
|
|
|
|
[Parameter(Mandatory, ParameterSetName = 'UserPass')]
|
|
[string]$Username,
|
|
|
|
[Parameter(Mandatory, ParameterSetName = 'UserPass')]
|
|
[SecureString]$Password,
|
|
|
|
[Parameter()]
|
|
[switch]$UseSsl,
|
|
|
|
[Parameter()]
|
|
[switch]$SkipCertificateCheck,
|
|
|
|
[Parameter()]
|
|
[int]$TimeoutSeconds = 30,
|
|
|
|
[Parameter()]
|
|
[switch]$PassThru
|
|
)
|
|
|
|
if ($PSCmdlet.ParameterSetName -eq 'UserPass') {
|
|
$Credential = [PSCredential]::new($Username, $Password)
|
|
}
|
|
|
|
$protocol = if ($UseSsl) { 'https' } else { 'https' }
|
|
$baseUrl = '{0}://{1}:{2}' -f $protocol, $Server, $Port
|
|
|
|
$plainPass = $Credential.GetNetworkCredential().Password
|
|
$credBytes = [System.Text.Encoding]::UTF8.GetBytes('{0}:{1}' -f $Credential.UserName, $plainPass)
|
|
$encodedCred = [Convert]::ToBase64String($credBytes)
|
|
|
|
$script:GpfsSession = @{
|
|
Server = $Server
|
|
Port = $Port
|
|
BaseUrl = $baseUrl
|
|
ApiVersion = $ApiVersion
|
|
SkipCertificateCheck = $SkipCertificateCheck.IsPresent
|
|
TimeoutSeconds = $TimeoutSeconds
|
|
Headers = @{
|
|
Authorization = "Basic $encodedCred"
|
|
Accept = 'application/json'
|
|
}
|
|
}
|
|
|
|
try {
|
|
$cluster = Invoke-GpfsApiRequest -Method GET -Endpoint 'cluster'
|
|
Write-Verbose "Connected to GPFS cluster: $($cluster.cluster.clusterName)"
|
|
}
|
|
catch {
|
|
$script:GpfsSession = $null
|
|
throw "Failed to connect to GPFS server '$Server': $_"
|
|
}
|
|
|
|
if ($PassThru) {
|
|
[PSCustomObject]@{
|
|
Server = $Server
|
|
Port = $Port
|
|
ApiVersion = $ApiVersion
|
|
Connected = $true
|
|
}
|
|
}
|
|
else {
|
|
Write-Host "Connected to GPFS server: $Server ($ApiVersion)" -ForegroundColor Green
|
|
}
|
|
}
|