-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from kiranisaac/master
Powershell JSON Converter
- Loading branch information
Showing
5 changed files
with
101 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/ServiceManagement/Automation/Commands.Automation/Common/PowershellJsonConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 10 additions & 1 deletion
11
src/ServiceManagement/Automation/Commands.Automation/Properties/Resources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters