Skip to content

Commit

Permalink
fix deserialization of array variables
Browse files Browse the repository at this point in the history
  • Loading branch information
kiranisaac committed Jan 27, 2015
1 parent 2d45d32 commit a496c36
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,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 Decrypt(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.Decrypt(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,4 +242,7 @@
<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>
</data>
</root>

0 comments on commit a496c36

Please sign in to comment.