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


C# PowerShell.EndInvoke方法代码示例

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


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

示例1: InvokePowerShell

 private PSDataCollection<PSObject> InvokePowerShell(PowerShell powershell, RunspaceCreatedEventArgs args)
 {
     string str;
     HostInfo hostInfo = this.remoteHost.HostInfo;
     IAsyncResult asyncResult = new ServerPowerShellDriver(powershell, null, true, Guid.Empty, this.InstanceId, this, args.Runspace.ApartmentState, hostInfo, RemoteStreamOptions.AddInvocationInfo, false, args.Runspace).Start();
     PSDataCollection<PSObject> datas = powershell.EndInvoke(asyncResult);
     ArrayList dollarErrorVariable = (ArrayList) powershell.Runspace.GetExecutionContext.DollarErrorVariable;
     if (dollarErrorVariable.Count <= 0)
     {
         return datas;
     }
     ErrorRecord record = dollarErrorVariable[0] as ErrorRecord;
     if (record != null)
     {
         str = record.ToString();
     }
     else
     {
         Exception exception = dollarErrorVariable[0] as Exception;
         if (exception != null)
         {
             str = (exception.Message != null) ? exception.Message : string.Empty;
         }
         else
         {
             str = string.Empty;
         }
     }
     throw PSTraceSource.NewInvalidOperationException("RemotingErrorIdStrings", PSRemotingErrorId.StartupScriptThrewTerminatingError.ToString(), new object[] { str });
 }
开发者ID:nickchal,项目名称:pash,代码行数:30,代码来源:ServerRunspacePoolDriver.cs

示例2: InvokePowerShell

        /// <summary>
        /// Invokes a PowerShell instance 
        /// </summary>
        /// <param name="powershell"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        private PSDataCollection<PSObject> InvokePowerShell(PowerShell powershell, RunspaceCreatedEventArgs args)
        {
            Debug.Assert(powershell != null, "powershell shouldn't be null");

            // run the startup script on the runspace's host
            HostInfo hostInfo = _remoteHost.HostInfo;
            ServerPowerShellDriver driver = new ServerPowerShellDriver(
                powershell,
                null,
                true,
                Guid.Empty,
                this.InstanceId,
                this,
#if !CORECLR // No ApartmentState In CoreCLR
                args.Runspace.ApartmentState,
#endif                
                hostInfo,
                RemoteStreamOptions.AddInvocationInfo,
                false,
                args.Runspace);

            IAsyncResult asyncResult = driver.Start();

            // if there was an exception running the script..this may throw..this will
            // result in the runspace getting closed/broken.
            PSDataCollection<PSObject> results = powershell.EndInvoke(asyncResult);

            // find out if there are any error records reported. If there is one, report the error..
            // this will result in the runspace getting closed/broken.
            ArrayList errorList = (ArrayList)powershell.Runspace.GetExecutionContext.DollarErrorVariable;
            if (errorList.Count > 0)
            {
                string exceptionThrown;
                ErrorRecord lastErrorRecord = errorList[0] as ErrorRecord;
                if (lastErrorRecord != null)
                {
                    exceptionThrown = lastErrorRecord.ToString();
                }
                else
                {
                    Exception lastException = errorList[0] as Exception;
                    if (lastException != null)
                    {
                        exceptionThrown = (lastException.Message != null) ? lastException.Message : string.Empty;
                    }
                    else
                    {
                        exceptionThrown = string.Empty;
                    }
                }

                throw PSTraceSource.NewInvalidOperationException(RemotingErrorIdStrings.StartupScriptThrewTerminatingError, exceptionThrown);
            }

            return results;
        }
开发者ID:40a,项目名称:PowerShell,代码行数:62,代码来源:ServerRunspacePoolDriver.cs

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