protected PropertyValueFilter(string propertyName, object expectedPropertyValue, BehaviorOnNoMatch behaviorOnNoMatch) { PropertyName = propertyName; _behaviorOnNoMatch = behaviorOnNoMatch; OriginalExpectedPropertyValue = expectedPropertyValue; CimTypedExpectedPropertyValue = CimValueConverter.ConvertFromDotNetToCim(expectedPropertyValue); }
public bool IsMatch(CimInstance o) { if (o == null) { return(false); } CimProperty propertyInfo = o.CimInstanceProperties[PropertyName]; if (propertyInfo == null) { return(false); } object actualPropertyValue = propertyInfo.Value; if (CimTypedExpectedPropertyValue == null) { HadMatch = HadMatch || (actualPropertyValue == null); return(actualPropertyValue == null); } CimValueConverter.AssertIntrinsicCimValue(actualPropertyValue); CimValueConverter.AssertIntrinsicCimValue(CimTypedExpectedPropertyValue); actualPropertyValue = ConvertActualValueToExpectedType(actualPropertyValue, CimTypedExpectedPropertyValue); Dbg.Assert(IsSameType(actualPropertyValue, CimTypedExpectedPropertyValue), "Types of actual vs expected property value should always match"); bool isMatch = this.IsMatchingValue(actualPropertyValue); HadMatch = HadMatch || isMatch; return(isMatch); }
private static string ObjectToWqlLiteral(object o) { if (LanguagePrimitives.IsNull(o)) { return("null"); // based on an example at http://msdn.microsoft.com/library/aa394054(VS.85).aspx } o = CimValueConverter.ConvertFromDotNetToCim(o); PSObject pso = PSObject.AsPSObject(o); Type type = pso.BaseObject.GetType(); TypeCode typeCode = LanguagePrimitives.GetTypeCode(type); if (typeCode == TypeCode.String) { string s = o.ToString(); s = s.Replace("\\", "\\\\"); s = s.Replace("'", "\\'"); return("'" + s + "'"); } if (typeCode == TypeCode.Char) { return(ObjectToWqlLiteral(LanguagePrimitives.ConvertTo(o, typeof(string), CultureInfo.InvariantCulture))); } if (typeCode == TypeCode.DateTime) { var dateTime = (DateTime)LanguagePrimitives.ConvertTo(o, typeof(DateTime), CultureInfo.InvariantCulture); string result = ClrFacade.ToDmtfDateTime(dateTime); return("'" + result + "'"); } if (type == typeof(TimeSpan)) { // WMIv1 does not support using interval literals in a WQL query return(null); } if (LanguagePrimitives.IsNumeric(typeCode)) { return((string)LanguagePrimitives.ConvertTo(o, typeof(string), CultureInfo.InvariantCulture)); } if (LanguagePrimitives.IsBooleanType(type)) { if ((bool)LanguagePrimitives.ConvertTo(o, typeof(bool), CultureInfo.InvariantCulture)) { return("TRUE"); // based on http://msdn.microsoft.com/library/aa394054(VS.85).aspx } return("FALSE"); // based on http://msdn.microsoft.com/library/aa394054(VS.85).aspx } throw CimValueConverter.GetInvalidCastException( null, /* inner exception */ "InvalidCimQueryCast", o, CmdletizationResources.CimConversion_WqlQuery); }
private void ProcessOutParameter(CimMethodResult methodResult, MethodParameter methodParameter, IDictionary <string, MethodParameter> cmdletOutput) { object value; CimMethodParameter item = methodResult.OutParameters[methodParameter.Name]; if (item == null) { value = null; } else { value = item.Value; } object obj = value; object dotNet = CimValueConverter.ConvertFromCimToDotNet(obj, methodParameter.ParameterType); if (MethodParameterBindings.Out != (methodParameter.Bindings & MethodParameterBindings.Out)) { if (MethodParameterBindings.Error == (methodParameter.Bindings & MethodParameterBindings.Error)) { bool flag = (bool)LanguagePrimitives.ConvertTo(dotNet, typeof(bool), CultureInfo.InvariantCulture); if (flag) { string str = (string)LanguagePrimitives.ConvertTo(dotNet, typeof(string), CultureInfo.InvariantCulture); CimJobException cimJobException = CimJobException.CreateFromMethodErrorCode(base.GetDescription(), base.JobContext, base.MethodName, str); throw cimJobException; } } } else { methodParameter.Value = dotNet; cmdletOutput.Add(methodParameter.Name, methodParameter); CimInstance[] cimInstanceArray = dotNet as CimInstance[]; if (cimInstanceArray != null) { CimInstance[] cimInstanceArray1 = cimInstanceArray; for (int i = 0; i < (int)cimInstanceArray1.Length; i++) { CimInstance cimInstance = cimInstanceArray1[i]; CimCmdletAdapter.AssociateSessionOfOriginWithInstance(cimInstance, this.JobContext.Session); } } CimInstance cimInstance1 = dotNet as CimInstance; if (cimInstance1 != null) { CimCmdletAdapter.AssociateSessionOfOriginWithInstance(cimInstance1, this.JobContext.Session); return; } } }
internal static void SetCustomOption(CimOperationOptions operationOptions, string optionName, object optionValue) { if (optionValue != null) { object cim = CimValueConverter.ConvertFromDotNetToCim(optionValue); CimType cimType = CimConverter.GetCimType(CimValueConverter.GetCimType(optionValue.GetType())); operationOptions.SetCustomOption(optionName, cim, cimType, false); return; } else { return; } }
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); }
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(0 != (methodParameter.Bindings & (MethodParameterBindings.Out | MethodParameterBindings.Error)), "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 == null) ? null : outParameter.Value; object dotNetValue = CimValueConverter.ConvertFromCimToDotNet(valueReturnedFromMethod, methodParameter.ParameterType); if (MethodParameterBindings.Out == (methodParameter.Bindings & MethodParameterBindings.Out)) { methodParameter.Value = dotNetValue; cmdletOutput.Add(methodParameter.Name, methodParameter); var cimInstances = dotNetValue as CimInstance[]; if (cimInstances != null) { foreach (var instance in cimInstances) { CimCmdletAdapter.AssociateSessionOfOriginWithInstance(instance, this.JobContext.Session); } } var cimInstance = dotNetValue as CimInstance; if (cimInstance != null) { CimCmdletAdapter.AssociateSessionOfOriginWithInstance(cimInstance, this.JobContext.Session); } } else if (MethodParameterBindings.Error == (methodParameter.Bindings & 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; } } }
internal void ModifyLocalCimInstance(CimInstance cimInstance) { foreach (MethodParameter methodParameter in this.GetMethodInputParameters()) { CimValueConverter.AssertIntrinsicCimType(methodParameter.ParameterType); CimProperty propertyBeingModified = cimInstance.CimInstanceProperties[methodParameter.Name]; if (propertyBeingModified != null) { propertyBeingModified.Value = methodParameter.Value; } else { CimProperty propertyBeingAdded = CimProperty.Create( methodParameter.Name, methodParameter.Value, CimValueConverter.GetCimTypeEnum(methodParameter.ParameterType), CimFlags.None); cimInstance.CimInstanceProperties.Add(propertyBeingAdded); } } }
private IEnumerable <MethodParameter> GetMethodInputParametersCore(Func <MethodParameter, bool> filter) { IEnumerable <MethodParameter> enumerable = this._methodInvocationInfo.Parameters.Where <MethodParameter>(filter); List <MethodParameter> list = new List <MethodParameter>(); foreach (MethodParameter parameter in enumerable) { object obj2 = CimValueConverter.ConvertFromDotNetToCim(parameter.Value); Type cimType = CimValueConverter.GetCimType(parameter.ParameterType); MethodParameter item = new MethodParameter { Name = parameter.Name, ParameterType = cimType, Bindings = parameter.Bindings, Value = obj2, IsValuePresent = parameter.IsValuePresent }; list.Add(item); } return(list); }
private IEnumerable <MethodParameter> GetMethodInputParametersCore(Func <MethodParameter, bool> filter) { IEnumerable <MethodParameter> inputParameters = _methodInvocationInfo.Parameters.Where(filter); var result = new List <MethodParameter>(); foreach (MethodParameter inputParameter in inputParameters) { object cimValue = CimSensitiveValueConverter.ConvertFromDotNetToCim(inputParameter.Value); Type cimType = CimSensitiveValueConverter.GetCimType(inputParameter.ParameterType); CimValueConverter.AssertIntrinsicCimType(cimType); result.Add(new MethodParameter { Name = inputParameter.Name, ParameterType = cimType, Bindings = inputParameter.Bindings, Value = cimValue, IsValuePresent = inputParameter.IsValuePresent }); } return(result); }
internal CimMethodParametersCollection GetCimMethodParametersCollection() { CimMethodParametersCollection cimMethodParametersCollection = new CimMethodParametersCollection(); foreach (MethodParameter methodInputParameter in base.GetMethodInputParameters()) { CimMethodParameter cimMethodParameter = CimMethodParameter.Create(methodInputParameter.Name, methodInputParameter.Value, CimValueConverter.GetCimTypeEnum(methodInputParameter.ParameterType), (CimFlags)((long)0)); cimMethodParametersCollection.Add(cimMethodParameter); } return(cimMethodParametersCollection); }
private static string ObjectToWqlLiteral(object o) { if (!LanguagePrimitives.IsNull(o)) { o = CimValueConverter.ConvertFromDotNetToCim(o); PSObject pSObject = PSObject.AsPSObject(o); Type type = pSObject.BaseObject.GetType(); TypeCode typeCode = LanguagePrimitives.GetTypeCode(type); if (typeCode != TypeCode.String) { if (typeCode != TypeCode.Char) { if (typeCode != TypeCode.DateTime) { if (type != typeof(TimeSpan)) { if (!LanguagePrimitives.IsNumeric(typeCode)) { if (!LanguagePrimitives.IsBooleanType(type)) { throw CimValueConverter.GetInvalidCastException(null, "InvalidCimQueryCast", o, CmdletizationResources.CimConversion_WqlQuery); } else { if (!(bool)LanguagePrimitives.ConvertTo(o, typeof(bool), CultureInfo.InvariantCulture)) { return("FALSE"); } else { return("TRUE"); } } } else { return((string)LanguagePrimitives.ConvertTo(o, typeof(string), CultureInfo.InvariantCulture)); } } else { return(null); } } else { DateTime dateTime = (DateTime)LanguagePrimitives.ConvertTo(o, typeof(DateTime), CultureInfo.InvariantCulture); string dmtfDateTime = ManagementDateTimeConverter.ToDmtfDateTime(dateTime); return(string.Concat("'", dmtfDateTime, "'")); } } else { return(CimQuery.ObjectToWqlLiteral(LanguagePrimitives.ConvertTo(o, typeof(string), CultureInfo.InvariantCulture))); } } else { string str = o.ToString(); str = str.Replace("\\", "\\\\"); str = str.Replace("'", "\\'"); return(string.Concat("'", str, "'")); } } else { return("null"); } }