Example #1
0
        public bool Execute()
        {
            ActivityImplementationContext activityImplementationContext = null;

            if (this.commandQueue != null)
            {
                bool flag = this.commandQueue.TryDequeue(out activityImplementationContext);
                lock (this.SyncRoot)
                {
                    while (flag)
                    {
                        RunCommandsArguments runCommandsArgument = new RunCommandsArguments(this.ActivityParams, this.Output, this.Input, this, this.WorkflowHost, this.RunInProc, this.ParameterDefaults, this.ActivityType, this.PrepareSession, this.ActivityObject, activityImplementationContext);
                        Interlocked.Increment(ref this.CommandsRunningCount);
                        PSActivity.BeginRunOneCommand(runCommandsArgument);
                        flag = this.commandQueue.TryDequeue(out activityImplementationContext);
                    }
                    this.AllCommandsStarted = true;
                }
                return(true);
            }
            else
            {
                throw new InvalidOperationException("commandQueue");
            }
        }
Example #2
0
 public virtual void StartResumablePSCommand(Guid jobInstanceId,
                                             Bookmark bookmark,
                                             System.Management.Automation.PowerShell command,
                                             PowerShellStreams <PSObject, PSObject> streams,
                                             PSActivityEnvironment environment,
                                             PSActivity activityInstance)
 {
     throw new NotImplementedException();
 }
Example #3
0
        private bool CheckIfSuspendable(Activity activity)
        {
            bool flag;

            if (!string.Equals(activity.GetType().ToString(), "Microsoft.PowerShell.Activities.PSPersist", StringComparison.OrdinalIgnoreCase))
            {
                if (!string.Equals(activity.GetType().ToString(), "Microsoft.PowerShell.Activities.Suspend", StringComparison.OrdinalIgnoreCase))
                {
                    PSActivity pSActivity = activity as PSActivity;
                    if (pSActivity == null || pSActivity.PSPersist == null || pSActivity.PSPersist.Expression == null)
                    {
                        Sequence sequence = activity as Sequence;
                        if (sequence != null && sequence.Variables != null && sequence.Variables.Count > 0)
                        {
                            foreach (Variable variable in sequence.Variables)
                            {
                                if (!string.Equals(variable.Name, "PSPersistPreference", StringComparison.OrdinalIgnoreCase))
                                {
                                    continue;
                                }
                                flag = true;
                                return(flag);
                            }
                        }
                        Parallel parallel = activity as Parallel;
                        if (parallel != null && parallel.Variables != null && parallel.Variables.Count > 0)
                        {
                            foreach (Variable variable1 in parallel.Variables)
                            {
                                if (!string.Equals(variable1.Name, "PSPersistPreference", StringComparison.OrdinalIgnoreCase))
                                {
                                    continue;
                                }
                                flag = true;
                                return(flag);
                            }
                        }
                        return(false);
                    }
                    else
                    {
                        return(true);
                    }
                }
                else
                {
                    return(true);
                }
            }
            else
            {
                return(true);
            }
        }
