当前位置: 首页>>代码示例>>C#>>正文


C# PowerShell.AddParameters方法代码示例

本文整理汇总了C#中PowerShell.AddParameters方法的典型用法代码示例。如果您正苦于以下问题:C# PowerShell.AddParameters方法的具体用法?C# PowerShell.AddParameters怎么用?C# PowerShell.AddParameters使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PowerShell的用法示例。


在下文中一共展示了PowerShell.AddParameters方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Job

        /// <summary>
        /// New job.
        /// </summary>
        /// <remarks>
        /// Keep seconds for UI-less jobs: 0 ~ hidden mode, in this case a job creates UI on errors, as it is not attended.
        /// Other UI-less jobs are completely owned creators.
        /// </remarks>
        internal Job(JobCommand command, object parameters, string name, bool ui, int keepSeconds)
        {
            JobCommand = command;
            Parameters = parameters;
            Name = name;
            KeepSeconds = keepSeconds;

            // create/open runspace
            //! *) Do not catch, if we fail, we fail and there is nothing to repair yet (not open)
            //! *) Use existing configuration, it is faster! Most of *-Far* cmdlets should not be used,
            //! but some of them can be used, e.g. Update-FarDescription; also we want to use ETS types,
            //! e.g. FarDescription property.
            if (ui)
            {
                JobUI = new JobUI();
                Runspace = RunspaceFactory.CreateRunspace(new FarHost(JobUI), Runspace.DefaultRunspace.InitialSessionState);
            }
            else
            {
                //! DefaultHost is created internally. Perhaps it is reasonable to live with it, not with a custom host.
                Runspace = RunspaceFactory.CreateRunspace(Runspace.DefaultRunspace.InitialSessionState);
            }
            Runspace.Open();

            // new shell with the command
            PowerShell = PowerShell.Create();
            PowerShell.Runspace = Runspace;
            JobCommand.Add(PowerShell);

            // add command parameters
            if (parameters != null)
            {
                IDictionary namedParameters = parameters as IDictionary;
                IList argumentList;
                if (namedParameters != null)
                    PowerShell.AddParameters(namedParameters);
                else if ((argumentList = parameters as IList) != null)
                    PowerShell.AddParameters(argumentList);
                else
                    PowerShell.AddParameters(new object[] { parameters });
            }

            // UI: Write all output, including errors.
            if (JobUI != null)
            {
                PowerShell.Commands.AddCommand(A.OutHostCommand);
            }
            // Hidden: Write output to "Out-Null" to avoid memory use.
            else if (keepSeconds <= 0)
            {
                //! User can use his Out-Null
                PowerShell.AddCommand("Out-Null");
            }
            // Output: create it once: it is cumulative
            else
            {
                Output = new PSDataCollection<PSObject>();
            }
        }
开发者ID:pezipink,项目名称:FarNet,代码行数:66,代码来源:Job.cs

示例2: Start

        void Start(ScriptBlock scriptBlock, Hashtable parameters)
        {
            SessionStateAssemblyEntry windowsBase = new SessionStateAssemblyEntry(typeof(Dispatcher).Assembly.ToString());
            SessionStateAssemblyEntry presentationCore = new SessionStateAssemblyEntry(typeof(UIElement).Assembly.ToString());
            SessionStateAssemblyEntry presentationFramework = new SessionStateAssemblyEntry(typeof(Control).Assembly.ToString());
            initialSessionState.Assemblies.Add(windowsBase);
            initialSessionState.Assemblies.Add(presentationCore);
            initialSessionState.Assemblies.Add(presentationFramework);
            initialSessionState.Assemblies.Add(presentationFramework);
            runspace = RunspaceFactory.CreateRunspace(this.initialSessionState);
            runspace.ThreadOptions = PSThreadOptions.ReuseThread;
            runspace.ApartmentState = ApartmentState.STA;
            runspace.Open();
            powerShellCommand = PowerShell.Create();
            powerShellCommand.Runspace = runspace;
            jobThread = powerShellCommand.AddScript("[Threading.Thread]::CurrentThread").Invoke<Thread>()[0];

            powerShellCommand.Streams.Error = this.Error;
            this.Error.DataAdded += new EventHandler<DataAddedEventArgs>(Error_DataAdded);
            powerShellCommand.Streams.Warning = this.Warning;
            this.Warning.DataAdded += new EventHandler<DataAddedEventArgs>(Warning_DataAdded);
            powerShellCommand.Streams.Verbose = this.Verbose;
            this.Verbose.DataAdded += new EventHandler<DataAddedEventArgs>(Verbose_DataAdded);
            powerShellCommand.Streams.Debug = this.Debug;
            this.Debug.DataAdded += new EventHandler<DataAddedEventArgs>(Debug_DataAdded);
            powerShellCommand.Streams.Progress = this.Progress;
            this.Progress.DataAdded += new EventHandler<DataAddedEventArgs>(Progress_DataAdded);
            this.Output.DataAdded += new EventHandler<DataAddedEventArgs>(Output_DataAdded);
            powerShellCommand.Commands.Clear();
            powerShellCommand.Commands.AddScript(scriptBlock.ToString(), false);
            if (parameters.Count > 0)
            {
                powerShellCommand.AddParameters(parameters);
            }
            Collection<Visual> output = powerShellCommand.Invoke<Visual>();
            if (output.Count == 0)
            {
                return;
            }
            powerShellCommand.Commands.Clear();
            powerShellCommand.Commands.AddCommand("Show-Window").AddArgument(output[0]).AddParameter("OutputWindowFirst");
            Object var = powerShellCommand.Runspace.SessionStateProxy.GetVariable("NamedControls");
            if (var != null && ((var as Hashtable) != null))
            {
                namedControls = var as Hashtable;
            }
            JobDispatcher = Dispatcher.FromThread(jobThread);
            JobDispatcher.UnhandledException += new DispatcherUnhandledExceptionEventHandler(jobDispatcher_UnhandledException);
            powerShellCommand.InvocationStateChanged += new EventHandler<PSInvocationStateChangedEventArgs>(powerShellCommand_InvocationStateChanged);
            powerShellCommand.BeginInvoke<Object, PSObject>(null, this.Output);
            DateTime startTime = DateTime.Now;
            if (output[0] is FrameworkElement)
            {

                while (JobWindow == null)
                {
                    if ((DateTime.Now - startTime) > TimeSpan.FromSeconds(30))
                    {
                        this.SetJobState(JobState.Failed);
                        return;
                    }
                    System.Threading.Thread.Sleep(25);
                }
            }


        }
开发者ID:hugodahl,项目名称:powershell-for-developers,代码行数:67,代码来源:ShowUICore.CLR2.0.50727.5446.cs


注:本文中的PowerShell.AddParameters方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。