Skip to content

Commit

Permalink
Merge pull request #7 from kiranisaac/master
Browse files Browse the repository at this point in the history
Powershell JSON Converter
  • Loading branch information
elvg committed Jan 29, 2015
2 parents 2cd218d + bc4966d commit b026b1b
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
<Compile Include="Common\AzureAutomationOperationException.cs" />
<Compile Include="Common\Constants.cs" />
<Compile Include="Common\IAutomationClient.cs" />
<Compile Include="Common\PowershellJsonConverter.cs" />
<Compile Include="Common\Requires.cs" />
<Compile Include="Common\RequiresExtensions.cs" />
<Compile Include="Common\ResourceCommonException.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using Microsoft.Azure.Commands.Automation.Properties;
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Text;

namespace Microsoft.Azure.Commands.Automation.Common
{
public static class PowershellJsonConverter
{
private const string PsCommandConvertToJson = "ConvertTo-Json";
private const string PsCommandConvertFromJson = "ConvertFrom-Json";
private const string PsCommandParamInputObject = "InputObject";
private const string PsCommandParamDepth = "Depth";

public static PSObject Deserialize(string json)
{
if (String.IsNullOrEmpty(json))
{
return null;
}

Hashtable parameters = new Hashtable();
parameters.Add(PsCommandParamInputObject, json);
var result = PowershellJsonConverter.InvokeScript(PsCommandConvertFromJson, parameters);
if (result.Count != 1)
{
return null;
}

//count == 1. return the first psobject
return result[0];
}

/// <summary>
/// Invokes a powershell script using the same runspace as the caller.
/// </summary>
/// <param name="scriptName">script name</param>
/// <param name="parameters">parameters for the script</param>
/// <returns></returns>
private static Collection<PSObject> InvokeScript(string scriptName, Hashtable parameters)
{
using (Pipeline pipe = Runspace.DefaultRunspace.CreateNestedPipeline())
{
Command scriptCommand = new Command(scriptName);

foreach (DictionaryEntry parameter in parameters)
{
CommandParameter commandParm = new CommandParameter(parameter.Key.ToString(), parameter.Value);
scriptCommand.Parameters.Add(commandParm);
}
pipe.Commands.Add(scriptCommand);

var result = pipe.Invoke();

//Error handling
if (pipe.Error.Count > 0)
{
StringBuilder errorStringBuilder = new StringBuilder();
while (!pipe.Error.EndOfPipeline)
{
var value = pipe.Error.Read() as PSObject;
if (value != null)
{
var r = value.BaseObject as ErrorRecord;
if (r != null)
{
errorStringBuilder.AppendLine(r.InvocationInfo.MyCommand.Name + " : " + r.Exception.Message);
errorStringBuilder.AppendLine(r.InvocationInfo.PositionMessage);
}
}
}

throw new AzureAutomationOperationException(string.Format(CultureInfo.CurrentCulture,
Resources.PowershellJsonDecrypterFailed, errorStringBuilder.ToString()));
}
return result;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using Microsoft.Azure.Commands.Automation.Common;
using System;

namespace Microsoft.Azure.Commands.Automation.Model
{
using AutomationManagement = Management.Automation;
using Newtonsoft.Json;
using System.Management.Automation;

/// <summary>
/// The Variable.
Expand Down Expand Up @@ -48,7 +46,7 @@ public Variable(AutomationManagement.Models.Variable variable, string automation
}
else
{
this.Value = JsonConvert.DeserializeObject<object>(variable.Properties.Value);
this.Value = PowershellJsonConverter.Deserialize(variable.Properties.Value);
}

this.Description = variable.Properties.Description;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@
<value>The certificate already exists. Certificate name: {0}.</value>
<comment>Automation</comment>
</data>
<data name="PowershellJsonDecrypterFailed" xml:space="preserve">
<value>Failed to decrypt. Error Details {0}</value>
<comment>Automation</comment>
</data>
<data name="ResourceNotFound" xml:space="preserve">
<value>Resource does not exists.</value>
<comment>Automation</comment>
Expand Down

0 comments on commit b026b1b

Please sign in to comment.