Example #1
0
        /// <summary>
        /// Converts <paramref name="wildcardPattern"/> into a DOS wildcard.
        /// </summary>
        internal static string Parse(WildcardPattern wildcardPattern)
        {
            var parser = new WildcardPatternToDosWildcardParser();

            WildcardPatternParser.Parse(wildcardPattern, parser);
            return(parser._result.ToString());
        }
Example #2
0
        private static IEnumerable <CimModule> GetCimModules(CimSession cimSession, Uri resourceUri, string cimNamespace, string moduleNamePattern, bool onlyManifests, Cmdlet cmdlet, CancellationToken cancellationToken)
        {
            Func <CimModule, CimModule> selector        = null;
            WildcardPattern             wildcardPattern = new WildcardPattern(moduleNamePattern, WildcardOptions.CultureInvariant | WildcardOptions.IgnoreCase);
            string optionValue          = WildcardPatternToDosWildcardParser.Parse(wildcardPattern);
            CimOperationOptions options = new CimOperationOptions {
                CancellationToken = new CancellationToken?(cancellationToken)
            };

            options.SetCustomOption("PS_ModuleNamePattern", optionValue, false);
            if (resourceUri != null)
            {
                options.ResourceUri = resourceUri;
            }
            if (string.IsNullOrEmpty(cimNamespace) && (resourceUri == null))
            {
                cimNamespace = "root/Microsoft/Windows/Powershellv3";
            }
            IEnumerable <CimModule> source = from cimInstance in cimSession.EnumerateInstances(cimNamespace, "PS_Module", options)
                                             select new CimModule(cimInstance) into cimModule
                                             where wildcardPattern.IsMatch(cimModule.ModuleName)
                                             select cimModule;

            if (!onlyManifests)
            {
                if (selector == null)
                {
                    selector = delegate(CimModule cimModule) {
                        cimModule.FetchAllModuleFiles(cimSession, cimNamespace, options);
                        return(cimModule);
                    };
                }
                source = source.Select <CimModule, CimModule>(selector);
            }
            return(EnumerateWithCatch <CimModule>(source, delegate(Exception exception) {
                ErrorRecord errorRecord = GetErrorRecordForRemoteDiscoveryProvider(exception);
                if (!cmdlet.MyInvocation.ExpectingInput && (((-1 != errorRecord.FullyQualifiedErrorId.IndexOf("DiscoveryProviderNotFound", StringComparison.OrdinalIgnoreCase)) || cancellationToken.IsCancellationRequested) || ((exception is OperationCanceledException) || !cimSession.TestConnection())))
                {
                    cmdlet.ThrowTerminatingError(errorRecord);
                }
                cmdlet.WriteError(errorRecord);
            }));
        }
Example #3
0
        private static IEnumerable <CimModule> GetCimModules(
            CimSession cimSession,
            Uri resourceUri,
            string cimNamespace,
            string moduleNamePattern,
            bool onlyManifests,
            Cmdlet cmdlet,
            CancellationToken cancellationToken)
        {
            Dbg.Assert(cimSession != null, "Caller should verify cimSession != null");
            Dbg.Assert(moduleNamePattern != null, "Caller should verify that moduleNamePattern != null");

            const WildcardOptions wildcardOptions = WildcardOptions.IgnoreCase | WildcardOptions.CultureInvariant;
            var    wildcardPattern = WildcardPattern.Get(moduleNamePattern, wildcardOptions);
            string dosWildcard     = WildcardPatternToDosWildcardParser.Parse(wildcardPattern);

            var options = new CimOperationOptions {
                CancellationToken = cancellationToken
            };

            options.SetCustomOption("PS_ModuleNamePattern", dosWildcard, mustComply: false);
            if (resourceUri != null)
            {
                options.ResourceUri = resourceUri;
            }

            if (string.IsNullOrEmpty(cimNamespace) && (resourceUri == null))
            {
                cimNamespace = DiscoveryProviderNamespace;
            }

            // TODO/FIXME: ETW for method invocation
            IEnumerable <CimInstance> syncResults = cimSession.EnumerateInstances(
                cimNamespace,
                DiscoveryProviderModuleClass,
                options);
            // TODO/FIXME: ETW for method results
            IEnumerable <CimModule> cimModules = syncResults
                                                 .Select(cimInstance => new CimModule(cimInstance))
                                                 .Where(cimModule => wildcardPattern.IsMatch(cimModule.ModuleName));

            if (!onlyManifests)
            {
                cimModules = cimModules.Select(
                    delegate(CimModule cimModule)
                {
                    cimModule.FetchAllModuleFiles(cimSession, cimNamespace, options);
                    return(cimModule);
                });
            }

            return(EnumerateWithCatch(
                       cimModules,
                       delegate(Exception exception)
            {
                ErrorRecord errorRecord = GetErrorRecordForRemoteDiscoveryProvider(exception);
                if (!cmdlet.MyInvocation.ExpectingInput)
                {
                    if (((-1) != errorRecord.FullyQualifiedErrorId.IndexOf(DiscoveryProviderNotFoundErrorId, StringComparison.OrdinalIgnoreCase)) ||
                        (cancellationToken.IsCancellationRequested || (exception is OperationCanceledException)) ||
                        (!cimSession.TestConnection()))
                    {
                        cmdlet.ThrowTerminatingError(errorRecord);
                    }
                }
                cmdlet.WriteError(errorRecord);
            }));
        }
Example #4
0
 /// <summary>
 /// Converts <paramref name="wildcardPattern"/> into a DOS wildcard
 /// </summary>
 internal static string Parse(WildcardPattern wildcardPattern)
 {
     var parser = new WildcardPatternToDosWildcardParser();
     WildcardPatternParser.Parse(wildcardPattern, parser);
     return parser._result.ToString();
 }