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); }
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); }