本文整理汇总了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 });
}
示例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;
}
示例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;
}
}