Example #4
0
        protected override List <ActivityImplementationContext> GetImplementation(NativeActivityContext context)
        {
            bool hasValue;

            string[]        strArrays       = this.PSComputerName.Get(context);
            string[]        strArrays1      = this.PSConnectionUri.Get(context);
            PSSessionOption pSSessionOption = this.PSSessionOption.Get(context);
            List <ActivityImplementationContext> activityImplementationContexts = new List <ActivityImplementationContext>();
            RemotingBehavior remotingBehavior = this.PSRemotingBehavior.Get(context);

            if (this.PSRemotingBehavior.Expression == null)
            {
                remotingBehavior = RemotingBehavior.PowerShell;
            }
            if (remotingBehavior != RemotingBehavior.Custom || this.SupportsCustomRemoting)
            {
                if (this.PSCredential.Get(context) != null)
                {
                    AuthenticationMechanism?nullable = this.PSAuthentication.Get(context);
                    if (nullable.GetValueOrDefault() != AuthenticationMechanism.NegotiateWithImplicitCredential)
                    {
                        hasValue = false;
                    }
                    else
                    {
                        hasValue = nullable.HasValue;
                    }
                    if (hasValue)
                    {
                        throw new ArgumentException(Resources.CredentialParameterCannotBeSpecifiedWithNegotiateWithImplicitAuthentication);
                    }
                }
                if ((remotingBehavior == RemotingBehavior.PowerShell || PSActivity.IsActivityInlineScript(this) && base.RunWithCustomRemoting(context)) && (this.GetIsComputerNameSpecified(context) || strArrays1 != null && (int)strArrays1.Length > 0))
                {
                    AuthenticationMechanism?nullable1 = this.PSAuthentication.Get(context);
                    bool?nullable2 = this.PSAllowRedirection.Get(context);
                    List <WSManConnectionInfo> connectionInfo = ActivityUtils.GetConnectionInfo(strArrays, strArrays1, this.PSCertificateThumbprint.Get(context), this.PSConfigurationName.Get(context), this.PSUseSsl.Get(context), this.PSPort.Get(context), this.PSApplicationName.Get(context), this.PSCredential.Get(context), nullable1.GetValueOrDefault(AuthenticationMechanism.Default), nullable2.GetValueOrDefault(false), pSSessionOption);
                    foreach (WSManConnectionInfo wSManConnectionInfo in connectionInfo)
                    {
                        this.CreatePowerShellInstance(context, wSManConnectionInfo, activityImplementationContexts);
                    }
                }
                else
                {
                    this.CreatePowerShellInstance(context, null, activityImplementationContexts);
                }
                return(activityImplementationContexts);
            }
            else
            {
                throw new ArgumentException(Resources.CustomRemotingNotSupported);
            }
        }
        private bool CheckIfSuspendable(Activity activity)
        {
            if (string.Equals(activity.GetType().ToString(), "Microsoft.PowerShell.Activities.PSPersist", StringComparison.OrdinalIgnoreCase))
            {
                return(true);
            }

            if (string.Equals(activity.GetType().ToString(), "Microsoft.PowerShell.Activities.Suspend", StringComparison.OrdinalIgnoreCase))
            {
                return(true);
            }

            PSActivity psactivity = activity as PSActivity;

            if (psactivity != null && psactivity.PSPersist != null && psactivity.PSPersist.Expression != null)
            {
                return(true);
            }

            Sequence seqActivity = activity as Sequence;

            if (seqActivity != null && seqActivity.Variables != null && seqActivity.Variables.Count > 0)
            {
                foreach (Variable var in seqActivity.Variables)
                {
                    if (string.Equals(var.Name, WorkflowPreferenceVariables.PSPersistPreference, StringComparison.OrdinalIgnoreCase))
                    {
                        return(true);
                    }
                }
            }

            Parallel parActivity = activity as Parallel;

            if (parActivity != null && parActivity.Variables != null && parActivity.Variables.Count > 0)
            {
                foreach (Variable var in parActivity.Variables)
                {
                    if (string.Equals(var.Name, WorkflowPreferenceVariables.PSPersistPreference, StringComparison.OrdinalIgnoreCase))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
        // This function executes the activity.
        // It is highly recommended to not to block the execution of this function while executing the activity.
        // All the information should be logged into the queue and the function should be returned.
        // In a separate thread or on a separate machine, read the data from the queue and execute the activity.
        // Once the activity action is completed the bookmark should be resumed.
        // Design the activity controller to hanlde multiple activities from one workflowflow.
        public override void StartResumablePSCommand(Guid jobInstanceId, Bookmark bookmark, PowerShell command, PowerShellStreams <PSObject, PSObject> streams, PSActivityEnvironment environment, PSActivity activityInstance)
        {
            ActivityActionData data = new ActivityActionData();

            data.jobInstanceId = jobInstanceId;
            data.bookmark      = bookmark;
            data.command       = command;
            data.streams       = streams;
            data.environment   = environment;

            // Add the request to the queue.
            ActivityActionsQueue.TryAdd(jobInstanceId, data);


            // Return the fucntion and allow the workfow do other work in parallel.
            // There should be a servicing thead which gets the data from the queue and perform the action.
            // To keep this sample simple, the worker thread calls the servicing function.
            ThreadPool.QueueUserWorkItem(ServiceRequests, jobInstanceId);
        }
Example #7
0
        public void Cancel()
        {
            ActivityImplementationContext activityImplementationContext = null;
            IAsyncResult asyncResult = null;

            if (this.WorkflowHost != null)
            {
                if (this.commandQueue != null)
                {
                    while (!this.commandQueue.IsEmpty)
                    {
                        bool flag = this.commandQueue.TryDequeue(out activityImplementationContext);
                        if (!flag)
                        {
                            continue;
                        }
                        PowerShell powerShellInstance = activityImplementationContext.PowerShellInstance;
                        object[]   objArray           = new object[1];
                        objArray[0] = powerShellInstance;
                        this._tracer.WriteMessage(string.Format(CultureInfo.InvariantCulture, "PowerShell activity: Cancelling pending command {0}.", objArray));
                        powerShellInstance.Dispose();
                    }
                }
                PSResumableActivityHostController pSActivityHostController = this.WorkflowHost.PSActivityHostController as PSResumableActivityHostController;
                if (pSActivityHostController == null)
                {
                    PSOutOfProcessActivityController pSOutOfProcessActivityController = this.WorkflowHost.PSActivityHostController as PSOutOfProcessActivityController;
                    if (pSOutOfProcessActivityController != null)
                    {
                        while (this.AsyncResults.Count > 0)
                        {
                            this.AsyncResults.TryDequeue(out asyncResult);
                            pSOutOfProcessActivityController.CancelInvokePowerShell(asyncResult);
                        }
                    }
                }
                else
                {
                    pSActivityHostController.StopAllResumablePSCommands(this.JobInstanceId);
                }
                while (this.runningCommands.Count > 0)
                {
                    PowerShell powerShell = null;
                    lock (this.runningCommands)
                    {
                        Dictionary <PowerShell, RetryCount> .KeyCollection.Enumerator enumerator = this.runningCommands.Keys.GetEnumerator();
                        try
                        {
                            if (enumerator.MoveNext())
                            {
                                PowerShell current = enumerator.Current;
                                powerShell = current;
                            }
                        }
                        finally
                        {
                            enumerator.Dispose();
                        }
                        if (powerShell == null)
                        {
                            break;
                        }
                    }
                    if (powerShell.InvocationStateInfo.State == PSInvocationState.Running)
                    {
                        object[] objArray1 = new object[1];
                        objArray1[0] = powerShell;
                        this._tracer.WriteMessage(string.Format(CultureInfo.InvariantCulture, "PowerShell activity: Stopping command {0}.", objArray1));
                        try
                        {
                            powerShell.Stop();
                        }
                        catch (NullReferenceException nullReferenceException)
                        {
                        }
                        catch (InvalidOperationException invalidOperationException)
                        {
                        }
                    }
                    if (powerShell.InvocationStateInfo.State != PSInvocationState.Completed || powerShell.HadErrors)
                    {
                        this.Failed = true;
                    }
                    int num = RunCommandsArguments.DetermineCommandExecutionType(powerShell.Runspace.ConnectionInfo as WSManConnectionInfo, this.RunInProc, this.ActivityType, this);
                    if (num != 1)
                    {
                        PSActivity.CloseRunspaceAndDisposeCommand(powerShell, this.WorkflowHost, this, num);
                    }
                    this.runningCommands.Remove(powerShell);
                }
                return;
            }
            else
            {
                throw new InvalidOperationException("WorkflowHost");
            }
        }