diff --git a/src/Automation/Automation/ChangeLog.md b/src/Automation/Automation/ChangeLog.md index 963cde70185d..de9ec2893080 100644 --- a/src/Automation/Automation/ChangeLog.md +++ b/src/Automation/Automation/ChangeLog.md @@ -18,6 +18,7 @@ - Additional information about change #1 --> ## Upcoming Release +* Fixed the issue of processing `PSCustomObject` and `Array`. ## Version 1.4.2 * Fixed issue where description was not populated for update management schedules diff --git a/src/Automation/Automation/Common/PowershellJsonConverter.cs b/src/Automation/Automation/Common/PowershellJsonConverter.cs index bca1cd5bbc1a..1504b514c58d 100644 --- a/src/Automation/Automation/Common/PowershellJsonConverter.cs +++ b/src/Automation/Automation/Common/PowershellJsonConverter.cs @@ -20,6 +20,9 @@ using System.Management.Automation; using System.Text; using Newtonsoft.Json; +using Microsoft.WindowsAzure.Commands.Utilities.Common; +using System.Linq; +using System.Collections.Generic; namespace Microsoft.Azure.Commands.Automation.Common { @@ -31,20 +34,41 @@ public static string Serialize(object inputObject) { return null; } - while(inputObject is PSObject && ((PSObject)inputObject).BaseObject != null) + if (inputObject is string @str) { - inputObject = ((PSObject)inputObject).BaseObject; + return str.Trim(); } - if(inputObject is string) + else if (inputObject is object[] @objectArray) { - inputObject = ((string)inputObject).Trim(); + return SerializeArray(objectArray); + } + else if (inputObject is PSObject @psObject) + { + return SerializePsObject(psObject); } return JsonConvert.SerializeObject(inputObject); } + private static string SerializePsObject(PSObject @psObject) + { + Dictionary hashTable = new Dictionary(); + foreach (var item in @psObject.Properties) + { + hashTable.Add(item.Name, Serialize(item.Value)); + } + + return JsonConvert.SerializeObject(hashTable); + } + + private static string SerializeArray(object[] objectArray) + { + List objectList = objectArray.ToList(); + return string.Format("[{0}]", string.Join(",", objectList.Select(Serialize).ToList())); + } + public static PSObject Deserialize(string json) { - if (String.IsNullOrEmpty(json)) + if (string.IsNullOrEmpty(json)) { return null; }