private void RemoveSessionInternal(CimSession session, CimSessionWrapper wrapper) { DebugHelper.WriteLogEx(); this.curCimSessionsByInstanceId.Remove(wrapper.InstanceId); this.curCimSessionsById.Remove(wrapper.SessionId); this.curCimSessionWrapper.Remove(session); session.Dispose(); }
/// <summary> /// Closing connection and disposing object /// </summary> public void Dispose() { if (_disposed) { throw new ObjectDisposedException(nameof(CimConnection)); } _connection?.Dispose(); _disposed = true; }
public void Dispose() { if (mostRecentMscFilename is not null) { File.Delete(mostRecentMscFilename); mostRecentMscFilename = null; } cimSession.Close(); cimSession.Dispose(); }
protected virtual void Dispose(bool disposing) { if (!_isDisposed) { if (disposing) { _subscription?.Dispose(); _cimSession?.Dispose(); } _isDisposed = true; } }
private void KickOffCimAsync(Computer computer, Credential credential, string commandline, CimSessionOptions options) { var optionsBuilder = new DbContextOptionsBuilder <ACEWebServiceDbContext>(); optionsBuilder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=ACEWebService;Trusted_Connection=True;MultipleActiveResultSets=true"); using (ACEWebServiceDbContext context = new ACEWebServiceDbContext(optionsBuilder.Options)) { // Convert stored password to a secure string SecureString securePwd = new SecureString(); foreach (char c in _cryptoService.Decrypt(credential.Password)) { securePwd.AppendChar(c); } CimCredential cimCreds = null; if (credential.UserName.Contains('\\')) { // Create a CimCredential object cimCreds = new CimCredential(PasswordAuthenticationMechanism.Kerberos, credential.UserName.Split('\\')[0], credential.UserName.Split('\\')[1], securePwd); } else { // Create a CimCredential object cimCreds = new CimCredential(PasswordAuthenticationMechanism.Default, null, credential.UserName, securePwd); } // Create a CimSession with the remote system options.AddDestinationCredentials(cimCreds); CimSession session = CimSession.Create(computer.ComputerName, options); // Create a CimMethodParametersCollection to pass to method invocation CimMethodParametersCollection collection = new CimMethodParametersCollection(); collection.Add(CimMethodParameter.Create("CommandLine", commandline, CimFlags.None)); CimMethodResult result = session.InvokeMethod("root/cimv2", "Win32_Process", "Create", collection); if (result.ReturnValue.ToString() == "0") { } else { } session.Dispose(); } }
private void KickOffCim(Computer computer, Credential credential, string commandline, CimSessionOptions options) { // Convert stored password to a secure string SecureString securePwd = new SecureString(); foreach (char c in _cryptoService.Decrypt(credential.Password)) { Console.WriteLine("[char]: {0}", c); securePwd.AppendChar(c); } CimCredential cimCreds = null; if (credential.UserName.Contains('\\')) { // Create a CimCredential object cimCreds = new CimCredential(PasswordAuthenticationMechanism.Kerberos, credential.UserName.Split('\\')[0], credential.UserName.Split('\\')[1], securePwd); } else { // Create a CimCredential object cimCreds = new CimCredential(PasswordAuthenticationMechanism.Default, null, credential.UserName, securePwd); } // Create a CimSession with the remote system options.AddDestinationCredentials(cimCreds); CimSession session = CimSession.Create(computer.ComputerName, options); // Create a CimMethodParametersCollection to pass to method invocation CimMethodParametersCollection collection = new CimMethodParametersCollection { CimMethodParameter.Create("CommandLine", commandline, CimFlags.None) }; CimMethodResult result = session.InvokeMethod("root/cimv2", "Win32_Process", "Create", collection); if (result.ReturnValue.ToString() == "0") { } else { } session.Dispose(); }
static void Main(string[] args) { // Create a CimSession with the remote system CimSessionOptions options = new DComSessionOptions(); //options.AddDestinationCredentials(cimCreds); CimSession session = CimSession.Create("localhost", options); // Create a CimMethodParametersCollection to pass to method invocation CimMethodParametersCollection collection = new CimMethodParametersCollection(); collection.Add(CimMethodParameter.Create("CommandLine", "calc.exe", CimFlags.None)); // Invoke the Win32_Process classes Create method to start a calc.exe process on a remote system CimMethodResult result = session.InvokeMethod("root/cimv2", "Win32_Process", "Create", collection); if (result.ReturnValue.ToString() == "0") { } else { } session.Dispose(); }
/// <summary> /// <para> /// Remove given <see cref="CimSession"/> object from partial of the cache only. /// </para> /// </summary> /// <param name="session"></param> /// <param name="psObject"></param> private void RemoveSessionInternal(CimSession session, CimSessionWrapper wrapper) { DebugHelper.WriteLogEx(); this.curCimSessionsByInstanceId.Remove(wrapper.InstanceId); this.curCimSessionsById.Remove(wrapper.SessionId); this.curCimSessionWrapper.Remove(session); session.Dispose(); }
/// <summary> /// <para>Wrapper function to remove CimSession from cache</para> /// </summary> /// <param name="session"></param> /// <param name="dispose">Whether need to dispose the <see cref="CimSession"/> object</param> private static void RemoveCimSessionFromTemporaryCache(CimSession session, bool dispose) { if (null != session) { bool removed = false; lock (temporarySessionCacheLock) { if (temporarySessionCache.ContainsKey(session)) { temporarySessionCache[session]--; DebugHelper.WriteLogEx(@"Decrease cimsession ref count {0}", 1, temporarySessionCache[session]); if (temporarySessionCache[session] == 0) { removed = true; temporarySessionCache.Remove(session); } } } // there is a race condition that if // one thread is waiting to add CimSession to cache, // while current thread is removing the CimSession, // then invalid CimSession may be added to cache. // Ignored this scenario in CimCmdlet implementation, // since the code inside cimcmdlet will not hit this // scenario anyway. if (removed && dispose) { DebugHelper.WriteLogEx(@"Dispose cimsession ", 1); session.Dispose(); } } }