Skip to content

Commit b026b1b

Browse files
committed
Merge pull request #7 from kiranisaac/master
Powershell JSON Converter
2 parents 2cd218d + bc4966d commit b026b1b

File tree

5 files changed

+101
-5
lines changed

5 files changed

+101
-5
lines changed

src/ServiceManagement/Automation/Commands.Automation/Commands.Automation.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
<Compile Include="Common\AzureAutomationOperationException.cs" />
169169
<Compile Include="Common\Constants.cs" />
170170
<Compile Include="Common\IAutomationClient.cs" />
171+
<Compile Include="Common\PowershellJsonConverter.cs" />
171172
<Compile Include="Common\Requires.cs" />
172173
<Compile Include="Common\RequiresExtensions.cs" />
173174
<Compile Include="Common\ResourceCommonException.cs" />
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using Microsoft.Azure.Commands.Automation.Properties;
2+
using System;
3+
using System.Collections;
4+
using System.Collections.ObjectModel;
5+
using System.Globalization;
6+
using System.Management.Automation;
7+
using System.Management.Automation.Runspaces;
8+
using System.Text;
9+
10+
namespace Microsoft.Azure.Commands.Automation.Common
11+
{
12+
public static class PowershellJsonConverter
13+
{
14+
private const string PsCommandConvertToJson = "ConvertTo-Json";
15+
private const string PsCommandConvertFromJson = "ConvertFrom-Json";
16+
private const string PsCommandParamInputObject = "InputObject";
17+
private const string PsCommandParamDepth = "Depth";
18+
19+
public static PSObject Deserialize(string json)
20+
{
21+
if (String.IsNullOrEmpty(json))
22+
{
23+
return null;
24+
}
25+
26+
Hashtable parameters = new Hashtable();
27+
parameters.Add(PsCommandParamInputObject, json);
28+
var result = PowershellJsonConverter.InvokeScript(PsCommandConvertFromJson, parameters);
29+
if (result.Count != 1)
30+
{
31+
return null;
32+
}
33+
34+
//count == 1. return the first psobject
35+
return result[0];
36+
}
37+
38+
/// <summary>
39+
/// Invokes a powershell script using the same runspace as the caller.
40+
/// </summary>
41+
/// <param name="scriptName">script name</param>
42+
/// <param name="parameters">parameters for the script</param>
43+
/// <returns></returns>
44+
private static Collection<PSObject> InvokeScript(string scriptName, Hashtable parameters)
45+
{
46+
using (Pipeline pipe = Runspace.DefaultRunspace.CreateNestedPipeline())
47+
{
48+
Command scriptCommand = new Command(scriptName);
49+
50+
foreach (DictionaryEntry parameter in parameters)
51+
{
52+
CommandParameter commandParm = new CommandParameter(parameter.Key.ToString(), parameter.Value);
53+
scriptCommand.Parameters.Add(commandParm);
54+
}
55+
pipe.Commands.Add(scriptCommand);
56+
57+
var result = pipe.Invoke();
58+
59+
//Error handling
60+
if (pipe.Error.Count > 0)
61+
{
62+
StringBuilder errorStringBuilder = new StringBuilder();
63+
while (!pipe.Error.EndOfPipeline)
64+
{
65+
var value = pipe.Error.Read() as PSObject;
66+
if (value != null)
67+
{
68+
var r = value.BaseObject as ErrorRecord;
69+
if (r != null)
70+
{
71+
errorStringBuilder.AppendLine(r.InvocationInfo.MyCommand.Name + " : " + r.Exception.Message);
72+
errorStringBuilder.AppendLine(r.InvocationInfo.PositionMessage);
73+
}
74+
}
75+
}
76+
77+
throw new AzureAutomationOperationException(string.Format(CultureInfo.CurrentCulture,
78+
Resources.PowershellJsonDecrypterFailed, errorStringBuilder.ToString()));
79+
}
80+
return result;
81+
}
82+
}
83+
}
84+
}

src/ServiceManagement/Automation/Commands.Automation/Model/Variable.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using System;
1615
using Microsoft.Azure.Commands.Automation.Common;
16+
using System;
1717

1818
namespace Microsoft.Azure.Commands.Automation.Model
1919
{
2020
using AutomationManagement = Management.Automation;
21-
using Newtonsoft.Json;
22-
using System.Management.Automation;
2321

2422
/// <summary>
2523
/// The Variable.
@@ -48,7 +46,7 @@ public Variable(AutomationManagement.Models.Variable variable, string automation
4846
}
4947
else
5048
{
51-
this.Value = JsonConvert.DeserializeObject<object>(variable.Properties.Value);
49+
this.Value = PowershellJsonConverter.Deserialize(variable.Properties.Value);
5250
}
5351

5452
this.Description = variable.Properties.Description;

src/ServiceManagement/Automation/Commands.Automation/Properties/Resources.Designer.cs

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ServiceManagement/Automation/Commands.Automation/Properties/Resources.resx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,10 @@
242242
<value>The certificate already exists. Certificate name: {0}.</value>
243243
<comment>Automation</comment>
244244
</data>
245+
<data name="PowershellJsonDecrypterFailed" xml:space="preserve">
246+
<value>Failed to decrypt. Error Details {0}</value>
247+
<comment>Automation</comment>
248+
</data>
245249
<data name="ResourceNotFound" xml:space="preserve">
246250
<value>Resource does not exists.</value>
247251
<comment>Automation</comment>

0 commit comments

Comments
 (0)