public static void CloseClient(this PSCmdlet cmdlet, IClusterClient client) { try { if (client == null) { return; } try { client.Close().GetAwaiter().GetResult(); } catch (Exception exception) { cmdlet.WriteError( new ErrorRecord( exception, $"{nameof(IClusterClient)}{nameof(IClusterClient.Close)}Failed", ErrorCategory.CloseError, client)); } client.Dispose(); } finally { var sessionClient = cmdlet.GetClient(); // If this client is the client associated with the current session, clear the current session's client. if (ReferenceEquals(sessionClient, client)) { cmdlet.SetClient(null); } } }