Skip to content

Commit dadfd0f

Browse files
authored
[Automation]Fix the issue of processing PSCustomObject (#14045)
* Fix the issue of processing PSCustomObject. * Add changelog * Update ChangeLog.md Co-authored-by: wyunchi-ms <[email protected]>
1 parent 6727874 commit dadfd0f

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/Automation/Automation/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Fixed the issue of processing `PSCustomObject` and `Array`.
2122

2223
## Version 1.4.2
2324
* Fixed issue where description was not populated for update management schedules

src/Automation/Automation/Common/PowershellJsonConverter.cs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
using System.Management.Automation;
2121
using System.Text;
2222
using Newtonsoft.Json;
23+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
24+
using System.Linq;
25+
using System.Collections.Generic;
2326

2427
namespace Microsoft.Azure.Commands.Automation.Common
2528
{
@@ -31,20 +34,41 @@ public static string Serialize(object inputObject)
3134
{
3235
return null;
3336
}
34-
while(inputObject is PSObject && ((PSObject)inputObject).BaseObject != null)
37+
if (inputObject is string @str)
3538
{
36-
inputObject = ((PSObject)inputObject).BaseObject;
39+
return str.Trim();
3740
}
38-
if(inputObject is string)
41+
else if (inputObject is object[] @objectArray)
3942
{
40-
inputObject = ((string)inputObject).Trim();
43+
return SerializeArray(objectArray);
44+
}
45+
else if (inputObject is PSObject @psObject)
46+
{
47+
return SerializePsObject(psObject);
4148
}
4249
return JsonConvert.SerializeObject(inputObject);
4350
}
4451

52+
private static string SerializePsObject(PSObject @psObject)
53+
{
54+
Dictionary<string, string> hashTable = new Dictionary<string, string>();
55+
foreach (var item in @psObject.Properties)
56+
{
57+
hashTable.Add(item.Name, Serialize(item.Value));
58+
}
59+
60+
return JsonConvert.SerializeObject(hashTable);
61+
}
62+
63+
private static string SerializeArray(object[] objectArray)
64+
{
65+
List<object> objectList = objectArray.ToList();
66+
return string.Format("[{0}]", string.Join(",", objectList.Select(Serialize).ToList()));
67+
}
68+
4569
public static PSObject Deserialize(string json)
4670
{
47-
if (String.IsNullOrEmpty(json))
71+
if (string.IsNullOrEmpty(json))
4872
{
4973
return null;
5074
}

0 commit comments

Comments
 (0)