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


C# PowerShell.BeginInvoke方法代码示例

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


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

示例1: InvokePipeline

        void InvokePipeline(string code, bool addHistory)
        {
            // drop history cache
            History.Cache = null;

            // push writer
            FarUI.PushWriter(new EditorOutputWriter1(Editor));

            // invoke
            try
            {
                // history
                if (addHistory)
                {
                    code = code.Trim();
                    if (code.Length > 0 && code[code.Length - 1] != '#' && A.Psf._myLastCommand != code)
                    {
                        History.AddLine(code);
                        A.Psf._myLastCommand = code;
                    }
                }

                // invoke command
                PowerShell = PowerShell.Create();
                PowerShell.Runspace = Runspace;
                PowerShell.Commands
                    .AddScript(code)
                    .AddCommand(A.OutHostCommand);

                Editor.BeginAsync();
                PowerShell.BeginInvoke<PSObject>(null, null, AsyncInvoke, null);
            }
            catch (RuntimeException ex)
            {
                Far.Api.ShowError(Res.Me, ex);
            }
        }
开发者ID:AndBicScadMedia,项目名称:farnet,代码行数:37,代码来源:EditorConsole.cs

示例2: Start

 /// <summary>
 /// Called to start the COM handler.
 /// </summary>
 /// <param name="data">Data string passed in from Task Scheduler action.</param>
 public override void Start(string data)
 {
     psInstance = PowerShell.Create();
     psInstance.AddScript(data);
     result = psInstance.BeginInvoke();
     psInstance.InvocationStateChanged += PowerShellInstance_InvocationStateChanged;
 }
开发者ID:tablesmit,项目名称:task-scheduler-managed-wrapper,代码行数:11,代码来源:PSTaskHandler.cs

示例3: RunScript

	    private async Task<bool> RunScript(string path)
	    {
            PowerShellInstance = PowerShell.Create();

            PowerShellInstance.AddScript(LoadScript(path));

            PowerShellInstance.AddArgument(CredentialHelper.GetFormRegistery(CredentialHelper.SourceRepoUserName));
            PowerShellInstance.AddArgument(CredentialHelper.GetFormRegistery(CredentialHelper.SourceRepoPassword));
            PowerShellInstance.AddArgument(CredentialHelper.GetFormRegistery(CredentialHelper.TargetRepoUserName));
            PowerShellInstance.AddArgument(CredentialHelper.GetFormRegistery(CredentialHelper.TargetRepoPassword));

            PSDataCollection<PSObject> outputCollection = new PSDataCollection<PSObject>();
            outputCollection.DataAdded += outputCollection_DataAdded;

            PowerShellInstance.Streams.Error.DataAdded += Error_DataAdded;

            IAsyncResult result = PowerShellInstance.BeginInvoke<PSObject,PSObject>(null, outputCollection);

	        while (result.IsCompleted == false)
	        {
                await Task.Delay(100);
	        }
	        return PowerShellInstance.HadErrors;
	    }
开发者ID:Orckestra,项目名称:C1-Packages,代码行数:24,代码来源:Form1.cs

示例4: 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

示例5: ExecuteScript

        public void ExecuteScript(ScriptConfigElement e)
        {
            using (_instance = PowerShell.Create())
            {
                _instance.AddScript(File.ReadAllText(e.PathToScript));

                PSDataCollection<PSObject> outputCollection = new PSDataCollection<PSObject>();
                outputCollection.DataAdded += outputCollection_DataAdded;
                _instance.Streams.Progress.DataAdded += Progress_DataAdded;
                _instance.Streams.Error.DataAdded += Error_DataAdded;
                _instance.Streams.Verbose.DataAdded += Verbose_DataAdded;
                _instance.Streams.Debug.DataAdded += Debug_DataAdded;
                _instance.Streams.Warning.DataAdded += Warning_DataAdded;

                IAsyncResult result = _instance.BeginInvoke<PSObject, PSObject>(null,
                    outputCollection);

                while (result.IsCompleted == false)
                {
                    Thread.Sleep(500);
                }

                foreach (PSObject o in outputCollection)
                {
                    Console.WriteLine(o.GetType());
                    Console.WriteLine(o);
                }
            }
        }
开发者ID:ClearMeasure,项目名称:PowerShellRunnerService,代码行数:29,代码来源:PowerShellRunner.cs

示例6: executeScript

        /// <summary>
        /// This is the primary method for executing powershell scripts
        /// </summary>
        /// <param name="script"></param>
        /// <param name="args"></param>(optional)
        /// <returns>ICollection<PSObject></returns>
        public ICollection<PSObject> executeScript(string script, IDictionary<string, object> args = null)
        {
            try
            {
                // create runspace if it is null
                if (_runspace == null)
                {
                    _runspace = createRunspace();
                }

                // The PowerShell class implements a Factory Pattern, offering a Create() method that returns a new PowerShell object
                _ps = PowerShell.Create();

                // assign the runspace to the Powershell object
                _ps.Runspace = _runspace;

                // create a Command object, initializing it with the script path
                Command psCommand = new Command(script);

                // if the args Dictionary is not null, add them to the Command.Parameters collection
                if (args != null)
                {
                    foreach (var arg in args)
                    {
                        psCommand.Parameters.Add(arg.Key, arg.Value);
                    }
                }

                // add the psCommands object to the Commands property of our PowerShell object
                _ps.Commands.Commands.Add(psCommand);

                // Invoke PowerShell asynchronously
                var asyncResult = _ps.BeginInvoke();

                // Could perform other tasks here while waiting for script to complete, if needed

                // this is analogous to the "await" keyword in an async method
                asyncResult.AsyncWaitHandle.WaitOne();

                // get the result from PowerShell execution
                var result = _ps.EndInvoke(asyncResult);

                // release the resources used by the WaitHandle
                asyncResult.AsyncWaitHandle.Close();

                // return the collection of PSObjects
                return result;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                return null;
            }
        }
开发者ID:singhn83,项目名称:WPF_StarterProjectv0.1,代码行数:60,代码来源:PSEngine.cs


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