diff --git a/src/ServiceManagement/Automation/Commands.Automation/Commands.Automation.csproj b/src/ServiceManagement/Automation/Commands.Automation/Commands.Automation.csproj index 6392bda88b1f..b43c069a262c 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Commands.Automation.csproj +++ b/src/ServiceManagement/Automation/Commands.Automation/Commands.Automation.csproj @@ -168,6 +168,7 @@ + diff --git a/src/ServiceManagement/Automation/Commands.Automation/Common/PowershellJsonConverter.cs b/src/ServiceManagement/Automation/Commands.Automation/Common/PowershellJsonConverter.cs new file mode 100644 index 000000000000..499de7915f3d --- /dev/null +++ b/src/ServiceManagement/Automation/Commands.Automation/Common/PowershellJsonConverter.cs @@ -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]; + } + + /// + /// Invokes a powershell script using the same runspace as the caller. + /// + /// script name + /// parameters for the script + /// + private static Collection 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; + } + } + } +} diff --git a/src/ServiceManagement/Automation/Commands.Automation/Model/Variable.cs b/src/ServiceManagement/Automation/Commands.Automation/Model/Variable.cs index a2786266b526..a2b4d4a660e7 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Model/Variable.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Model/Variable.cs @@ -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; /// /// The Variable. @@ -48,7 +46,7 @@ public Variable(AutomationManagement.Models.Variable variable, string automation } else { - this.Value = JsonConvert.DeserializeObject(variable.Properties.Value); + this.Value = PowershellJsonConverter.Deserialize(variable.Properties.Value); } this.Description = variable.Properties.Description; diff --git a/src/ServiceManagement/Automation/Commands.Automation/Properties/Resources.Designer.cs b/src/ServiceManagement/Automation/Commands.Automation/Properties/Resources.Designer.cs index 0a78503ba834..0e579118b401 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Properties/Resources.Designer.cs +++ b/src/ServiceManagement/Automation/Commands.Automation/Properties/Resources.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.34014 +// Runtime Version:4.0.30319.0 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -195,6 +195,15 @@ internal static string ParameterEmpty { } } + /// + /// Looks up a localized string similar to Failed to decrypt. Error Details {0}. + /// + internal static string PowershellJsonDecrypterFailed { + get { + return ResourceManager.GetString("PowershellJsonDecrypterFailed", resourceCulture); + } + } + /// /// Looks up a localized string similar to Disassociating the Azure Automation runbook and schedule.. /// diff --git a/src/ServiceManagement/Automation/Commands.Automation/Properties/Resources.resx b/src/ServiceManagement/Automation/Commands.Automation/Properties/Resources.resx index 42a2f54b08fc..9bae70791b4e 100644 --- a/src/ServiceManagement/Automation/Commands.Automation/Properties/Resources.resx +++ b/src/ServiceManagement/Automation/Commands.Automation/Properties/Resources.resx @@ -242,6 +242,10 @@ The certificate already exists. Certificate name: {0}. Automation + + Failed to decrypt. Error Details {0} + Automation + Resource does not exists. Automation