/// <summary> /// Import CIM classes from the given file /// </summary> /// <param name="path"></param> /// <param name="moduleInfo"></param> /// <param name="errors"></param> /// <returns></returns> public static List<CimClass> ImportClasses(string path, Tuple<string, Version> moduleInfo, Collection<Exception> errors) { if (string.IsNullOrEmpty(path)) { throw PSTraceSource.NewArgumentNullException("path"); } s_tracer.WriteLine("DSC ClassCache: importing file: {0}", path); var parser = new Microsoft.PowerShell.DesiredStateConfiguration.CimDSCParser(MyClassCallback); List<CimClass> classes = null; try { classes = parser.ParseSchemaMof(path); } catch (PSInvalidOperationException e) { // Ignore modules with invalid schemas. s_tracer.WriteLine("DSC ClassCache: Error importing file '{0}', with error '{1}'. Skipping file.", path, e); if (errors != null) { errors.Add(e); } } if (classes != null) { foreach (var c in classes) { // Only add the class once... var className = c.CimSystemProperties.ClassName; string moduleQualifiedResourceName = GetModuleQualifiedResourceName(moduleInfo.Item1, moduleInfo.Item2.ToString(), className); Tuple<DSCResourceRunAsCredential, Microsoft.Management.Infrastructure.CimClass> cimClassInfo; if (ClassCache.TryGetValue(moduleQualifiedResourceName, out cimClassInfo)) { CimClass cimClass = cimClassInfo.Item2; // If this is a nested object and we already have exactly same nested object, we will // allow sharing of nested objects. if (!IsSameNestedObject(cimClass, c)) { var files = string.Join(",", GetFileDefiningClass(className)); PSInvalidOperationException e = PSTraceSource.NewInvalidOperationException( ParserStrings.DuplicateCimClassDefinition, className, path, files); e.SetErrorId("DuplicateCimClassDefinition"); if (errors != null) { errors.Add(e); } } } if (s_hiddenResourceCache.Contains(className)) { continue; } if (!CacheResourcesFromMultipleModuleVersions) { // Find & remove the previous version of the resource. List<KeyValuePair<string, Tuple<DSCResourceRunAsCredential, Microsoft.Management.Infrastructure.CimClass>>> resourceList = FindResourceInCache(moduleInfo.Item1, className); if (resourceList.Count > 0 && !string.IsNullOrEmpty(resourceList[0].Key)) { ClassCache.Remove(resourceList[0].Key); } } ClassCache[moduleQualifiedResourceName] = new Tuple<DSCResourceRunAsCredential, Microsoft.Management.Infrastructure.CimClass>(DSCResourceRunAsCredential.Default, c); ByClassModuleCache[className] = moduleInfo; } var sb = new System.Text.StringBuilder(); foreach (var c in classes) { sb.Append(c.CimSystemProperties.ClassName); sb.Append(","); } s_tracer.WriteLine("DSC ClassCache: loading file '{0}' added the following classes to the cache: {1}", path, sb.ToString()); } else { s_tracer.WriteLine("DSC ClassCache: loading file '{0}' added no classes to the cache."); } ByFileClassCache[path] = classes; return classes; }
private static bool ImportKeywordsFromAssembly(PSModuleInfo module, ICollection<string> resourcesToImport, ICollection<string> resourcesFound, Dictionary<string, ScriptBlock> functionsToDefine, Assembly assembly) { bool result = false; var parser = new Microsoft.PowerShell.DesiredStateConfiguration.CimDSCParser(MyClassCallback); IEnumerable<Type> resourceDefinitions = assembly.GetTypes().Where(t => t.GetTypeInfo().GetCustomAttributes<DscResourceAttribute>().Any()); foreach (var r in resourceDefinitions) { result = true; bool skip = true; foreach (var toImport in resourcesToImport) { if ((WildcardPattern.Get(toImport, WildcardOptions.IgnoreCase)).IsMatch(r.Name)) { skip = false; break; } } if (skip) continue; var mof = GenerateMofForType(r); ProcessMofForDynamicKeywords(module, resourcesFound, functionsToDefine, parser, mof, DSCResourceRunAsCredential.Default); } return result; }
/// <summary> /// Routine used to load a set of CIM instances from a .mof file using the /// current set of cached classes for schema validation. /// </summary> /// <param name="path"></param> /// <param name="schemaValidationOption"></param> /// <returns></returns> public static List<CimInstance> ImportInstances(string path, int schemaValidationOption) { if (string.IsNullOrEmpty(path)) { throw PSTraceSource.NewArgumentNullException("path"); throw new ArgumentNullException("path"); } if (schemaValidationOption < (int)Microsoft.Management.Infrastructure.Serialization.MofDeserializerSchemaValidationOption.Default || schemaValidationOption > (int)Microsoft.Management.Infrastructure.Serialization.MofDeserializerSchemaValidationOption.Ignore) { throw new IndexOutOfRangeException("schemaValidationOption"); } var parser = new Microsoft.PowerShell.DesiredStateConfiguration.CimDSCParser(MyClassCallback, (Microsoft.Management.Infrastructure.Serialization.MofDeserializerSchemaValidationOption)schemaValidationOption); return parser.ParseInstanceMof(path); }
/// <summary> /// A routine that validates a string containing MOF instances against the /// current set of cached classes. /// </summary> /// <param name="instanceText"></param> public static void ValidateInstanceText(string instanceText) { if (string.IsNullOrEmpty(instanceText)) { throw PSTraceSource.NewArgumentNullException("instanceText"); } var parser = new Microsoft.PowerShell.DesiredStateConfiguration.CimDSCParser(MyClassCallback); parser.ValidateInstanceText(instanceText); }
/// <summary> /// Routine used to load a set of CIM instances from a .mof file using the /// current set of cached classes for schema validation. /// </summary> /// <param name="path">The file to load the classes from.</param> /// <returns></returns> public static List<CimInstance> ImportInstances(string path) { if (string.IsNullOrEmpty(path)) { throw PSTraceSource.NewArgumentNullException("path"); } var parser = new Microsoft.PowerShell.DesiredStateConfiguration.CimDSCParser(MyClassCallback); return parser.ParseInstanceMof(path); }