Beispiel #1
0
        private static void ProcessMofForDynamicKeywords(PSModuleInfo module, ICollection<string> resourcesFound,
            Dictionary<string, ScriptBlock> functionsToDefine, CimDSCParser parser, string mof, DSCResourceRunAsCredential runAsBehavior)
        {
            foreach (var c in parser.ParseSchemaMofFileBuffer(mof))
            {
                var className = c.CimSystemProperties.ClassName;
                if (!CacheResourcesFromMultipleModuleVersions)
                {
                    // Find & remove the previous version of the resource.
                    List<KeyValuePair<string, Tuple<DSCResourceRunAsCredential, Microsoft.Management.Infrastructure.CimClass>>> resourceList =
                        FindResourceInCache(module.Name, className);

                    if (resourceList.Count > 0 && !string.IsNullOrEmpty(resourceList[0].Key))
                    {
                        ClassCache.Remove(resourceList[0].Key);
                    }
                }
                var moduleQualifiedResourceName = GetModuleQualifiedResourceName(module.Name, module.Version.ToString(), className);
                ClassCache[moduleQualifiedResourceName] = new Tuple<DSCResourceRunAsCredential, Microsoft.Management.Infrastructure.CimClass>(runAsBehavior, c);
                ByClassModuleCache[className] = new Tuple<string, Version>(module.Name, module.Version);
                resourcesFound.Add(className);
                CreateAndRegisterKeywordFromCimClass(module.Name, module.Version, c, functionsToDefine, runAsBehavior);
            }
        }
Beispiel #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="module"></param>
        /// <param name="resourcesToImport"></param>
        /// <param name="resourcesFound"></param>
        /// <param name="functionsToDefine"></param>
        /// <param name="errorList"></param>
        /// <param name="extent"></param>
        /// <returns></returns>
        private static bool ImportKeywordsFromScriptFile(string fileName, PSModuleInfo module, ICollection<string> resourcesToImport, ICollection<string> resourcesFound, Dictionary<string, ScriptBlock> functionsToDefine, List<ParseError> errorList, IScriptExtent extent)
        {
            IEnumerable<Ast> resourceDefinitions;
            if (!GetResourceDefinitionsFromModule(fileName, out resourceDefinitions, errorList, extent))
            {
                return false;
            }

            var result = false;
            var parser = new CimDSCParser(MyClassCallback);

            const WildcardOptions wildcardOptions = WildcardOptions.IgnoreCase | WildcardOptions.CultureInvariant;
            IEnumerable<WildcardPattern> patternList = SessionStateUtilities.CreateWildcardsFromStrings(module._declaredDscResourceExports, wildcardOptions);

            foreach (var r in resourceDefinitions)
            {
                result = true;
                var resourceDefnAst = (TypeDefinitionAst)r;

                if (!SessionStateUtilities.MatchesAnyWildcardPattern(resourceDefnAst.Name, patternList, true))
                {
                    continue;
                }

                bool skip = true;
                foreach (var toImport in resourcesToImport)
                {
                    if ((WildcardPattern.Get(toImport, WildcardOptions.IgnoreCase)).IsMatch(resourceDefnAst.Name))
                    {
                        skip = false;
                        break;
                    }
                }

                if (skip) continue;

                // Parse the Resource Attribute to see if RunAs behavior is specified for the resource. 
                DSCResourceRunAsCredential runAsBehavior = DSCResourceRunAsCredential.Default;
                foreach (var attr in resourceDefnAst.Attributes)
                {
                    if (attr.TypeName.GetReflectionAttributeType() == typeof(DscResourceAttribute))
                    {
                        foreach (var na in attr.NamedArguments)
                        {
                            if (na.ArgumentName.Equals("RunAsCredential", StringComparison.OrdinalIgnoreCase))
                            {
                                var dscResourceAttribute = attr.GetAttribute() as DscResourceAttribute;
                                if (dscResourceAttribute != null)
                                {
                                    runAsBehavior = dscResourceAttribute.RunAsCredential;
                                }
                            }
                        }
                    }
                }

                var mof = GenerateMofForAst(resourceDefnAst);

                ProcessMofForDynamicKeywords(module, resourcesFound, functionsToDefine, parser, mof, runAsBehavior);
            }

            return result;
        }