public CimActivityImplementationContext(ActivityImplementationContext activityImplementationContext, string computerName, PSCredential credential, string certificateThumbprint, AuthenticationMechanism? authenticationMechanism, bool useSsl, uint port, PSSessionOption sessionOption, CimSession session, CimSessionOptions cimSessionOptions, string moduleDefinition, Uri resourceUri) { if (activityImplementationContext != null) { base.PowerShellInstance = activityImplementationContext.PowerShellInstance; this.ResourceUri = resourceUri; this.ComputerName = computerName; base.PSCredential = credential; base.PSCertificateThumbprint = certificateThumbprint; base.PSAuthentication = authenticationMechanism; base.PSUseSsl = new bool?(useSsl); base.PSPort = new uint?(port); base.PSSessionOption = sessionOption; this.Session = session; this.SessionOptions = cimSessionOptions; if (moduleDefinition != null) { CimActivityImplementationContext._moduleScriptBlock = ScriptBlock.Create(moduleDefinition); this._moduleDefinition = moduleDefinition; } return; } else { throw new ArgumentNullException("activityImplementationContext"); } }
internal CimSessionOptions(CimSessionOptions optionsToClone) { if (optionsToClone == null) { throw new ArgumentNullException("optionsToClone"); } this.Protocol = optionsToClone.Protocol; if (optionsToClone.DestinationOptionsHandle == null) { // underline DestinationOptions is not created yet, then create a new one this._destinationOptionsHandle = new Lazy <MI_DestinationOptions>( delegate { MI_DestinationOptions tmp; MI_Result result = CimApplication.Handle.NewDestinationOptions(out tmp); CimException.ThrowIfMiResultFailure(result); return(tmp); }); } else { MI_DestinationOptions tmp; MI_Result result = optionsToClone.DestinationOptionsHandle.Clone(out tmp); CimException.ThrowIfMiResultFailure(result); this._destinationOptionsHandle = new Lazy <MI_DestinationOptions>(() => tmp); } // Ensure the destinationOptions is created if (this.DestinationOptionsHandleOnDemand == null) { CimException.ThrowIfMiResultFailure(MI_Result.MI_RESULT_FAILED); } }
internal CimSessionOptions(CimSessionOptions optionsToClone) { if (optionsToClone != null) { this.Protocol = optionsToClone.Protocol; return; } else { throw new ArgumentNullException("optionsToClone"); } }
internal void NewCimSession(NewCimSessionCommand cmdlet, CimSessionOptions sessionOptions, CimCredential credential) { string localhostComputerName; DebugHelper.WriteLogEx(); IEnumerable<string> computerNames = ConstValue.GetComputerNames(cmdlet.ComputerName); foreach (string computerName in computerNames) { if (sessionOptions == null) { DebugHelper.WriteLog("Create CimSessionOption due to NewCimSessionCommand has null sessionoption", 1); sessionOptions = CimSessionProxy.CreateCimSessionOption(computerName, cmdlet.OperationTimeoutSec, credential); } CimSessionProxy cimSessionProxyTestConnection = new CimSessionProxyTestConnection(computerName, sessionOptions); if (computerName == ConstValue.NullComputerName) { localhostComputerName = ConstValue.LocalhostComputerName; } else { localhostComputerName = computerName; } string str = localhostComputerName; CimSessionWrapper cimSessionWrapper = new CimSessionWrapper(0, Guid.Empty, cmdlet.Name, str, cimSessionProxyTestConnection.CimSession, cimSessionProxyTestConnection.Protocol); CimNewSession.CimTestCimSessionContext cimTestCimSessionContext = new CimNewSession.CimTestCimSessionContext(cimSessionProxyTestConnection, cimSessionWrapper); cimSessionProxyTestConnection.ContextObject = cimTestCimSessionContext; SwitchParameter skipTestConnection = cmdlet.SkipTestConnection; if (!skipTestConnection.IsPresent) { this.cimTestSession.TestCimSession(computerName, cimSessionProxyTestConnection); } else { this.AddSessionToCache(cimSessionProxyTestConnection.CimSession, cimTestCimSessionContext, new CmdletOperationBase(cmdlet)); } } }
/// <summary> /// Create <see cref="CimSession"/> by given computer name, /// session options /// </summary> /// <param name="computerName"></param> /// <param name="sessionOptions"></param> /// <param name="operOptions">Used when create async operation</param> public CimSessionProxy(string computerName, CimSessionOptions sessionOptions, CimOperationOptions operOptions) { CreateSetSession(computerName, null, sessionOptions, operOptions, false); this.isDefaultSession = (computerName == ConstValue.NullComputerName); }
/// <summary> /// Initialize CimSessionProxy object /// </summary> /// <param name="computerName"></param> /// <param name="session"></param> /// <param name="sessionOptions"></param> /// <param name="options"></param> private void CreateSetSession( string computerName, CimSession cimSession, CimSessionOptions sessionOptions, CimOperationOptions operOptions, bool temporaryCimSession) { DebugHelper.WriteLogEx("computername {0}; cimsession {1}; sessionOptions {2}; operationOptions {3}.", 0, computerName, cimSession, sessionOptions, operOptions); lock (this.stateLock) { this.CancelOperation = null; this.operation = null; } InitOption(operOptions); this.protocol = ProtocolType.Wsman; this.isTemporaryCimSession = temporaryCimSession; if (cimSession != null) { this.session = cimSession; CimSessionState state = CimSessionBase.GetCimSessionState(); if (state != null) { CimSessionWrapper wrapper = state.QuerySession(cimSession); if (wrapper != null) { this.protocol = wrapper.GetProtocolType(); } } } else { if (sessionOptions != null) { if (sessionOptions is DComSessionOptions) { string defaultComputerName = ConstValue.IsDefaultComputerName(computerName) ? ConstValue.NullComputerName : computerName; this.session = CimSession.Create(defaultComputerName, sessionOptions); this.protocol = ProtocolType.Dcom; } else { this.session = CimSession.Create(computerName, sessionOptions); } } else { this.session = CreateCimSessionByComputerName(computerName); } this.isTemporaryCimSession = true; } if (this.isTemporaryCimSession) { AddCimSessionToTemporaryCache(this.session); } this.invocationContextObject = new InvocationContext(this); DebugHelper.WriteLog("Protocol {0}, Is temporary session ? {1}", 1, this.protocol, this.isTemporaryCimSession); }
private void CreateSetSession(string computerName, CimSession cimSession, CimSessionOptions sessionOptions, CimOperationOptions operOptions, bool temporaryCimSession) { string nullComputerName; object[] objArray = new object[4]; objArray[0] = computerName; objArray[1] = cimSession; objArray[2] = sessionOptions; objArray[3] = operOptions; DebugHelper.WriteLogEx("computername {0}; cimsession {1}; sessionOptions {2}; operationOptions {3}.", 0, objArray); lock (this.stateLock) { this.CancelOperation = null; this.operation = null; } this.InitOption(operOptions); this.protocol = ProtocolType.Wsman; this.isTemporaryCimSession = temporaryCimSession; if (cimSession == null) { if (sessionOptions == null) { this.session = this.CreateCimSessionByComputerName(computerName); } else { if (sessionOptions as DComSessionOptions == null) { this.session = CimSession.Create(computerName, sessionOptions); } else { if (ConstValue.IsDefaultComputerName(computerName)) { nullComputerName = ConstValue.NullComputerName; } else { nullComputerName = computerName; } string str = nullComputerName; this.session = CimSession.Create(str, sessionOptions); this.protocol = ProtocolType.Dcom; } } this.isTemporaryCimSession = true; } else { this.session = cimSession; CimSessionState cimSessionState = CimSessionBase.GetCimSessionState(); if (cimSessionState != null) { CimSessionWrapper cimSessionWrapper = cimSessionState.QuerySession(cimSession); if (cimSessionWrapper != null) { this.protocol = cimSessionWrapper.GetProtocolType(); } } } if (this.isTemporaryCimSession) { CimSessionProxy.AddCimSessionToTemporaryCache(this.session); } this.invocationContextObject = new InvocationContext(this); object[] objArray1 = new object[2]; objArray1[0] = this.protocol; objArray1[1] = this.isTemporaryCimSession; DebugHelper.WriteLog("Protocol {0}, Is temporary session ? {1}", 1, objArray1); }
internal void BuildSessionOptions (out CimSessionOptions outputOptions, out CimCredential outputCredential) { WSManSessionOptions wSManSessionOption; PasswordAuthenticationMechanism authentication; DebugHelper.WriteLogEx (); CimSessionOptions dComSessionOption = null; /* Requires Authentication for Remote Host */ if (this.credential == null && ComputerName != null) { bool requiredAuth = false; foreach(var c in ComputerName) { if (c != null && !c.Equals("localhost", StringComparison.OrdinalIgnoreCase)) { requiredAuth = true; break; } } if (requiredAuth) { TrySetCredentials(); } } if (this.SessionOption != null) { if (this.SessionOption as WSManSessionOptions == null) { dComSessionOption = new DComSessionOptions (this.sessionOption as DComSessionOptions); } else { dComSessionOption = new WSManSessionOptions (this.sessionOption as WSManSessionOptions); } } outputOptions = null; outputCredential = null; if (dComSessionOption != null) { DComSessionOptions dComSessionOption1 = dComSessionOption as DComSessionOptions; if (dComSessionOption1 != null) { bool flag = false; string empty = string.Empty; if (this.CertificateThumbprint != null) { flag = true; empty = "CertificateThumbprint"; } if (this.portSet) { flag = true; empty = "Port"; } if (flag) { base.ThrowConflictParameterWasSet ("New-CimSession", empty, "DComSessionOptions"); return; } } } if (this.portSet || this.CertificateThumbprint != null) { if (dComSessionOption == null) { wSManSessionOption = new WSManSessionOptions (); } else { wSManSessionOption = dComSessionOption as WSManSessionOptions; } WSManSessionOptions port = wSManSessionOption; if (this.portSet) { port.DestinationPort = this.Port; this.portSet = false; } if (this.CertificateThumbprint != null) { CimCredential cimCredential = new CimCredential (CertificateAuthenticationMechanism.Default, this.CertificateThumbprint); port.AddDestinationCredentials (cimCredential); } dComSessionOption = port; } if (this.operationTimeoutSet && dComSessionOption != null) { dComSessionOption.Timeout = TimeSpan.FromSeconds ((double)((float)this.OperationTimeoutSec)); } if (this.authenticationSet || this.credential != null) { if (this.authenticationSet) { authentication = this.Authentication; } else { authentication = PasswordAuthenticationMechanism.Default; } PasswordAuthenticationMechanism passwordAuthenticationMechanism = authentication; if (this.authenticationSet) { this.authenticationSet = false; } CimCredential cimCredential1 = base.CreateCimCredentials (this.Credential, passwordAuthenticationMechanism, "New-CimSession", "Authentication"); if (cimCredential1 != null) { object[] objArray = new object[1]; objArray [0] = cimCredential1; DebugHelper.WriteLog ("Credentials: {0}", 1, objArray); outputCredential = cimCredential1; if (dComSessionOption != null) { object[] objArray1 = new object[1]; objArray1 [0] = dComSessionOption; DebugHelper.WriteLog ("Add credentials to option: {0}", 1, objArray1); dComSessionOption.AddDestinationCredentials (cimCredential1); } } else { return; } } object[] objArray2 = new object[1]; objArray2[0] = outputOptions; DebugHelper.WriteLogEx("Set outputOptions: {0}", 1, objArray2); outputOptions = dComSessionOption; }
public CimSessionProxy(string computerName, CimSessionOptions sessionOptions, CimOperationOptions operOptions) { this.stateLock = new object(); this.operationParameters = new Hashtable(); this.CreateSetSession(computerName, null, sessionOptions, operOptions, false); this.isDefaultSession = computerName == ConstValue.NullComputerName; }
internal CimSession GetSession(string computerName, PSCredential credential, string certificateThumbprint, AuthenticationMechanism authenticationMechanism, CimSessionOptions sessionOptions, bool useSsl, uint port, PSSessionOption pssessionOption) { CimSession session; lock (this.SyncRoot) { if (this.availableSessions.ContainsKey(computerName)) { List<CimConnectionManager.SessionEntry> item = this.availableSessions[computerName]; if (item.Count > 0) { int num = 0; while (num < item.Count) { CimConnectionManager.SessionEntry sessionEntry = item[num]; if ((sessionEntry.SessionOptions != null || sessionOptions != null) && !CimConnectionManager.CompareSessionOptions(sessionEntry, sessionOptions, credential, certificateThumbprint, authenticationMechanism, useSsl, port, pssessionOption)) { num++; } else { sessionEntry.AddReference(); session = sessionEntry.Session; return session; } } } } CimConnectionManager.SessionEntry sessionEntry1 = new CimConnectionManager.SessionEntry(computerName, credential, certificateThumbprint, authenticationMechanism, sessionOptions, useSsl, port, pssessionOption); sessionEntry1.IterationsRemaining = 6; sessionEntry1.AddReference(); if (!this.availableSessions.ContainsKey(computerName)) { this.availableSessions.Add(computerName, new List<CimConnectionManager.SessionEntry>()); } this.availableSessions[computerName].Add(sessionEntry1); session = sessionEntry1.Session; } return session; }
public CimSessionProxyTestConnection(string computerName, CimSessionOptions sessionOptions) : base(computerName, sessionOptions) { }
protected CimSessionProxy CreateCimSessionProxy(string computerName, CimSessionOptions options) { CimSessionProxy cimSessionProxy = new CimSessionProxy(computerName, options); this.SubscribeEventAndAddProxytoCache(cimSessionProxy); return cimSessionProxy; }
private static bool CompareSessionOptions(CimConnectionManager.SessionEntry sessionEntry, CimSessionOptions options2, PSCredential credential2, string certificateThumbprint, AuthenticationMechanism authenticationMechanism, bool useSsl, uint port, PSSessionOption pssessionOption) { TimeSpan timeout = sessionEntry.SessionOptions.Timeout; if (timeout.Equals(options2.Timeout)) { if (string.Equals(sessionEntry.SessionOptions.Culture.ToString(), options2.Culture.ToString(), StringComparison.OrdinalIgnoreCase)) { if (string.Equals(sessionEntry.SessionOptions.UICulture.ToString(), options2.UICulture.ToString(), StringComparison.OrdinalIgnoreCase)) { if (string.Equals(sessionEntry.CertificateThumbprint, certificateThumbprint, StringComparison.OrdinalIgnoreCase)) { if (sessionEntry.AuthenticationMechanism == authenticationMechanism) { if (WorkflowUtils.CompareCredential(sessionEntry.Credential, credential2)) { if (sessionEntry.UseSsl == useSsl) { if (sessionEntry.Port == port) { if (!(pssessionOption == null ^ sessionEntry.PSSessionOption == null)) { if (pssessionOption != null && sessionEntry.PSSessionOption != null) { if (sessionEntry.PSSessionOption.ProxyAccessType == pssessionOption.ProxyAccessType) { if (sessionEntry.PSSessionOption.ProxyAuthentication == pssessionOption.ProxyAuthentication) { if (WorkflowUtils.CompareCredential(sessionEntry.PSSessionOption.ProxyCredential, pssessionOption.ProxyCredential)) { if (sessionEntry.PSSessionOption.SkipCACheck == pssessionOption.SkipCACheck) { if (sessionEntry.PSSessionOption.SkipCNCheck == pssessionOption.SkipCNCheck) { if (sessionEntry.PSSessionOption.SkipRevocationCheck == pssessionOption.SkipRevocationCheck) { if (sessionEntry.PSSessionOption.NoEncryption == pssessionOption.NoEncryption) { if (sessionEntry.PSSessionOption.UseUTF16 != pssessionOption.UseUTF16) { return false; } } else { return false; } } else { return false; } } else { return false; } } else { return false; } } else { return false; } } else { return false; } } else { return false; } } return true; } else { return false; } } else { return false; } } else { return false; } } else { return false; } } else { return false; } } else { return false; } } else { return false; } } else { return false; } } else { return false; } }
private static bool CompareSessionOptions(SessionEntry sessionEntry, CimSessionOptions options2, PSCredential credential2, string certificateThumbprint, AuthenticationMechanism authenticationMechanism, bool useSsl, uint port, PSSessionOption pssessionOption) { if (!sessionEntry.SessionOptions.Timeout.Equals(options2.Timeout)) return false; if (!string.Equals(sessionEntry.SessionOptions.Culture.ToString(), options2.Culture.ToString(), StringComparison.OrdinalIgnoreCase)) return false; if (!string.Equals(sessionEntry.SessionOptions.UICulture.ToString(), options2.UICulture.ToString(), StringComparison.OrdinalIgnoreCase)) return false; if (!string.Equals(sessionEntry.CertificateThumbprint, certificateThumbprint, StringComparison.OrdinalIgnoreCase)) return false; if (sessionEntry.AuthenticationMechanism != authenticationMechanism) return false; if (!Workflow.WorkflowUtils.CompareCredential(sessionEntry.Credential, credential2)) return false; if (sessionEntry.UseSsl != useSsl) return false; if (sessionEntry.Port != port) return false; // check PSSessionOption if present if (pssessionOption == null ^ sessionEntry.PSSessionOption == null) { return false; } if (pssessionOption != null && sessionEntry.PSSessionOption != null) { if (sessionEntry.PSSessionOption.ProxyAccessType != pssessionOption.ProxyAccessType) return false; if (sessionEntry.PSSessionOption.ProxyAuthentication != pssessionOption.ProxyAuthentication) return false; if (!Workflow.WorkflowUtils.CompareCredential(sessionEntry.PSSessionOption.ProxyCredential, pssessionOption.ProxyCredential)) return false; if (sessionEntry.PSSessionOption.SkipCACheck != pssessionOption.SkipCACheck) return false; if (sessionEntry.PSSessionOption.SkipCNCheck != pssessionOption.SkipCNCheck) return false; if (sessionEntry.PSSessionOption.SkipRevocationCheck != pssessionOption.SkipRevocationCheck) return false; if (sessionEntry.PSSessionOption.NoEncryption != pssessionOption.NoEncryption) return false; if (sessionEntry.PSSessionOption.UseUTF16 != pssessionOption.UseUTF16) return false; } return true; }
/// <summary> /// Get a CIM session for the target computer /// </summary> /// <param name="computerName"></param> /// <param name="credential"></param> /// <param name="certificateThumbprint"></param> /// <param name="authenticationMechanism"></param> /// <param name="sessionOptions"></param> /// <param name="useSsl"></param> /// <param name="port"></param> /// <param name="pssessionOption"></param> /// <returns></returns> internal CimSession GetSession(string computerName, PSCredential credential, string certificateThumbprint, AuthenticationMechanism authenticationMechanism, CimSessionOptions sessionOptions, bool useSsl, uint port, PSSessionOption pssessionOption) { System.Diagnostics.Debug.Assert(!string.IsNullOrEmpty(computerName), "ComputerName is null in GetSession. GetSession should not be called in this case."); lock (SyncRoot) { SessionEntry newSessionEntry; if (availableSessions.ContainsKey(computerName)) { List<SessionEntry> sel = availableSessions[computerName]; if (sel.Count > 0) { for (int i = 0; i < sel.Count; i++) { SessionEntry se = sel[i]; // No session options specified or the object matches exactly... if ((se.SessionOptions == null && sessionOptions == null) || CompareSessionOptions(se, sessionOptions, credential, certificateThumbprint, authenticationMechanism, useSsl, port, pssessionOption)) { // Up the number of references to this session object... se.AddReference(); return se.Session; } } } } // Allocate a new session entry for this computer newSessionEntry = new SessionEntry(computerName, credential, certificateThumbprint, authenticationMechanism, sessionOptions, useSsl, port, pssessionOption); newSessionEntry.IterationsRemaining = MaxIterations; newSessionEntry.AddReference(); if (! availableSessions.ContainsKey(computerName)) { availableSessions.Add(computerName, new List<SessionEntry>()); } availableSessions[computerName].Add(newSessionEntry); // Return the session object return newSessionEntry.Session; } }
public SessionEntry(string computerName, PSCredential credential, string certificateThumbprint, AuthenticationMechanism authenticationMechanism, CimSessionOptions sessionOptions, bool useSsl, uint port, PSSessionOption pssessionOption) { SessionOptions = sessionOptions; _credential = credential; _certificateThumbprint = certificateThumbprint; _authenticationMechanism = authenticationMechanism; _useSsl = useSsl; _port = port; _psSessionOption = pssessionOption; Session = CimSession.Create(computerName, sessionOptions); }
/// <summary> /// Create an instance of the CIM activity implementation class /// </summary> /// <param name="activityImplementationContext"></param> /// <param name="computerName"></param> /// <param name="credential"></param> /// <param name="certificateThumbprint"></param> /// <param name="authenticationMechanism"></param> /// <param name="useSsl"></param> /// <param name="port"></param> /// <param name="sessionOption"></param> /// <param name="session"></param> /// <param name="cimSessionOptions"></param> /// <param name="moduleDefinition"></param> /// <param name="resourceUri"></param> public CimActivityImplementationContext( ActivityImplementationContext activityImplementationContext, string computerName, PSCredential credential, string certificateThumbprint, AuthenticationMechanism? authenticationMechanism, bool useSsl, uint port, PSSessionOption sessionOption, CimSession session, CimSessionOptions cimSessionOptions, string moduleDefinition, Uri resourceUri) { if (activityImplementationContext == null) { throw new ArgumentNullException("activityImplementationContext"); } this.PowerShellInstance = activityImplementationContext.PowerShellInstance; ResourceUri = resourceUri; ComputerName = computerName; PSCredential = credential; PSCertificateThumbprint = certificateThumbprint; PSAuthentication = authenticationMechanism; PSUseSsl = useSsl; PSPort = port; PSSessionOption = sessionOption; Session = session; SessionOptions = cimSessionOptions; if (moduleDefinition != null) { // Creating a script block forces the string into the compiled script cache so we // don't need to reparse it at execution time. Locking the static _moduleDefinition is not // required since the operation is idempotent. _moduleScriptBlock = ScriptBlock.Create(moduleDefinition); _moduleDefinition = moduleDefinition; } }
public static CimSession Create(string computerName, CimSessionOptions sessionOptions) { IPAddress pAddress = null; InstanceHandle instanceHandle = null; SessionHandle sessionHandle = null; CimSession cimSession; string protocol; DestinationOptionsHandle destinationOptionsHandle; string str = computerName; if (!string.IsNullOrEmpty(str) && IPAddress.TryParse(str, out pAddress) && pAddress.AddressFamily == AddressFamily.InterNetworkV6 && str[0] != '[') { str = string.Concat("[", str, "]"); } IDisposable disposable = CimApplication.AssertNoPendingShutdown(); using (disposable) { ApplicationHandle handle = CimApplication.Handle; if (sessionOptions == null) { protocol = null; } else { protocol = sessionOptions.Protocol; } string str1 = str; if (sessionOptions == null) { destinationOptionsHandle = null; } else { destinationOptionsHandle = sessionOptions.DestinationOptionsHandle ?? sessionOptions.DestinationOptionsHandleOnDemand; } MiResult miResult = ApplicationMethods.NewSession(handle, protocol, str1, destinationOptionsHandle, out instanceHandle, out sessionHandle); if (miResult != MiResult.NOT_FOUND) { CimException.ThrowIfMiResultFailure(miResult, instanceHandle); CimSession cimSession1 = new CimSession(sessionHandle, computerName); cimSession = cimSession1; } else { throw new CimException(miResult, null, instanceHandle, System.Management.Automation.Strings.UnrecognizedProtocolName); } } return cimSession; }
public static CimAsyncResult<CimSession> CreateAsync(string computerName, CimSessionOptions sessionOptions) { IObservable<CimSession> cimAsyncDelegatedObservable = new CimAsyncDelegatedObservable<CimSession>((IObserver<CimSession> observer) => { CimSession cimSession = null; try { cimSession = CimSession.Create(computerName, sessionOptions); } catch (Exception exception1) { Exception exception = exception1; observer.OnError(exception); } observer.OnNext(cimSession); observer.OnCompleted(); } ); return new CimAsyncResult<CimSession>(cimAsyncDelegatedObservable); }
/// <summary> /// Create a new <see cref="CimSession"/> base on given cmdlet /// and its parameter /// </summary> /// <param name="cmdlet"></param> /// <param name="sessionOptions"></param> /// <param name="credential"></param> internal void NewCimSession(NewCimSessionCommand cmdlet, CimSessionOptions sessionOptions, CimCredential credential) { DebugHelper.WriteLogEx(); IEnumerable<string> computerNames = ConstValue.GetComputerNames(cmdlet.ComputerName); foreach (string computerName in computerNames) { CimSessionProxy proxy; if (sessionOptions == null) { DebugHelper.WriteLog("Create CimSessionOption due to NewCimSessionCommand has null sessionoption", 1); sessionOptions = CimSessionProxy.CreateCimSessionOption(computerName, cmdlet.OperationTimeoutSec, credential); } proxy = new CimSessionProxyTestConnection(computerName, sessionOptions); string computerNameValue = (computerName == ConstValue.NullComputerName) ? ConstValue.LocalhostComputerName : computerName; CimSessionWrapper wrapper = new CimSessionWrapper(0, Guid.Empty, cmdlet.Name, computerNameValue, proxy.CimSession, proxy.Protocol); CimTestCimSessionContext context = new CimTestCimSessionContext(proxy, wrapper); proxy.ContextObject = context; // Skip test the connection if user intend to if(cmdlet.SkipTestConnection.IsPresent) { AddSessionToCache(proxy.CimSession, context, new CmdletOperationBase(cmdlet)); } else { //CimSession will be returned as part of TestConnection this.cimTestSession.TestCimSession(computerName, proxy); } } }
private static void InitializeCmdletInstanceParameters(Command command, PSObject wrappedCmdlet, bool isGenericCim, PSActivityContext psActivityContext, CimSessionOptions cimSessionOptions, ActivityImplementationContext implementationContext) { PSActivity.PSActivity variable = new PSActivity.PSActivity(); variable.cimSessionOptions = cimSessionOptions; bool flag = false; foreach (CommandParameter parameter in command.Parameters) { if (PSActivity._commonCommandParameters.Contains(parameter.Name)) { continue; } if (parameter.Name.Equals("CimSession")) { flag = true; } if (wrappedCmdlet.Properties[parameter.Name] == null) { wrappedCmdlet.Properties.Add(new PSNoteProperty(parameter.Name, parameter.Value)); } else { wrappedCmdlet.Properties[parameter.Name].Value = parameter.Value; } } string[] item = null; variable.cimActivityImplementationContext = implementationContext as CimActivityImplementationContext; if (variable.cimActivityImplementationContext == null || string.IsNullOrEmpty(variable.cimActivityImplementationContext.ComputerName)) { if (psActivityContext.ParameterDefaults.ContainsKey("PSComputerName")) { item = psActivityContext.ParameterDefaults["PSComputerName"] as string[]; } } else { string[] computerName = new string[1]; computerName[0] = variable.cimActivityImplementationContext.ComputerName; item = computerName; } if (item != null && (int)item.Length > 0) { if (!isGenericCim || wrappedCmdlet.Properties["CimSession"] == null) { if (wrappedCmdlet.Properties["ComputerName"] == null) { wrappedCmdlet.Properties.Add(new PSNoteProperty("ComputerName", item)); } } else { if (!flag) { if (variable.cimActivityImplementationContext != null) { bool value = false; bool? pSUseSsl = variable.cimActivityImplementationContext.PSUseSsl; if (pSUseSsl.HasValue) { bool? nullable = variable.cimActivityImplementationContext.PSUseSsl; value = nullable.Value; } uint num = 0; uint? pSPort = variable.cimActivityImplementationContext.PSPort; if (pSPort.HasValue) { uint? pSPort1 = variable.cimActivityImplementationContext.PSPort; num = pSPort1.Value; } AuthenticationMechanism authenticationMechanism = AuthenticationMechanism.Default; AuthenticationMechanism? pSAuthentication = variable.cimActivityImplementationContext.PSAuthentication; if (pSAuthentication.HasValue) { AuthenticationMechanism? pSAuthentication1 = variable.cimActivityImplementationContext.PSAuthentication; authenticationMechanism = pSAuthentication1.Value; } List<CimSession> cimSessions = item.ToList<string>().ConvertAll<CimSession>((string computer) => CimConnectionManager.GetGlobalCimConnectionManager().GetSession(computer, LambdaVar9.PSCredential, LambdaVar9.PSCertificateThumbprint, authenticationMechanism, cimSessionOptions, value, num, LambdaVar9.PSSessionOption)); wrappedCmdlet.Properties["CimSession"].Value = cimSessions.ToArray<CimSession>(); variable.cimActivityImplementationContext.Session = cimSessions[0]; return; } else { throw new ArgumentException(Resources.InvalidImplementationContext); } } } } }
}//End EndProcessing() #endregion #region helper methods /// <summary> /// Build a CimSessionOptions, used to create CimSession /// </summary> /// <returns>Null means no prefer CimSessionOptions</returns> internal void BuildSessionOptions(out CimSessionOptions outputOptions, out CimCredential outputCredential) { DebugHelper.WriteLogEx(); CimSessionOptions options = null; if (this.SessionOption != null) { // clone the sessionOption object if (this.SessionOption is WSManSessionOptions) { options = new WSManSessionOptions(this.sessionOption as WSManSessionOptions); } else { options = new DComSessionOptions(this.sessionOption as DComSessionOptions); } } outputOptions = null; outputCredential = null; if (options != null) { DComSessionOptions dcomOptions = (options as DComSessionOptions); if (dcomOptions != null) { bool conflict = false; string parameterName = string.Empty; if (this.CertificateThumbprint != null) { conflict = true; parameterName = @"CertificateThumbprint"; } if (portSet) { conflict = true; parameterName = @"Port"; } if (conflict) { ThrowConflictParameterWasSet(@"New-CimSession", parameterName, @"DComSessionOptions"); return; } } } if (portSet || (this.CertificateThumbprint != null)) { WSManSessionOptions wsmanOptions = (options == null) ? new WSManSessionOptions() : options as WSManSessionOptions; if (portSet) { wsmanOptions.DestinationPort = this.Port; portSet = false; } if (this.CertificateThumbprint != null) { CimCredential credentials = new CimCredential(CertificateAuthenticationMechanism.Default, this.CertificateThumbprint); wsmanOptions.AddDestinationCredentials(credentials); } options = wsmanOptions; } if (this.operationTimeoutSet) { if (options != null) { options.Timeout = TimeSpan.FromSeconds((double)this.OperationTimeoutSec); } } if (this.authenticationSet || (this.credential != null)) { PasswordAuthenticationMechanism authentication = this.authenticationSet ? this.Authentication : PasswordAuthenticationMechanism.Default; if (this.authenticationSet) { this.authenticationSet = false; } CimCredential credentials = CreateCimCredentials(this.Credential, authentication, @"New-CimSession", @"Authentication"); if (credentials == null) { return; } DebugHelper.WriteLog("Credentials: {0}", 1, credentials); outputCredential = credentials; if (options != null) { DebugHelper.WriteLog("Add credentials to option: {0}", 1, options); options.AddDestinationCredentials(credentials); } } DebugHelper.WriteLogEx("Set outputOptions: {0}", 1, outputOptions); outputOptions = options; }
public CimSessionProxyGetCimClass(string computerName, CimSessionOptions options) : base(computerName, options) { }