/// <summary> /// Validates that each parameter argument matches the scriptblock /// </summary> /// <param name="element">object to validate</param> /// <exception cref="ValidationMetadataException">if <paramref name="element"/> is invalid</exception> protected override void ValidateElement(object element) { if (element == null) { throw new ValidationMetadataException("ArgumentIsEmpty", null); } object result = ScriptBlock.DoInvokeReturnAsIs(true, 2, element, null, null, new object[] { element }); if (!LanguagePrimitives.IsTrue(result)) { var errorMessageFormat = String.IsNullOrEmpty(ErrorMessage) ? "Error executing validation script: {0} against {{ {1} }}" : ErrorMessage; throw new ValidationMetadataException(String.Format(errorMessageFormat, element, ScriptBlock)); } }
/// <summary> /// Do a rich invocation of the scriptblock /// </summary> /// <param name="UseLocalScope">Whether a new scope should be created for this</param> /// <param name="DollerUnder">The value to make available as $_</param> /// <param name="Input">The value to make available to $input</param> /// <param name="ScriptThis">The value to make available as $this</param> /// <param name="ImportContext">Whether to first import the scriptblock into the current Context.</param> /// <param name="ImportGlobal">When importing the ScriptBlock, import it into the global Context instead.</param> /// <param name="Args">The value to make available as $args</param> /// <returns>Whatever output this scriptblock generates</returns> public System.Collections.ObjectModel.Collection <PSObject> InvokeEx(bool UseLocalScope, object DollerUnder, object Input, object ScriptThis, bool ImportContext, bool ImportGlobal, params object[] Args) { ScriptBlock tempScriptBlock = ScriptBlock; if (ImportContext) { tempScriptBlock = ScriptBlock.Clone().Import(ImportGlobal); } object result = tempScriptBlock.DoInvokeReturnAsIs(UseLocalScope, 2, DollerUnder, Input, ScriptThis, Args); if (result == null) { return(null); } if (result.GetType() == typeof(PSObject)) { return new System.Collections.ObjectModel.Collection <PSObject>() { result as PSObject } } ; return((System.Collections.ObjectModel.Collection <PSObject>)result); }
/// <summary> /// Do a rich invocation of the specified scriptblock /// </summary> /// <param name="ScriptCode">The scriptblock to execute</param> /// <param name="UseLocalScope">Whether a new scope should be created for this</param> /// <param name="DollerUnder">The value to make available as $_</param> /// <param name="Input">The value to make available to $input</param> /// <param name="ScriptThis">The value to make available as $this</param> /// <param name="Args">The value to make available as $args</param> /// <returns>Whatever output this scriptblock generates</returns> public object Invoke(string ScriptCode, bool UseLocalScope, object DollerUnder, object Input, object ScriptThis, object[] Args) { ScriptBlock script = ScriptBlock.Create(ScriptCode); return(script.DoInvokeReturnAsIs(UseLocalScope, 2, DollerUnder, Input, ScriptThis, Args)); }
/// <summary> /// Do a rich invocation of the specified scriptblock /// </summary> /// <param name="ScriptBlock">The scriptblock to execute</param> /// <param name="UseLocalScope">Whether a new scope should be created for this</param> /// <param name="DollerUnder">The value to make available as $_</param> /// <param name="Input">The value to make available to $input</param> /// <param name="ScriptThis">The value to make available as $this</param> /// <param name="Args">The value to make available as $args</param> /// <returns>Whatever output this scriptblock generates</returns> public object Invoke(ScriptBlock ScriptBlock, bool UseLocalScope, object DollerUnder, object Input, object ScriptThis, object[] Args) { return(ScriptBlock.DoInvokeReturnAsIs(UseLocalScope, 2, DollerUnder, Input, ScriptThis, Args)); }
private Collection <PSObject> InvokeScript(ScriptBlock sb, bool useNewScope, PipelineResultTypes writeToPipeline, IList input, params object[] args) { if (_cmdlet != null) { _cmdlet.ThrowIfStopping(); } Cmdlet cmdletToUse = null; ScriptBlock.ErrorHandlingBehavior errorHandlingBehavior = ScriptBlock.ErrorHandlingBehavior.WriteToExternalErrorPipe; // Check if they want output if ((writeToPipeline & PipelineResultTypes.Output) == PipelineResultTypes.Output) { cmdletToUse = _cmdlet; writeToPipeline &= (~PipelineResultTypes.Output); } // Check if they want error if ((writeToPipeline & PipelineResultTypes.Error) == PipelineResultTypes.Error) { errorHandlingBehavior = ScriptBlock.ErrorHandlingBehavior.WriteToCurrentErrorPipe; writeToPipeline &= (~PipelineResultTypes.Error); } if (writeToPipeline != PipelineResultTypes.None) { // The only output types are Output and Error. throw PSTraceSource.NewNotImplementedException(); } // If the cmdletToUse is not null, then the result of the evaluation will be // streamed out the output pipe of the cmdlet. object rawResult; if (cmdletToUse != null) { sb.InvokeUsingCmdlet( contextCmdlet: cmdletToUse, useLocalScope: useNewScope, errorHandlingBehavior: errorHandlingBehavior, dollarUnder: AutomationNull.Value, input: input, scriptThis: AutomationNull.Value, args: args); rawResult = AutomationNull.Value; } else { rawResult = sb.DoInvokeReturnAsIs( useLocalScope: useNewScope, errorHandlingBehavior: errorHandlingBehavior, dollarUnder: AutomationNull.Value, input: input, scriptThis: AutomationNull.Value, args: args); } if (rawResult == AutomationNull.Value) { return(new Collection <PSObject>()); } // If the result is already a collection of PSObjects, just return it... Collection <PSObject> result = rawResult as Collection <PSObject>; if (result != null) { return(result); } result = new Collection <PSObject>(); IEnumerator list = null; list = LanguagePrimitives.GetEnumerator(rawResult); if (list != null) { while (list.MoveNext()) { object val = list.Current; result.Add(LanguagePrimitives.AsPSObjectOrNull(val)); } } else { result.Add(LanguagePrimitives.AsPSObjectOrNull(rawResult)); } return(result); }