/// <summary> /// Closes the connection (if open). /// </summary> /// <remarks>Must not fail or throw.</remarks> public void Close() { try { Runspace?.Close(); Runspace?.Dispose(); } catch { /* must not throw! */ } Runspace = null; ConnectionInfo = null; Host = null; FileSystem = null; }
/// <summary> /// Opens the connection. /// </summary> /// <param name="connectionInfo">The connection information.</param> public void Open(PSConnectionInfo connectionInfo) { if (connectionInfo == null) { throw new ArgumentNullException(nameof(connectionInfo)); } this.Close(); try { Runspace rrs = null; this.Host = new PSClientHost(); if (connectionInfo.IsLocalConnection()) { // Local connection rrs = RunspaceFactory.CreateRunspace ( Host ); } else { // Remote connection rrs = RunspaceFactory.CreateRunspace ( Host, connectionInfo.ToRunspaceConnectionInfo() ); } rrs.Open(); this.Runspace = rrs; this.ConnectionInfo = connectionInfo; FileSystem = new PSFileSystem(this); } catch { this.Close(); throw; } }