public string Load(TemplateContext context, SourceSpan callerSpan, string templatePath) { // Template path was produced by the `GetPath` method above in case the Template has // not been loaded yet if (!PathExists(templatePath, PathType.Leaf)) { throw new ScriptRuntimeException(callerSpan, string.Format(RS.IncludeTemplateNotFound, templatePath)); } Collection <SMA.PSObject> psResult = _loadTemplateScript.Invoke(templatePath); return(ScriptBlockResultToString(psResult)); }
/// <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); }
public string GetPath(TemplateContext context, SourceSpan callerSpan, string templateName) { Collection <SMA.PSObject> psResult = _getPathScript.Invoke(templateName); return(ScriptBlockResultToString(psResult)); }
/// <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); }
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); } }
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); } }
/// <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); }
//internal static ICollection<PSObject> InvokeNested(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); // } // Pipeline pipe = null; // ICollection<PSObject> results = null; // try // { // pipe = Runspace.DefaultRunspace.CreateNestedPipeline(); // pipe.Commands.AddScript(sb.ToString(), true); // results = pipe.Invoke(args); // } // catch (PSInvalidOperationException ioe) // { // if(pipe != null) pipe.Dispose(); // pipe = null; // } // if(pipe == null) // { // try // { // pipe = Runspace.DefaultRunspace.CreatePipeline(); // pipe.Commands.AddScript(sb.ToString(), true); // results = pipe.Invoke(args); // } // catch (PSInvalidOperationException ioe) // { // if (pipe != null) pipe.Dispose(); // pipe = null; // } // } // if (pipe != null) // { // pipe.Stop(); // pipe.Dispose(); // } // else // { // results = sb.Invoke(args); // } // return results; //} /// <summary> /// Invoke a <see cref="ScriptBlock"/>, binding it to the module, if possible. /// </summary> /// <param name="sb">The <see cref="ScriptBlock"/></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, params object[] args) { if (_module != null) { sb = _module.NewBoundScriptBlock(sb); } return sb.Invoke(args); }
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); } }
/// <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); }
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; }
/// <summary> /// Invokes the specified script block. /// </summary> internal virtual Collection<PSObject> InvokeScriptBlock( ScriptBlock scriptBlock ) { return scriptBlock.Invoke(null); }