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