internal Exception GetExceptionIfBrokenSession(
            CimSession potentiallyBrokenSession,
            bool skipTestConnection,
            out bool sessionWasAlreadyTerminated)
        {
            if (IsSessionTerminated(potentiallyBrokenSession))
            {
                sessionWasAlreadyTerminated = true;
                return(null);
            }

            Exception sessionException = null;

            if (!skipTestConnection &&
                !this.DidSessionAlreadyPassedConnectivityTest(potentiallyBrokenSession))
            {
                try
                {
                    CimInstance  throwAwayCimInstance;
                    CimException cimException;
                    potentiallyBrokenSession.TestConnection(out throwAwayCimInstance, out cimException);
                    sessionException = cimException;
                    if (sessionException == null)
                    {
                        this.MarkSessionAsConnected(potentiallyBrokenSession);
                    }
                }
                catch (InvalidOperationException invalidOperationException)
                {
                    sessionException = invalidOperationException;
                }
            }

            if (sessionException != null)
            {
                MarkSessionAsTerminated(potentiallyBrokenSession, out sessionWasAlreadyTerminated);
                return(sessionException);
            }
            else
            {
                sessionWasAlreadyTerminated = false;
                return(null);
            }
        }
Beispiel #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);
            }));
        }
Beispiel #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);
            }));
        }
Beispiel #4
0
 /// <summary>
 /// Test the connection
 /// </summary>
 /// <returns>true if connection alive, otherwise false</returns>
 public bool TestConnection() => _connection.TestConnection();