File size: 8,682 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Management.Automation;
using Microsoft.Management.Infrastructure;
using Microsoft.PowerShell.Cim;
using Dbg = System.Management.Automation.Diagnostics;
namespace Microsoft.PowerShell.Cmdletization.Cim
{
/// <summary>
/// Job wrapping invocation of an extrinsic CIM method.
/// </summary>
internal abstract class ExtrinsicMethodInvocationJob : MethodInvocationJobBase<CimMethodResultBase>
{
internal ExtrinsicMethodInvocationJob(CimJobContext jobContext, bool passThru, string methodSubject, MethodInvocationInfo methodInvocationInfo)
: base(jobContext, passThru, methodSubject, methodInvocationInfo)
{
}
#region Processing of "in" parameters
internal CimMethodParametersCollection GetCimMethodParametersCollection()
{
var methodParameters = new CimMethodParametersCollection();
foreach (MethodParameter parameter in this.GetMethodInputParameters())
{
CimValueConverter.AssertIntrinsicCimType(parameter.ParameterType);
var methodParameter = CimMethodParameter.Create(
parameter.Name,
parameter.Value,
CimValueConverter.GetCimTypeEnum(parameter.ParameterType),
CimFlags.None);
methodParameters.Add(methodParameter);
}
return methodParameters;
}
#endregion
#region Processing of "out" parameters
private void ProcessOutParameter(CimMethodResult methodResult, MethodParameter methodParameter, IDictionary<string, MethodParameter> cmdletOutput)
{
Dbg.Assert(methodResult != null, "Caller should verify methodResult != null");
Dbg.Assert(methodParameter != null, "Caller should verify methodParameter != null");
Dbg.Assert((methodParameter.Bindings & (MethodParameterBindings.Out | MethodParameterBindings.Error)) != 0, "Caller should verify that this is an out parameter");
Dbg.Assert(cmdletOutput != null, "Caller should verify cmdletOutput != null");
Dbg.Assert(this.MethodSubject != null, "MethodSubject property should be initialized before starting main job processing");
CimMethodParameter outParameter = methodResult.OutParameters[methodParameter.Name];
object valueReturnedFromMethod = outParameter?.Value;
object dotNetValue = CimValueConverter.ConvertFromCimToDotNet(valueReturnedFromMethod, methodParameter.ParameterType);
if ((methodParameter.Bindings & MethodParameterBindings.Out) == MethodParameterBindings.Out)
{
methodParameter.Value = dotNetValue;
cmdletOutput.Add(methodParameter.Name, methodParameter);
if (dotNetValue is CimInstance[] cimInstances)
{
foreach (var instance in cimInstances)
{
CimCmdletAdapter.AssociateSessionOfOriginWithInstance(instance, this.JobContext.Session);
}
}
if (dotNetValue is CimInstance cimInstance)
{
CimCmdletAdapter.AssociateSessionOfOriginWithInstance(cimInstance, this.JobContext.Session);
}
}
else if ((methodParameter.Bindings & MethodParameterBindings.Error) == MethodParameterBindings.Error)
{
var gotError = (bool)LanguagePrimitives.ConvertTo(dotNetValue, typeof(bool), CultureInfo.InvariantCulture);
if (gotError)
{
var errorCodeAsString = (string)LanguagePrimitives.ConvertTo(dotNetValue, typeof(string), CultureInfo.InvariantCulture);
CimJobException cje = CimJobException.CreateFromMethodErrorCode(this.GetDescription(), this.JobContext, this.MethodName, errorCodeAsString);
throw cje;
}
}
}
private void OnNext(CimMethodResult methodResult)
{
Dbg.Assert(this.MethodSubject != null, "MethodSubject property should be initialized before starting main job processing");
var cmdletOutput = new Dictionary<string, MethodParameter>(StringComparer.OrdinalIgnoreCase);
foreach (MethodParameter methodParameter in this.GetMethodOutputParameters())
{
ProcessOutParameter(methodResult, methodParameter, cmdletOutput);
}
if (cmdletOutput.Count == 1)
{
var singleOutputParameter = cmdletOutput.Values.First();
if (singleOutputParameter.Value == null)
{
return;
}
IEnumerable enumerable = LanguagePrimitives.GetEnumerable(singleOutputParameter.Value);
if (enumerable != null)
{
foreach (object o in enumerable)
{
this.WriteObject(o, singleOutputParameter);
}
}
else
{
this.WriteObject(singleOutputParameter.Value, singleOutputParameter);
}
}
else if (cmdletOutput.Count > 1)
{
var propertyBag = new PSObject();
foreach (var element in cmdletOutput)
{
var tmp = new PSNoteProperty(element.Key, element.Value.Value);
propertyBag.Properties.Add(tmp);
}
this.WriteObject(propertyBag);
}
}
private void OnNext(CimMethodStreamedResult streamedResult)
{
MethodParameter methodParameter = this.GetMethodOutputParameters()
.SingleOrDefault(p => p.Name.Equals(streamedResult.ParameterName, StringComparison.OrdinalIgnoreCase));
if (methodParameter == null)
{
string errorMessage = string.Format(
CultureInfo.InvariantCulture,
CmdletizationResources.CimJob_InvalidOutputParameterName,
this.MethodSubject,
this.MethodName,
streamedResult.ParameterName);
throw CimJobException.CreateWithFullControl(
this.JobContext,
errorMessage,
"CimJob_InvalidOutputParameterName",
ErrorCategory.MetadataError);
}
var array = LanguagePrimitives.GetEnumerable(streamedResult.ItemValue);
if (array != null)
{
foreach (var element in array)
{
this.WriteObject(element, methodParameter);
}
}
else
{
this.WriteObject(streamedResult.ItemValue, methodParameter);
}
}
private void WriteObject(object cmdletOutput, MethodParameter methodParameter)
{
Dbg.Assert(methodParameter != null, "Caller should verify that methodParameter != null");
if ((cmdletOutput != null) && (!string.IsNullOrEmpty(methodParameter.ParameterTypeName)))
{
PSObject pso = PSObject.AsPSObject(cmdletOutput);
if (!pso.TypeNames.Contains(methodParameter.ParameterTypeName, StringComparer.OrdinalIgnoreCase))
{
pso.TypeNames.Insert(0, methodParameter.ParameterTypeName);
}
}
this.WriteObject(cmdletOutput);
}
public override void OnNext(CimMethodResultBase item)
{
this.ExceptionSafeWrapper(
delegate
{
if (item is CimMethodResult methodResult)
{
this.OnNext(methodResult);
return;
}
if (item is CimMethodStreamedResult streamedResult)
{
this.OnNext(streamedResult);
return;
}
Dbg.Assert(false, "CimMethodResultBase has to be either a CimMethodResult or CimMethodStreamedResult");
});
}
#endregion
}
}
|