public/Disconnect-Server.ps1
|
function Disconnect-ZStackServer { # .SYNOPSIS # Closes the connection and removes its local session files. # .DESCRIPTION # ZStack PowerCLI (ZStack.PowerCLI 1.1.4) uses CLI 1.1.4, commit dca13b1d898c80c0f7189b5adb4a5537685ad4a6. # Connection configuration namespace: zstack-powershell. # .LINK # https://github.com/terraform-zstack-modules/zstack-cli-go/blob/main/docs/powershell.md [CmdletBinding()] param([Parameter(Mandatory, Position = 0, ValueFromPipeline)] [object[]] $Server) process { foreach ($item in $Server) { $connection = Get-CloudConnection $item $failure = $null try { $arguments = @('logout', '--context', $connection.ContextName, '--error-format', 'json') $result = Invoke-CliProcess -Path $connection.CliPath -ArgumentList $arguments -ConfigPath $connection.ConfigPath $failure = Get-CloudProcessError $result 'Disconnect' -Target $connection.Id } finally { try { Remove-CloudConnection $connection } catch { $PSCmdlet.WriteError((New-CloudError -Operation Disconnect -ExitCode 9 -Target $connection.Id)) } } if ($failure) { $PSCmdlet.WriteError($failure) } } } } |