|
18 | 18 | using System.Collections.ObjectModel;
|
19 | 19 | using System.Globalization;
|
20 | 20 | using System.Management.Automation;
|
21 |
| -using System.Management.Automation.Runspaces; |
22 | 21 | using System.Text;
|
23 | 22 |
|
24 | 23 | namespace Microsoft.Azure.Commands.Automation.Common
|
@@ -72,40 +71,31 @@ public static PSObject Deserialize(string json)
|
72 | 71 | /// <returns></returns>
|
73 | 72 | private static Collection<PSObject> InvokeScript(string scriptName, Hashtable parameters)
|
74 | 73 | {
|
75 |
| - using (Pipeline pipe = Runspace.DefaultRunspace.CreateNestedPipeline()) |
| 74 | + using (var powerShell = System.Management.Automation.PowerShell.Create(RunspaceMode.CurrentRunspace)) |
76 | 75 | {
|
77 |
| - Command scriptCommand = new Command(scriptName); |
78 |
| - |
| 76 | + powerShell.AddCommand(scriptName); |
79 | 77 | foreach (DictionaryEntry parameter in parameters)
|
80 | 78 | {
|
81 |
| - CommandParameter commandParm = new CommandParameter(parameter.Key.ToString(), parameter.Value); |
82 |
| - scriptCommand.Parameters.Add(commandParm); |
| 79 | + powerShell.AddParameter(parameter.Key.ToString(), parameter.Value); |
83 | 80 | }
|
84 |
| - pipe.Commands.Add(scriptCommand); |
85 | 81 |
|
86 |
| - var result = pipe.Invoke(); |
| 82 | + |
| 83 | + var result = powerShell.Invoke(); |
87 | 84 |
|
88 | 85 | //Error handling
|
89 |
| - if (pipe.Error.Count > 0) |
| 86 | + if (powerShell.HadErrors) |
90 | 87 | {
|
91 | 88 | StringBuilder errorStringBuilder = new StringBuilder();
|
92 |
| - while (!pipe.Error.EndOfPipeline) |
| 89 | + foreach (var error in powerShell.Streams.Error) |
93 | 90 | {
|
94 |
| - var value = pipe.Error.Read() as PSObject; |
95 |
| - if (value != null) |
96 |
| - { |
97 |
| - var r = value.BaseObject as ErrorRecord; |
98 |
| - if (r != null) |
99 |
| - { |
100 |
| - errorStringBuilder.AppendLine(r.InvocationInfo.MyCommand.Name + " : " + r.Exception.Message); |
101 |
| - errorStringBuilder.AppendLine(r.InvocationInfo.PositionMessage); |
102 |
| - } |
103 |
| - } |
| 91 | + errorStringBuilder.AppendLine(error.InvocationInfo.MyCommand.Name + " : " + error.Exception.Message); |
| 92 | + errorStringBuilder.AppendLine(error.InvocationInfo.PositionMessage); |
104 | 93 | }
|
105 | 94 |
|
106 | 95 | throw new AzureAutomationOperationException(string.Format(CultureInfo.CurrentCulture,
|
107 |
| - Resources.PowershellJsonDecrypterFailed, errorStringBuilder.ToString())); |
| 96 | + Resources.PowershellJsonDecrypterFailed, errorStringBuilder.ToString())); |
108 | 97 | }
|
| 98 | + |
109 | 99 | return result;
|
110 | 100 | }
|
111 | 101 | }
|
|
0 commit comments