本文整理汇总了C#中System.Management.Automation.ScriptBlock.Invoke方法的典型用法代码示例。如果您正苦于以下问题:C# ScriptBlock.Invoke方法的具体用法?C# ScriptBlock.Invoke怎么用?C# ScriptBlock.Invoke使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Management.Automation.ScriptBlock
的用法示例。
在下文中一共展示了ScriptBlock.Invoke方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Invoke
/// <summary>
/// Invoke a <see cref="ScriptBlock"/>, binding it to the module, if possible.
/// </summary>
/// <param name="sb">The <see cref="ScriptBlock"/></param>
/// <param name="variables">Variables to set before invoking</param>
/// <param name="args">Arguments to the <see cref="ScriptBlock"/></param>
/// <returns>A collection of <see cref="PSObject"/></returns>
internal static ICollection<PSObject> Invoke(ScriptBlock sb, PSVariable[] variables, params object[] args)
{
if (variables == null)
throw new ArgumentNullException("variables");
foreach (var v in variables) SetScriptVariable(v);
if (_module != null)
{
sb = _module.NewBoundScriptBlock(sb);
}
return sb.Invoke(args);
}
示例2: SetKeyHandler
/// <summary>
/// Helper function for the Set-PSReadlineKeyHandler cmdlet.
/// </summary>
public static void SetKeyHandler(string[] key, ScriptBlock scriptBlock, string briefDescription, string longDescription)
{
Action<ConsoleKeyInfo?, object> handler = (k, arg) =>
{
try
{
scriptBlock.Invoke(k, arg);
}
catch (Exception e)
{
throw new CustomHandlerException(e);
}
};
_singleton.SetKeyHandlerInternal(key, handler, briefDescription, longDescription, scriptBlock);
}
示例3: runSBActionWithParams
private void runSBActionWithParams(
ScriptBlock sb,
object[] parameters)
{
Collection<PSObject> psObjects = null;
try {
this.WriteVerbose(
this,
"select whether a scriptblock has parameters or doesn't");
if (null == parameters || 0 == parameters.Length) {
this.WriteVerbose(
this,
"without parameters");
psObjects =
sb.Invoke();
} else {
this.WriteVerbose(
this,
"with parameters");
psObjects =
sb.Invoke(parameters);
}
this.WriteVerbose(
this,
"scriptblock has been fired successfully");
} catch (Exception eOuter) {
// 20130318
// ErrorRecord err =
// new ErrorRecord(eOuter,
// "ErrorInInvokingScriptBlock",
// ErrorCategory.InvalidOperation,
// System.Management.Automation.Runspaces.Runspace.DefaultRunspace);
// err.ErrorDetails =
// new ErrorDetails(
// "Unable to issue the following command:\r\n" +
// sb.ToString() +
// "\r\nThe exception raised is\r\n" +
// eOuter.Message);
// //"System.Management.Automation.Runspaces.Runspace.DefaultRunspace = RunspaceFactory.CreateRunspace();");
// WriteError(err);
// 20130606
// this.WriteVerbose(
// this,
// eOuter.InnerException.Message);
this.WriteError(
this,
"Unable to issue the following command:\r\n" +
sb.ToString() +
"\r\nThe exception raised is\r\n" +
eOuter.Message,
"ErrorInInvokingScriptBlock",
ErrorCategory.InvalidOperation,
// 20130318
//false);
true);
}
}
示例4: runSBAction
private void runSBAction(ScriptBlock sb,
AutomationElement src,
AutomationEventArgs e)
{
Collection<PSObject> psObjects = null;
try {
psObjects =
sb.Invoke();
// int counter = 0;
// foreach (PSObject pso in psObjects) {
// //if pso.
// counter++;
// WriteVerbose("result " + counter.ToString() + ":");
// WriteVerbose(pso.ToString());
// //WriteObject(pso.TypeNames
// foreach ( string typeName in pso.TypeNames) {
// WriteVerbose(typeName);
// }
// }
} catch (Exception eOuter) {
// 20130318
// ErrorRecord err =
// new ErrorRecord(eOuter,
// "ErrorInInvokingScriptBlock",
// ErrorCategory.InvalidOperation,
// System.Management.Automation.Runspaces.Runspace.DefaultRunspace);
// err.ErrorDetails =
// new ErrorDetails(
// "Unable to issue the following command:\r\n" +
// sb.ToString() +
// "\r\nThe exception raised is\r\n" +
// eOuter.Message);
// //"System.Management.Automation.Runspaces.Runspace.DefaultRunspace = RunspaceFactory.CreateRunspace();");
// WriteError(err);
this.WriteError(
this,
"Unable to issue the following command:\r\n" +
sb.ToString() +
"\r\nThe exception raised is\r\n" +
eOuter.Message,
"ErrorInInvokingScriptBlock",
ErrorCategory.InvalidOperation,
// 20130318
//false);
true);
}
}
示例5: InvokeScript
/// <summary>
/// Invokes the script block and returns the result collection.
/// </summary>
/// <param name="script">The script block to invoke.</param>
/// <param name="args">Script arguments.</param>
internal static Collection<PSObject> InvokeScript(ScriptBlock script, params object[] args)
{
if (Runspace.DefaultRunspace == null)
Runspace.DefaultRunspace = Psf.Runspace;
return script.Invoke(args);
}
示例6: RunSbActionWithParams
void RunSbActionWithParams(ScriptBlock sb, object[] parameters)
{
Collection<PSObject> psObjects = null;
try {
Trace.TraceInformation("runSBActionWithParams.1");
if (null == parameters || 0 == parameters.Length)
psObjects = sb.Invoke();
else
psObjects = sb.Invoke(parameters);
Trace.TraceInformation("runSBActionWithParams.2");
} catch (Exception eOuter) {
// TODO: AOP
Trace.TraceError("runSBActionWithParams(ScriptBlock sb, object[] parameters)");
Trace.TraceError(eOuter.Message);
throw new Exception(
"Unable to issue the following command:\r\n" +
sb +
"\r\nThe exception raised is\r\n" +
eOuter.Message);
}
}
示例7: SetKeyHandler
/// <summary>
/// Helper function for the Set-PSReadlineKeyHandler cmdlet.
/// </summary>
public static void SetKeyHandler(string[] key, ScriptBlock scriptBlock, string briefDescription, string longDescription)
{
Action<ConsoleKeyInfo?, object> handler =
(k, arg) => scriptBlock.Invoke(k, arg);
_singleton.SetKeyHandlerInternal(key, handler, briefDescription, longDescription, scriptBlock);
}
示例8: ArbitraryFilter
private static bool ArbitraryFilter(NtThread thread, ScriptBlock filter)
{
try
{
ICollection<PSObject> os = filter.Invoke(thread);
if (os.Count == 1)
{
return (bool)os.First().BaseObject;
}
}
catch
{
}
return false;
}
示例9: InvokeScriptBlock
/// <summary>
/// Invokes the specified script block.
/// </summary>
internal virtual Collection<PSObject> InvokeScriptBlock(
ScriptBlock scriptBlock
)
{
return scriptBlock.Invoke(null);
}