bin/projects/dbatools/dbatools/Connection/ConnectionHost.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
using System;
using System.Collections.Generic; using System.Management.Automation.Runspaces; namespace Sqlcollaborative.Dbatools.Connection { /// <summary> /// Provides static tools for managing connections /// </summary> public static class ConnectionHost { /// <summary> /// List of all registered connections. /// </summary> public static Dictionary<string, ManagementConnection> Connections = new Dictionary<string, ManagementConnection>(); #region Configuration Computer Management /// <summary> /// The time interval that must pass, before a connection using a known to not work connection protocol is reattempted /// </summary> public static TimeSpan BadConnectionTimeout = new TimeSpan(0, 15, 0); /// <summary> /// Globally disables all caching done by the Computer Management functions. /// </summary> public static bool DisableCache = false; /// <summary> /// Disables the caching of bad credentials. dbatools caches bad logon credentials for wmi/cim and will not reuse them. /// </summary> public static bool DisableBadCredentialCache = false; /// <summary> /// Disables the automatic registration of working credentials. dbatools will caches the last working credential when connecting using wmi/cim and will use those rather than using known bad credentials /// </summary> public static bool DisableCredentialAutoRegister = false; /// <summary> /// Enabling this will force the use of the last credentials known to work, rather than even trying explicit credentials. /// </summary> public static bool OverrideExplicitCredential = false; /// <summary> /// Enables automatic failover to working credentials, when passed credentials either are known, or turn out to not work. /// </summary> public static bool EnableCredentialFailover = false; /// <summary> /// Globally disables the persistence of Cim sessions used to connect to a target system. /// </summary> public static bool DisableCimPersistence = false; /// <summary> /// Whether the CM connection using Cim over WinRM is disabled globally /// </summary> public static bool DisableConnectionCimRM = false; /// <summary> /// Whether the CM connection using Cim over DCOM is disabled globally /// </summary> public static bool DisableConnectionCimDCOM = false; /// <summary> /// Whether the CM connection using WMI is disabled globally /// </summary> public static bool DisableConnectionWMI = true; /// <summary> /// Whether the CM connection using PowerShell Remoting is disabled globally /// </summary> public static bool DisableConnectionPowerShellRemoting = true; #endregion Configuration Computer Management #region Configuration Sql Connection /// <summary> /// The number of seconds before a sql connection attempt times out /// </summary> public static int SqlConnectionTimeout = 15; #endregion Configuration Sql Connection #region PowerShell remoting sessions /// <summary> /// List of all session containers used to maintain a cache /// </summary> public static Dictionary<Guid, PSSessionContainer> PSSessions = new Dictionary<Guid, PSSessionContainer>(); #region Public operations /// <summary> /// Returns a registered session for a given computer on a given runspace. Returns null if nothing is registered. /// </summary> /// <param name="Runspace">The host runspace that opened the session</param> /// <param name="ComputerName">The computer connected to</param> /// <returns></returns> public static PSSession PSSessionGet(Guid Runspace, string ComputerName) { if (!PSSessions.ContainsKey(Runspace)) return null; return PSSessions[Runspace].Get(ComputerName.ToLower()); } /// <summary> /// Registeres a remote session under the owning runspace in its respective computer name /// </summary> /// <param name="Runspace">The runspace that owns the session</param> /// <param name="ComputerName">The computer the session connects to</param> /// <param name="Session">The session object</param> public static void PSSessionSet(Guid Runspace, string ComputerName, PSSession Session) { if (!PSSessionCacheEnabled) return; if (!PSSessions.ContainsKey(Runspace)) PSSessions[Runspace] = new PSSessionContainer(Runspace); PSSessions[Runspace].Set(ComputerName.ToLower(), Session); } /// <summary> /// Searches the cache for an expired remoting session and purges it. After purging it from the list, it still needs to be closed! /// </summary> /// <returns>The session purged that then needs to be closed</returns> public static PSSession PSSessionPurgeExpired() { foreach (PSSessionContainer container in PSSessions.Values) if (container.CountExpired > 0) return container.PurgeExpiredSession(); return null; } /// <summary> /// The number of expired sessions /// </summary> public static int PSSessionCountExpired { get { int num = 0; foreach (PSSessionContainer container in PSSessions.Values) num += container.CountExpired; return num; } } #endregion Public operations #region Configuration /// <summary> /// The time until established connections will be considered expired (if available) /// </summary> public static TimeSpan PSSessionTimeout = new TimeSpan(0, 5, 0); /// <summary> /// Whether sessions should be cached at all /// </summary> public static bool PSSessionCacheEnabled = true; #endregion Configuration #endregion PowerShell remoting sessions } } |