private TSR.ScriptObject ConvertPSObjectToScriptObject(PSObject psObject, int recurseDepth = 0) { if (psObject == null) { return(null); } TSR.ScriptObject templateScriptObj = new TSR.ScriptObject(); foreach (PSPropertyInfo psProperty in psObject.Properties) { string propertyName = psProperty.Name; object propertyValue = psProperty.Value; TSR.ScriptObject convertedPropertyValue = null; TSR.ScriptArray convertedPropertyArrayValue = null; // handle object arrays if (propertyValue is Array) { if ((_infiniteDepth == true) || (recurseDepth < this.Depth)) { convertedPropertyArrayValue = ConvertArrayObject((SC.IEnumerable)propertyValue, recurseDepth + 1); } else { base.WriteWarning(string.Format(RS.MaxDepthReached, propertyName, "IEnumerable", this.Depth)); } if (convertedPropertyArrayValue != null) { templateScriptObj.Add(propertyName, convertedPropertyArrayValue); } else { templateScriptObj.Add(propertyName, propertyValue); } } else { if (propertyValue is SC.Hashtable) { if (_convertHashtableRecurse && ((_infiniteDepth == true) || (recurseDepth < this.Depth))) { convertedPropertyValue = ConvertHashtableToScriptObject((SC.Hashtable)propertyValue, recurseDepth + 1); } else { // only warn if it's because max depth is reached if (_convertHashtableRecurse == true) { base.WriteWarning(string.Format(RS.MaxDepthReached, propertyName, "Hashtable", this.Depth)); } } } else if (propertyValue is PSObject) { if ((_infiniteDepth == true) || (recurseDepth < this.Depth)) { convertedPropertyValue = ConvertPSObjectToScriptObject((PSObject)propertyValue, recurseDepth + 1); } else { base.WriteWarning(string.Format(RS.MaxDepthReached, propertyName, "PSObject", this.Depth)); } } else { // do nothing //base.WriteObject(string.Format("{0} is {1}", propertyName, propertyValue.GetType().ToString())); } if (convertedPropertyValue != null) { templateScriptObj.Add(propertyName, convertedPropertyValue); } else { templateScriptObj.Add(propertyName, propertyValue); } } } return(templateScriptObj); }
private TSR.ScriptObject ConvertHashtableToScriptObject(SC.Hashtable hashtable, int recurseDepth = 0) { if (hashtable == null) { return(null); } TSR.ScriptObject templateScriptObj = new TSR.ScriptObject(); SC.ICollection keys = hashtable.Keys; foreach (object key in keys) { string propertyName = key.ToString(); object propertyValue = hashtable[key]; TSR.ScriptObject convertedPropertyValue = null; TSR.ScriptArray convertedPropertyArrayValue = null; if (propertyValue is Array) { if ((_infiniteDepth == true) || (recurseDepth < this.Depth)) { convertedPropertyArrayValue = ConvertArrayObject((SC.IEnumerable)propertyValue, recurseDepth + 1); } if (convertedPropertyArrayValue != null) { templateScriptObj.Add(propertyName, convertedPropertyArrayValue); } else { templateScriptObj.Add(propertyName, propertyValue); } } else { // recursively convert property value to script object // supports hashtable, psobject and pscustomobject if (propertyValue is SC.Hashtable) { if (_convertHashtableRecurse && ((_infiniteDepth == true) || (recurseDepth < this.Depth))) { convertedPropertyValue = ConvertHashtableToScriptObject((SC.Hashtable)propertyValue, recurseDepth + 1); } //base.WriteObject(string.Format("{0} is hashtable", propertyName)); } else if (propertyValue is PSObject) { if ((_infiniteDepth == true) || (recurseDepth < this.Depth)) { convertedPropertyValue = ConvertPSObjectToScriptObject((PSObject)propertyValue, recurseDepth + 1); } //base.WriteObject(string.Format("{0} is psobject", propertyName)); } else { // do nothing } if (convertedPropertyValue != null) { templateScriptObj.Add(propertyName, convertedPropertyValue); } else { templateScriptObj.Add(propertyName, propertyValue); } } } return(templateScriptObj); }
/// <see cref="Cmdlet.EndProcessing()" /> protected override void EndProcessing() { // ignore if template is empty if (string.IsNullOrEmpty(this.Template)) { // returns an empty string if input is an empty string if (this.Template != null) { base.WriteObject(string.Empty); } return; } // template runtime TS.Template templateRuntime = null; // parse first try { TSP.ParserOptions parserOptions = new TSP.ParserOptions(); if (!_infiniteNesting && this.NestingLimit > 0) { parserOptions.ExpressionDepthLimit = this.NestingLimit; } templateRuntime = TS.Template.Parse(this.Template, null, parserOptions); // this is mostly to output warnings if (templateRuntime != null && templateRuntime.Messages != null && templateRuntime.Messages.Count > 0) { WriteTemplateParseMessages(templateRuntime.Messages); } } catch (Exception ex) { // the exception will contain all errors and warnings ErrorRecord parseErrorRecord = new ErrorRecord(ex, "TemplateParseFailure", ErrorCategory.ParserError, null); base.ThrowTerminatingError(parseErrorRecord); } // add variable if specified // then return rendered result ITemplateLoader templateLoader = null; if (_fileAccess) { string getFSLocationScript = "Get-Location -PSProvider FileSystem | Select-Object -ExpandProperty Path"; string currentDirectory = ScriptBlock.Create(getFSLocationScript).Invoke()[0].ToString(); templateLoader = new LocalFileTemplateLoader(currentDirectory); } else if (_overrideFileSystemProvider) { templateLoader = new PSCustomFileTemplateLoader(_getPathScript, _loadFileScript); } TS.TemplateContext templateContext = new TS.TemplateContext() { // loads file from disk using the 'include' keyword TemplateLoader = templateLoader }; try { if (_strictMode) { templateContext.StrictVariables = true; templateContext.EnableRelaxedMemberAccess = false; } else { templateContext.StrictVariables = false; templateContext.EnableRelaxedMemberAccess = true; } // limit loops templateContext.LoopLimit = this.LoopLimit; // limit recursive calls templateContext.RecursiveLimit = this.StackLimit; // set timeout templateContext.RegexTimeOut = _timeout; if (this.InputObject != null) { if (this.InputObject is SC.Hashtable) { _templateVariable = ConvertHashtableToScriptObject((SC.Hashtable) this.InputObject); } else if (this.InputObject is PSObject) { _templateVariable = ConvertPSObjectToScriptObject((PSObject)this.InputObject); } else { throw new ArgumentException(string.Format(RS.ArgumentTypeNotSupported, "hashtable, PSObject"), "InputObject"); } templateContext.PushGlobal(_templateVariable); } Task <string> task = Task <string> .Run(() => { return(templateRuntime.Render(templateContext)); }); bool completed = task.Wait(_timeout); if (!completed) { throw new TimeoutException(); } else { base.WriteObject(task.Result); } if (task.IsFaulted == true && task.Exception != null) { throw task.Exception; } } catch (AggregateException aex) { foreach (Exception subex in aex.InnerExceptions) { ErrorRecord subErrorRecord = new ErrorRecord(subex, "TemplateRenderFailure", ErrorCategory.ParserError, null); base.WriteError(subErrorRecord); } } catch (Exception ex) { ErrorRecord renderErrorRecord = new ErrorRecord(ex, "TemplateRenderFailure", ErrorCategory.ParserError, null); base.ThrowTerminatingError(renderErrorRecord); } }