Ejemplo n.º 1
0
        private Dictionary <string, object> CreateCommonParameters()
        {
            Dictionary <string, object> parameterCollection = new Dictionary <string, object>();

            parameterCollection.Add(ScheduledJobInvocationInfo.RunAs32Parameter, RunAs32.ToBool());
            parameterCollection.Add(ScheduledJobInvocationInfo.AuthenticationParameter, Authentication);

            if (InitializationScript != null)
            {
                parameterCollection.Add(ScheduledJobInvocationInfo.InitializationScriptParameter, InitializationScript);
            }

            if (ArgumentList != null)
            {
                parameterCollection.Add(ScheduledJobInvocationInfo.ArgumentListParameter, ArgumentList);
            }

            return(parameterCollection);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a new parameter dictionary with update parameters.
        /// </summary>
        /// <returns>Updated parameters.</returns>
        private Dictionary <string, object> UpdateParameters()
        {
            Debug.Assert(_definition.InvocationInfo.Parameters.Count != 0,
                         "ScheduledJobDefinition must always have some job invocation parameters");
            Dictionary <string, object> newParameters = new Dictionary <string, object>();

            foreach (CommandParameter parameter in _definition.InvocationInfo.Parameters[0])
            {
                newParameters.Add(parameter.Name, parameter.Value);
            }

            // RunAs32
            if (MyInvocation.BoundParameters.ContainsKey(nameof(RunAs32)))
            {
                if (newParameters.ContainsKey(ScheduledJobInvocationInfo.RunAs32Parameter))
                {
                    newParameters[ScheduledJobInvocationInfo.RunAs32Parameter] = RunAs32.ToBool();
                }
                else
                {
                    newParameters.Add(ScheduledJobInvocationInfo.RunAs32Parameter, RunAs32.ToBool());
                }
            }

            // Authentication
            if (MyInvocation.BoundParameters.ContainsKey(nameof(Authentication)))
            {
                if (newParameters.ContainsKey(ScheduledJobInvocationInfo.AuthenticationParameter))
                {
                    newParameters[ScheduledJobInvocationInfo.AuthenticationParameter] = Authentication;
                }
                else
                {
                    newParameters.Add(ScheduledJobInvocationInfo.AuthenticationParameter, Authentication);
                }
            }

            // InitializationScript
            if (InitializationScript == null)
            {
                if (newParameters.ContainsKey(ScheduledJobInvocationInfo.InitializationScriptParameter))
                {
                    newParameters.Remove(ScheduledJobInvocationInfo.InitializationScriptParameter);
                }
            }
            else
            {
                if (newParameters.ContainsKey(ScheduledJobInvocationInfo.InitializationScriptParameter))
                {
                    newParameters[ScheduledJobInvocationInfo.InitializationScriptParameter] = InitializationScript;
                }
                else
                {
                    newParameters.Add(ScheduledJobInvocationInfo.InitializationScriptParameter, InitializationScript);
                }
            }

            // ScriptBlock
            if (ScriptBlock != null)
            {
                // FilePath cannot also be specified.
                if (newParameters.ContainsKey(ScheduledJobInvocationInfo.FilePathParameter))
                {
                    newParameters.Remove(ScheduledJobInvocationInfo.FilePathParameter);
                }

                if (newParameters.ContainsKey(ScheduledJobInvocationInfo.ScriptBlockParameter))
                {
                    newParameters[ScheduledJobInvocationInfo.ScriptBlockParameter] = ScriptBlock;
                }
                else
                {
                    newParameters.Add(ScheduledJobInvocationInfo.ScriptBlockParameter, ScriptBlock);
                }
            }

            // FilePath
            if (FilePath != null)
            {
                // ScriptBlock cannot also be specified.
                if (newParameters.ContainsKey(ScheduledJobInvocationInfo.ScriptBlockParameter))
                {
                    newParameters.Remove(ScheduledJobInvocationInfo.ScriptBlockParameter);
                }

                if (newParameters.ContainsKey(ScheduledJobInvocationInfo.FilePathParameter))
                {
                    newParameters[ScheduledJobInvocationInfo.FilePathParameter] = FilePath;
                }
                else
                {
                    newParameters.Add(ScheduledJobInvocationInfo.FilePathParameter, FilePath);
                }
            }

            // ArgumentList
            if (ArgumentList == null)
            {
                // Clear existing argument list only if new scriptblock or script file path was specified
                // (in this case old argument list is invalid).
                if (newParameters.ContainsKey(ScheduledJobInvocationInfo.ArgumentListParameter) &&
                    (ScriptBlock != null || FilePath != null))
                {
                    newParameters.Remove(ScheduledJobInvocationInfo.ArgumentListParameter);
                }
            }
            else
            {
                if (newParameters.ContainsKey(ScheduledJobInvocationInfo.ArgumentListParameter))
                {
                    newParameters[ScheduledJobInvocationInfo.ArgumentListParameter] = ArgumentList;
                }
                else
                {
                    newParameters.Add(ScheduledJobInvocationInfo.ArgumentListParameter, ArgumentList);
                }
            }

            return(newParameters);
        }
Ejemplo n.º 3
0
        // Module defining this command


        // Optional custom code for this activity


        /// <summary>
        /// Returns a configured instance of System.Management.Automation.PowerShell, pre-populated with the command to run.
        /// </summary>
        /// <param name="context">The NativeActivityContext for the currently running activity.</param>
        /// <returns>A populated instance of System.Management.Automation.PowerShell</returns>
        /// <remarks>The infrastructure takes responsibility for closing and disposing the PowerShell instance returned.</remarks>
        protected override ActivityImplementationContext GetPowerShell(NativeActivityContext context)
        {
            System.Management.Automation.PowerShell invoker       = global::System.Management.Automation.PowerShell.Create();
            System.Management.Automation.PowerShell targetCommand = invoker.AddCommand(PSCommandName);

            // Initialize the arguments

            if (DefinitionName.Expression != null)
            {
                targetCommand.AddParameter("DefinitionName", DefinitionName.Get(context));
            }

            if (DefinitionPath.Expression != null)
            {
                targetCommand.AddParameter("DefinitionPath", DefinitionPath.Get(context));
            }

            if (Type.Expression != null)
            {
                targetCommand.AddParameter("Type", Type.Get(context));
            }

            if (Name.Expression != null)
            {
                targetCommand.AddParameter("Name", Name.Get(context));
            }

            if (ScriptBlock.Expression != null)
            {
                targetCommand.AddParameter("ScriptBlock", ScriptBlock.Get(context));
            }

            if (Credential.Expression != null)
            {
                targetCommand.AddParameter("Credential", Credential.Get(context));
            }

            if (FilePath.Expression != null)
            {
                targetCommand.AddParameter("FilePath", FilePath.Get(context));
            }

            if (LiteralPath.Expression != null)
            {
                targetCommand.AddParameter("LiteralPath", LiteralPath.Get(context));
            }

            if (Authentication.Expression != null)
            {
                targetCommand.AddParameter("Authentication", Authentication.Get(context));
            }

            if (InitializationScript.Expression != null)
            {
                targetCommand.AddParameter("InitializationScript", InitializationScript.Get(context));
            }

            if (RunAs32.Expression != null)
            {
                targetCommand.AddParameter("RunAs32", RunAs32.Get(context));
            }

            if (PSVersion.Expression != null)
            {
                targetCommand.AddParameter("PSVersion", PSVersion.Get(context));
            }

            if (InputObject.Expression != null)
            {
                targetCommand.AddParameter("InputObject", InputObject.Get(context));
            }

            if (ArgumentList.Expression != null)
            {
                targetCommand.AddParameter("ArgumentList", ArgumentList.Get(context));
            }


            return(new ActivityImplementationContext()
            {
                PowerShellInstance = invoker
            });
        }