internal RunCommandsArguments(ActivityParameters activityParameters, PSDataCollection <PSObject> output, PSDataCollection <PSObject> input, PSActivityContext psActivityContext, PSWorkflowHost workflowHost, bool runInProc, Dictionary <string, object> parameterDefaults, Type activityType, PrepareSessionDelegate prepareSession, object activityObject, ActivityImplementationContext implementationContext) { this.ActivityParameters = activityParameters; this.Output = output; this.Input = input; this.PSActivityContext = psActivityContext; this.ParameterDefaults = parameterDefaults; this.ActivityType = activityType; this.Delegate = prepareSession; this.ActivityObject = activityObject; this.WorkflowHost = workflowHost; this.ImplementationContext = implementationContext; this.CommandExecutionType = RunCommandsArguments.DetermineCommandExecutionType(implementationContext.ConnectionInfo, runInProc, activityType, psActivityContext); }
internal static void CloseRunspaceAndDisposeCommand(System.Management.Automation.PowerShell currentCommand, PSWorkflowHost WorkflowHost, PSActivityContext psActivityContext, int commandType) { if (!currentCommand.IsRunspaceOwner && (currentCommand.Runspace.RunspaceStateInfo.State == RunspaceState.Opened || currentCommand.Runspace.RunspaceStateInfo.State == RunspaceState.Disconnected)) { PSActivity.CloseRunspace(currentCommand.Runspace, commandType, WorkflowHost, psActivityContext); } currentCommand.Dispose(); }
internal static int DetermineCommandExecutionType(WSManConnectionInfo connectionInfo, bool runInProc, Type activityType, PSActivityContext psActivityContext) { if (!typeof(PSCleanupActivity).IsAssignableFrom(activityType)) { if (connectionInfo == null) { if (runInProc) { if (typeof(WmiActivity).IsAssignableFrom(activityType) || typeof(GenericCimCmdletActivity).IsAssignableFrom(activityType)) { return(1); } else { if (!typeof(PSGeneratedCIMActivity).IsAssignableFrom(activityType)) { return(0); } else { return(4); } } } else { return(2); } } else { if (psActivityContext == null || !psActivityContext.RunWithCustomRemoting) { return(3); } else { return(0); } } } else { return(5); } }
private static bool CheckForCancel(PSActivityContext psActivityContext) { bool isCanceled = psActivityContext.IsCanceled; if (isCanceled) { PSActivity.RaiseTerminalCallback(psActivityContext); } return isCanceled; }
private static void CloseRunspace(Runspace runspace, int commandType = 0, PSWorkflowHost workflowHost = null, PSActivityContext psActivityContext = null) { int num = commandType; switch (num) { case 0: { workflowHost.LocalRunspaceProvider.ReleaseRunspace(runspace); return; } case 1: case 2: { return; } case 3: { PSActivity.UnregisterAndReleaseRunspace(runspace, workflowHost, psActivityContext); return; } case 4: { workflowHost.LocalRunspaceProvider.ReleaseRunspace(runspace); return; } default: { return; } } }
protected override void Execute(NativeActivityContext context) { object obj; WaitCallback waitCallback; bool value; object[] activityInstanceId = new object[1]; activityInstanceId[0] = context.ActivityInstanceId; this.Tracer.WriteMessage(string.Format(CultureInfo.InvariantCulture, "PowerShell activity ID={0}: Beginning execution.", activityInstanceId)); string displayName = base.DisplayName; if (string.IsNullOrEmpty(displayName)) { displayName = base.GetType().Name; } if (PSActivity._structuredTracer.IsEnabled) { PSActivity._structuredTracer.ActivityExecutionStarted(displayName, base.GetType().FullName); } PSSQMAPI.IncrementWorkflowActivityPresent(base.GetType().FullName); bool? activityPersistFlag = this.GetActivityPersistFlag(context); if (!activityPersistFlag.HasValue || !activityPersistFlag.HasValue) { value = false; } else { value = activityPersistFlag.Value; } bool flag = value; if (!flag) { NoPersistHandle noPersistHandle = this.noPersistHandle.Get(context); noPersistHandle.Enter(context); } this.bookmarking.Set(context, flag); Dictionary<string, object> strs = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase); HostParameterDefaults extension = context.GetExtension<HostParameterDefaults>(); if (extension != null) { Dictionary<string, object> parameters = extension.Parameters; foreach (KeyValuePair<string, object> parameter in parameters) { strs[parameter.Key] = parameter.Value; } if (strs.ContainsKey("PSComputerName") && strs["PSComputerName"] as string != null) { object[] item = new object[1]; item[0] = (string)strs["PSComputerName"]; strs["PSComputerName"] = item; } } if (!base.UseDefaultInput && strs.ContainsKey("Input")) { strs.Remove("Input"); } context.SetValue<Dictionary<string, object>>(this.ParameterDefaults, strs); PSActivityContext pSActivityContext = new PSActivityContext(); pSActivityContext.runningCommands = new Dictionary<PowerShell, RetryCount>(); pSActivityContext.commandQueue = new ConcurrentQueue<ActivityImplementationContext>(); pSActivityContext.IsCanceled = false; pSActivityContext.HostExtension = extension; GenericCimCmdletActivity genericCimCmdletActivity = this as GenericCimCmdletActivity; if (genericCimCmdletActivity != null) { pSActivityContext.TypeImplementingCmdlet = genericCimCmdletActivity.TypeImplementingCmdlet; } foreach (PSActivityArgumentInfo activityArgument in this.GetActivityArguments()) { Argument argument = activityArgument.Value; this.PopulateParameterFromDefault(argument, context, activityArgument.Name, strs); if (argument.Get(context) == null) { continue; } object[] name = new object[3]; name[0] = context.ActivityInstanceId; name[1] = activityArgument.Name; name[2] = argument.Get(context); this.Tracer.WriteMessage(string.Format(CultureInfo.InvariantCulture, "PowerShell activity ID={0}: Using parameter {1}, with value '{2}'.", name)); } PSDataCollection<PSObject> pSObjects = base.Input.Get(context); if (pSObjects == null || pSObjects.Count != 0) { bool flag1 = false; PSDataCollection<PSObject> pSObjects1 = base.Result.Get(context); if (pSObjects1 != null) { if (this.GetDisableSerialization(context)) { pSObjects1.SerializeInput = false; } else { pSObjects1.SerializeInput = true; } } if ((pSObjects1 == null || !pSObjects1.IsOpen) && base.Result.Expression != null) { if (pSObjects1 != null) { flag1 = true; } else { pSObjects1 = this.CreateOutputStream(context); } } else { if ((this.ParameterDefaults == null || !strs.ContainsKey("Result") || strs["Result"] != base.Result.Get(context)) && pSObjects1 != null) { bool flag2 = false; bool? appendOutput = base.AppendOutput; if (appendOutput.HasValue) { bool? nullable = base.AppendOutput; if (nullable.Value) { flag2 = true; } } if (!flag2) { flag1 = true; } } } pSActivityContext.errors = this.PSError.Get(context); if (this.PSError.Expression != null && (pSActivityContext.errors == null || pSActivityContext.errors.IsAutoGenerated)) { pSActivityContext.errors = new PSDataCollection<ErrorRecord>(); pSActivityContext.errors.IsAutoGenerated = true; this.PSError.Set(context, pSActivityContext.errors); object[] objArray = new object[1]; objArray[0] = context.ActivityInstanceId; this.Tracer.WriteMessage(string.Format(CultureInfo.InvariantCulture, "PowerShell activity ID={0}: No ErrorStream was passed in; creating a new stream.", objArray)); } bool? nullable1 = this.MergeErrorToOutput.Get(context); if (nullable1.HasValue) { bool? nullable2 = this.MergeErrorToOutput.Get(context); if (nullable2.GetValueOrDefault(false) && pSObjects1 != null && pSActivityContext.errors != null) { if (this.ParameterDefaults != null && strs.ContainsKey("PSError") && strs["PSError"] == this.PSError.Get(context)) { pSActivityContext.errors = new PSDataCollection<ErrorRecord>(); pSActivityContext.errors.IsAutoGenerated = true; this.PSError.Set(context, pSActivityContext.errors); object[] activityInstanceId1 = new object[1]; activityInstanceId1[0] = context.ActivityInstanceId; this.Tracer.WriteMessage(string.Format(CultureInfo.InvariantCulture, "PowerShell activity ID={0}: Merge error to the output stream and current error stream is the host default; creating a new stream.", activityInstanceId1)); } pSActivityContext.MergeErrorToOutput = true; } } PSDataCollection<ProgressRecord> progressRecords = this.PSProgress.Get(context); if (this.PSProgress.Expression != null && (progressRecords == null || progressRecords.IsAutoGenerated)) { progressRecords = new PSDataCollection<ProgressRecord>(); progressRecords.IsAutoGenerated = true; this.PSProgress.Set(context, progressRecords); object[] objArray1 = new object[1]; objArray1[0] = context.ActivityInstanceId; this.Tracer.WriteMessage(string.Format(CultureInfo.InvariantCulture, "PowerShell activity ID={0}: No ProgressStream was passed in; creating a new stream.", objArray1)); } pSActivityContext.progress = progressRecords; this.WriteProgressRecord(context, progressRecords, Resources.RunningString, ProgressRecordType.Processing); PSDataCollection<VerboseRecord> verboseRecords = this.PSVerbose.Get(context); if (this.PSVerbose.Expression != null && (verboseRecords == null || verboseRecords.IsAutoGenerated)) { verboseRecords = new PSDataCollection<VerboseRecord>(); verboseRecords.IsAutoGenerated = true; this.PSVerbose.Set(context, verboseRecords); object[] activityInstanceId2 = new object[1]; activityInstanceId2[0] = context.ActivityInstanceId; this.Tracer.WriteMessage(string.Format(CultureInfo.InvariantCulture, "PowerShell activity ID={0}: No VerboseStream was passed in; creating a new stream.", activityInstanceId2)); } PSDataCollection<DebugRecord> debugRecords = this.PSDebug.Get(context); if (this.PSDebug.Expression != null && (debugRecords == null || debugRecords.IsAutoGenerated)) { debugRecords = new PSDataCollection<DebugRecord>(); debugRecords.IsAutoGenerated = true; this.PSDebug.Set(context, debugRecords); object[] objArray2 = new object[1]; objArray2[0] = context.ActivityInstanceId; this.Tracer.WriteMessage(string.Format(CultureInfo.InvariantCulture, "PowerShell activity ID={0}: No DebugStream was passed in; creating a new stream.", objArray2)); } PSDataCollection<WarningRecord> warningRecords = this.PSWarning.Get(context); if (this.PSWarning.Expression != null && (warningRecords == null || warningRecords.IsAutoGenerated)) { warningRecords = new PSDataCollection<WarningRecord>(); warningRecords.IsAutoGenerated = true; this.PSWarning.Set(context, warningRecords); object[] activityInstanceId3 = new object[1]; activityInstanceId3[0] = context.ActivityInstanceId; this.Tracer.WriteMessage(string.Format(CultureInfo.InvariantCulture, "PowerShell activity ID={0}: No WarningStream was passed in; creating a new stream.", activityInstanceId3)); } pSObjects = base.Input.Get(context); if (pSObjects != null) { bool flag3 = false; if (this.ParameterDefaults != null && strs.ContainsKey("Input") && strs["Input"] == base.Input.Get(context)) { flag3 = true; } if (flag3) { PSDataCollection<PSObject> pSObjects2 = new PSDataCollection<PSObject>(pSObjects); pSObjects2.IsAutoGenerated = true; base.Input.Set(context, pSObjects2); pSObjects.Clear(); pSObjects = base.Input.Get(context); } } List<ActivityImplementationContext> tasks = this.GetTasks(context); foreach (ActivityImplementationContext task in tasks) { bool flag4 = false; PropertyInfo[] properties = base.GetType().GetProperties(); for (int i = 0; i < (int)properties.Length; i++) { PropertyInfo propertyInfo = properties[i]; if (typeof(Argument).IsAssignableFrom(propertyInfo.PropertyType)) { Argument value1 = (Argument)propertyInfo.GetValue(this, null); if (value1 != null && (value1.ArgumentType.IsAssignableFrom(typeof(ScriptBlock)) || value1.ArgumentType.IsAssignableFrom(typeof(ScriptBlock[])))) { flag4 = true; break; } } } if (flag4) { this.PopulateRunspaceFromContext(task, pSActivityContext, context); } pSActivityContext.commandQueue.Enqueue(task); } uint? nullable3 = this.PSActionRunningTimeoutSec.Get(context); activityInstanceId = new object[2]; activityInstanceId[0] = context.ActivityInstanceId; activityInstanceId[1] = nullable3; this.Tracer.WriteMessage(string.Format(CultureInfo.InvariantCulture, "PowerShell activity ID={0}: Max running time: {1}.", activityInstanceId)); if (nullable3.HasValue) { ActivityInstance activityInstance = context.ScheduleActivity(this.cancelTimer, new CompletionCallback(this.MaxRunTimeElapsed)); pSActivityContext.runningCancelTimer = activityInstance; } activityInstanceId = new object[1]; activityInstanceId[0] = context.ActivityInstanceId; this.Tracer.WriteMessage(string.Format(CultureInfo.InvariantCulture, "PowerShell activity ID={0}: Invoking command.", activityInstanceId)); PSActivity.OnActivityCreated(this, new ActivityCreatedEventArgs(null)); uint? nullable4 = null; uint? nullable5 = null; IImplementsConnectionRetry implementsConnectionRetry = this as IImplementsConnectionRetry; if (implementsConnectionRetry != null) { nullable4 = implementsConnectionRetry.PSConnectionRetryCount.Get(context); nullable5 = implementsConnectionRetry.PSConnectionRetryIntervalSec.Get(context); } List<string> strs1 = new List<string>(); if (!string.IsNullOrEmpty(this.PSDefiningModule)) { strs1.Add(this.PSDefiningModule); } string[] strArrays = this.PSRequiredModules.Get(context); if (strArrays != null) { strs1.AddRange(strArrays); } Action<object> activateDelegate = null; if (extension != null && extension.ActivateDelegate != null) { activateDelegate = extension.ActivateDelegate; } Guid empty = Guid.Empty; Guid guid = Guid.NewGuid(); Bookmark bookmark = context.CreateBookmark(string.Concat(PSActivity.PSBookmarkPrefix, guid.ToString()), new BookmarkCallback(this.OnResumeBookmark)); if (activateDelegate == null) { PSWorkflowInstanceExtension pSWorkflowInstanceExtension = context.GetExtension<PSWorkflowInstanceExtension>(); BookmarkContext bookmarkContext = new BookmarkContext(); bookmarkContext.CurrentBookmark = bookmark; bookmarkContext.BookmarkResumingExtension = pSWorkflowInstanceExtension; BookmarkContext bookmarkContext1 = bookmarkContext; waitCallback = new WaitCallback(PSActivity.OnComplete); obj = bookmarkContext1; } else { obj = bookmark; waitCallback = new WaitCallback(activateDelegate.Invoke); empty = extension.JobInstanceId; if (extension != null && extension.AsyncExecutionCollection != null) { Dictionary<string, PSActivityContext> asyncExecutionCollection = extension.AsyncExecutionCollection; if (asyncExecutionCollection != null) { if (asyncExecutionCollection.ContainsKey(context.ActivityInstanceId)) { asyncExecutionCollection.Remove(context.ActivityInstanceId); } asyncExecutionCollection.Add(context.ActivityInstanceId, pSActivityContext); } } } this.psActivityContextImplementationVariable.Set(context, pSActivityContext); pSActivityContext.Callback = waitCallback; pSActivityContext.AsyncState = obj; pSActivityContext.JobInstanceId = empty; pSActivityContext.ActivityParams = new ActivityParameters(nullable4, nullable5, this.PSActionRetryCount.Get(context), this.PSActionRetryIntervalSec.Get(context), strs1.ToArray()); pSActivityContext.Input = pSObjects; if (flag1) { pSObjects1 = this.CreateOutputStream(context); } pSActivityContext.Output = pSObjects1; pSActivityContext.WorkflowHost = PSActivity.GetWorkflowHost(extension); pSActivityContext.RunInProc = this.GetRunInProc(context); pSActivityContext.ParameterDefaults = strs; pSActivityContext.ActivityType = base.GetType(); PSActivity pSActivity = this; pSActivityContext.PrepareSession = new PrepareSessionDelegate(pSActivity.PrepareSession); pSActivityContext.ActivityObject = this; if (PSActivity.IsActivityInlineScript(this) && this.RunWithCustomRemoting(context)) { pSActivityContext.RunWithCustomRemoting = true; } context.SetValue<Dictionary<string, object>>(this.ParameterDefaults, strs); pSActivityContext.Execute(); if (PSActivity._structuredTracer.IsEnabled) { PSActivity._structuredTracer.ActivityExecutionFinished(displayName); } return; } else { object[] objArray3 = new object[1]; objArray3[0] = context.ActivityInstanceId; this.Tracer.WriteMessage(string.Format(CultureInfo.InvariantCulture, "PowerShell activity ID={0}: Execution skipped due to supplied (but empty) pipeline input.", objArray3)); return; } }
private static void WriteError(Exception exception, string errorId, ErrorCategory errorCategory, object originalTarget, PSActivityContext psActivityContext) { if (psActivityContext.errors == null) { lock (psActivityContext.exceptions) { psActivityContext.exceptions.Add(exception); } } else { ErrorRecord errorRecord = new ErrorRecord(exception, errorId, errorCategory, originalTarget); lock (psActivityContext.errors) { psActivityContext.errors.Add(errorRecord); } } }
private static void SetCurrentDirectory(PSActivityContext psActivityContext, Runspace runspace) { if (psActivityContext.ParameterDefaults != null && psActivityContext.ParameterDefaults.ContainsKey("PSCurrentDirectory")) { string item = psActivityContext.ParameterDefaults["PSCurrentDirectory"] as string; if (item != null) { runspace.SessionStateProxy.Path.SetLocation(item); } } }
private static void UnregisterAndReleaseRunspace(Runspace runspace, PSWorkflowHost workflowHost, PSActivityContext psActivityContext) { RunCommandsArguments runCommandsArgument = null; PSActivity.ArgsTableForRunspaces.TryRemove(runspace.InstanceId, out runCommandsArgument); if (psActivityContext.HandleRunspaceStateChanged != null) { runspace.StateChanged -= psActivityContext.HandleRunspaceStateChanged; } workflowHost.RemoteRunspaceProvider.ReleaseRunspace(runspace); }
private static void RaiseTerminalCallback(PSActivityContext psActivityContext) { lock (psActivityContext.SyncRoot) { if (psActivityContext.AllCommandsStarted || psActivityContext.commandQueue.Count == 0) { ThreadPool.QueueUserWorkItem(psActivityContext.Callback, psActivityContext.AsyncState); } } }
private static void RunDirectExecutionActivity(System.Management.Automation.PowerShell commandToRun, PSDataCollection<PSObject> input, PSDataCollection<PSObject> output, PSActivityContext psActivityContext, ActivityImplementationContext implementationContext) { PSDataCollection<PSObject> pSObjects; Type type; CimSessionOptions sessionOptions; Command item = commandToRun.Commands.Commands[0]; string commandText = item.CommandText; Cmdlet invokeWmiMethod = null; bool flag = false; if (!string.Equals(commandText, "Get-WMIObject", StringComparison.OrdinalIgnoreCase)) { if (string.Equals(commandText, "Invoke-WMIMethod", StringComparison.OrdinalIgnoreCase)) { invokeWmiMethod = new InvokeWmiMethod(); flag = true; } } else { invokeWmiMethod = new GetWmiObjectCommand(); } if (!PSActivity.CheckForCancel(psActivityContext)) { if (output != null) { pSObjects = output; } else { pSObjects = new PSDataCollection<PSObject>(); } ActivityImplementationContext activityImplementationContext = implementationContext; if (invokeWmiMethod != null) { type = invokeWmiMethod.GetType(); } else { type = psActivityContext.TypeImplementingCmdlet; } DirectExecutionActivitiesCommandRuntime directExecutionActivitiesCommandRuntime = new DirectExecutionActivitiesCommandRuntime(pSObjects, activityImplementationContext, type); directExecutionActivitiesCommandRuntime.Error = commandToRun.Streams.Error; directExecutionActivitiesCommandRuntime.Warning = commandToRun.Streams.Warning; directExecutionActivitiesCommandRuntime.Progress = commandToRun.Streams.Progress; directExecutionActivitiesCommandRuntime.Verbose = commandToRun.Streams.Verbose; directExecutionActivitiesCommandRuntime.Debug = commandToRun.Streams.Debug; if (invokeWmiMethod == null) { CimActivityImplementationContext cimActivityImplementationContext = implementationContext as CimActivityImplementationContext; if (cimActivityImplementationContext != null) { sessionOptions = cimActivityImplementationContext.SessionOptions; } else { sessionOptions = null; } CimSessionOptions cimSessionOption = sessionOptions; if (psActivityContext.TypeImplementingCmdlet != null) { if (input == null || input.Count <= 0 && !input.IsOpen) { CimBaseCommand cimBaseCommand = (CimBaseCommand)Activator.CreateInstance(psActivityContext.TypeImplementingCmdlet); using (cimBaseCommand) { cimBaseCommand.CommandRuntime = directExecutionActivitiesCommandRuntime; PSObject pSObject = PSObject.AsPSObject(cimBaseCommand); PSActivity.InitializeCmdletInstanceParameters(item, pSObject, true, psActivityContext, cimSessionOption, implementationContext); cimBaseCommand.Invoke().GetEnumerator().MoveNext(); } } else { if (psActivityContext.TypeImplementingCmdlet.GetProperty("InputObject") != null) { foreach (PSObject pSObject1 in input) { try { CimBaseCommand cimBaseCommand1 = (CimBaseCommand)Activator.CreateInstance(psActivityContext.TypeImplementingCmdlet); using (cimBaseCommand1) { cimBaseCommand1.CommandRuntime = directExecutionActivitiesCommandRuntime; CimInstance cimInstance = LanguagePrimitives.ConvertTo<CimInstance>(pSObject1); PSObject pSObject2 = PSObject.AsPSObject(cimBaseCommand1); PSActivity.InitializeCmdletInstanceParameters(item, pSObject2, true, psActivityContext, cimSessionOption, implementationContext); PSPropertyInfo pSPropertyInfo = pSObject2.Properties["InputObject"]; pSPropertyInfo.Value = cimInstance; cimBaseCommand1.Invoke().GetEnumerator().MoveNext(); } } catch (PSInvalidCastException pSInvalidCastException1) { PSInvalidCastException pSInvalidCastException = pSInvalidCastException1; if (pSInvalidCastException.ErrorRecord != null) { directExecutionActivitiesCommandRuntime.Error.Add(pSInvalidCastException.ErrorRecord); } } if (!PSActivity.CheckForCancel(psActivityContext)) { continue; } return; } } else { object[] objArray = new object[1]; objArray[0] = commandText; throw new NotImplementedException(string.Format(CultureInfo.CurrentCulture, Resources.CmdletDoesNotImplementInputObjectProperty, objArray)); } } } else { throw new InvalidOperationException(commandText); } } else { invokeWmiMethod.CommandRuntime = directExecutionActivitiesCommandRuntime; PSObject pSObject3 = PSObject.AsPSObject(invokeWmiMethod); PSActivity.InitializeCmdletInstanceParameters(item, pSObject3, false, psActivityContext, null, implementationContext); if (!flag || input == null || input.Count <= 0 && !input.IsOpen) { invokeWmiMethod.Invoke().GetEnumerator().MoveNext(); return; } else { InvokeWmiMethod invokeWmiMethod1 = invokeWmiMethod as InvokeWmiMethod; foreach (PSObject pSObject4 in input) { try { ManagementObject managementObject = LanguagePrimitives.ConvertTo<ManagementObject>(pSObject4); invokeWmiMethod1.InputObject = managementObject; invokeWmiMethod1.Invoke().GetEnumerator().MoveNext(); } catch (PSInvalidCastException pSInvalidCastException3) { PSInvalidCastException pSInvalidCastException2 = pSInvalidCastException3; if (pSInvalidCastException2.ErrorRecord != null) { directExecutionActivitiesCommandRuntime.Error.Add(pSInvalidCastException2.ErrorRecord); } } if (!PSActivity.CheckForCancel(psActivityContext)) { continue; } return; } } } return; } else { return; } }
private void PopulateRunspaceFromContext(ActivityImplementationContext implementationContext, PSActivityContext activityContext, NativeActivityContext context) { if (implementationContext.PowerShellInstance != null) { PropertyDescriptorCollection properties = context.DataContext.GetProperties(); foreach (PropertyDescriptor property in properties) { string name = property.Name; object value = property.GetValue(context.DataContext); if (value == null) { continue; } object item = value; PSDataCollection<PSObject> pSObjects = value as PSDataCollection<PSObject>; if (pSObjects != null && pSObjects.Count == 1) { item = pSObjects[0]; } activityContext.UserVariables.Add(name, item); } } }
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); } } } } }
private static bool HandleFailure(int attempts, uint? retryCount, uint? retryInterval, ActivityImplementationContext implementationContext, string errorId, Exception e, PSActivityContext psActivityContext) { bool flag = false; if ((long)attempts <= (ulong)retryCount.GetValueOrDefault(0)) { if (e != null) { PSActivity.WriteError(e, errorId, ErrorCategory.InvalidResult, implementationContext.PowerShellInstance.Runspace.ConnectionInfo, psActivityContext); } if (!psActivityContext.IsCanceled) { flag = true; } for (int i = 0; (long)i < (ulong)retryInterval.GetValueOrDefault(1) && !PSActivity.CheckForCancel(psActivityContext); i++) { Thread.Sleep(0x3e8); } } else { if (implementationContext.PSComputerName == null || (int)implementationContext.PSComputerName.Length <= 1) { if (e != null) { lock (psActivityContext.exceptions) { psActivityContext.exceptions.Add(e); } } } else { if (e != null) { PSActivity.WriteError(e, errorId, ErrorCategory.InvalidResult, implementationContext.PowerShellInstance.Runspace.ConnectionInfo, psActivityContext); } } } return flag; }
internal static int DetermineCommandExecutionType(WSManConnectionInfo connectionInfo, bool runInProc, Type activityType, PSActivityContext psActivityContext) { if (!typeof(PSCleanupActivity).IsAssignableFrom(activityType)) { if (connectionInfo == null) { if (runInProc) { if (typeof(WmiActivity).IsAssignableFrom(activityType) || typeof(GenericCimCmdletActivity).IsAssignableFrom(activityType)) { return 1; } else { if (!typeof(PSGeneratedCIMActivity).IsAssignableFrom(activityType)) { return 0; } else { return 4; } } } else { return 2; } } else { if (psActivityContext == null || !psActivityContext.RunWithCustomRemoting) { return 3; } else { return 0; } } } else { return 5; } }
private static void UpdatePowerShell(ActivityImplementationContext implementationContext, PSActivityContext psActivityContext, Type ActivityType, PrepareSessionDelegate PrepareSession, object activityObject) { try { PrepareSession(implementationContext); PowerShell powerShellInstance = implementationContext.PowerShellInstance; if (implementationContext.PSError != null) { powerShellInstance.Streams.Error = implementationContext.PSError; } if (implementationContext.PSProgress != null) { powerShellInstance.Streams.Progress = implementationContext.PSProgress; } if (implementationContext.PSVerbose != null) { powerShellInstance.Streams.Verbose = implementationContext.PSVerbose; } if (implementationContext.PSDebug != null) { powerShellInstance.Streams.Debug = implementationContext.PSDebug; } if (implementationContext.PSWarning != null) { powerShellInstance.Streams.Warning = implementationContext.PSWarning; } PSActivity pSActivity = activityObject as PSActivity; if (pSActivity.UpdatePreferenceVariable) { PSActivity.UpdatePreferenceVariables(implementationContext); } } catch (Exception exception1) { Exception exception = exception1; lock (psActivityContext.exceptions) { psActivityContext.exceptions.Add(exception); } } }
internal RunCommandsArguments(ActivityParameters activityParameters, PSDataCollection<PSObject> output, PSDataCollection<PSObject> input, PSActivityContext psActivityContext, PSWorkflowHost workflowHost, bool runInProc, Dictionary<string, object> parameterDefaults, Type activityType, PrepareSessionDelegate prepareSession, object activityObject, ActivityImplementationContext implementationContext) { this.ActivityParameters = activityParameters; this.Output = output; this.Input = input; this.PSActivityContext = psActivityContext; this.ParameterDefaults = parameterDefaults; this.ActivityType = activityType; this.Delegate = prepareSession; this.ActivityObject = activityObject; this.WorkflowHost = workflowHost; this.ImplementationContext = implementationContext; this.CommandExecutionType = RunCommandsArguments.DetermineCommandExecutionType(implementationContext.ConnectionInfo, runInProc, activityType, psActivityContext); }
private static void DecrementRunningCountAndCheckForEnd(PSActivityContext psActivityContext) { Interlocked.Decrement(ref psActivityContext.CommandsRunningCount); if (psActivityContext.CommandsRunningCount == 0) { PSActivity.RaiseTerminalCallback(psActivityContext); return; } else { return; } }