コード例 #1
0
        internal static IEnumerable <CimModule> GetCimModules(
            CimSession cimSession,
            Uri resourceUri,
            string cimNamespace,
            IEnumerable <string> moduleNamePatterns,
            bool onlyManifests,
            Cmdlet cmdlet,
            CancellationToken cancellationToken)
        {
            moduleNamePatterns = moduleNamePatterns ?? new[] { "*" };
            HashSet <string> alreadyEmittedNamesOfCimModules = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            IEnumerable <CimModule> remoteModules = moduleNamePatterns
                                                    .SelectMany(moduleNamePattern =>
                                                                RemoteDiscoveryHelper.GetCimModules(cimSession, resourceUri, cimNamespace, moduleNamePattern, onlyManifests, cmdlet, cancellationToken));

            foreach (CimModule remoteModule in remoteModules)
            {
                if (!alreadyEmittedNamesOfCimModules.Contains(remoteModule.ModuleName))
                {
                    alreadyEmittedNamesOfCimModules.Add(remoteModule.ModuleName);
                    yield return(remoteModule);
                }
            }
        }
コード例 #2
0
        private static IEnumerable <PSObject> InvokeTopLevelPowerShell(PowerShell powerShell, CancellationToken cancellationToken, PSCmdlet cmdlet, PSInvocationSettings invocationSettings, string errorMessageTemplate)
        {
            Action <PSObject>      action        = null;
            Action <ErrorRecord>   action1       = null;
            Action <WarningRecord> action2       = null;
            Action <VerboseRecord> action3       = null;
            Action <DebugRecord>   action4       = null;
            AsyncCallback          asyncCallback = null;

            using (BlockingCollection <Func <PSCmdlet, IEnumerable <PSObject> > > funcs = new BlockingCollection <Func <PSCmdlet, IEnumerable <PSObject> > >(RemoteDiscoveryHelper.BlockingCollectionCapacity))
            {
                PSDataCollection <PSObject> pSObjects = new PSDataCollection <PSObject>();
                if (action == null)
                {
                    action = (PSObject output) => funcs.Add((PSCmdlet argument0) =>
                    {
                        PSObject[] pSObjectArray = new PSObject[1];
                        pSObjectArray[0]         = output;
                        return(pSObjectArray);
                    }
                                                            );
                }
                EventHandler <DataAddedEventArgs> streamForwarder = RemoteDiscoveryHelper.GetStreamForwarder <PSObject>(action, true);
                if (action1 == null)
                {
                    action1 = (ErrorRecord errorRecord) => funcs.Add((PSCmdlet c) =>
                    {
                        errorRecord = RemoteDiscoveryHelper.GetErrorRecordForRemotePipelineInvocation(errorRecord, errorMessageTemplate);
                        RemoteDiscoveryHelper.HandleErrorFromPipeline(c, errorRecord, powerShell);
                        return(Enumerable.Empty <PSObject>());
                    }
                                                                     );
                }
                EventHandler <DataAddedEventArgs> eventHandler = RemoteDiscoveryHelper.GetStreamForwarder <ErrorRecord>(action1, true);
                if (action2 == null)
                {
                    action2 = (WarningRecord warningRecord) => funcs.Add((PSCmdlet c) =>
                    {
                        c.WriteWarning(warningRecord.Message);
                        return(Enumerable.Empty <PSObject>());
                    }
                                                                         );
                }
                EventHandler <DataAddedEventArgs> streamForwarder1 = RemoteDiscoveryHelper.GetStreamForwarder <WarningRecord>(action2, true);
                if (action3 == null)
                {
                    action3 = (VerboseRecord verboseRecord) => funcs.Add((PSCmdlet c) =>
                    {
                        c.WriteVerbose(verboseRecord.Message);
                        return(Enumerable.Empty <PSObject>());
                    }
                                                                         );
                }
                EventHandler <DataAddedEventArgs> eventHandler1 = RemoteDiscoveryHelper.GetStreamForwarder <VerboseRecord>(action3, true);
                if (action4 == null)
                {
                    action4 = (DebugRecord debugRecord) => funcs.Add((PSCmdlet c) =>
                    {
                        c.WriteDebug(debugRecord.Message);
                        return(Enumerable.Empty <PSObject>());
                    }
                                                                     );
                }
                EventHandler <DataAddedEventArgs> streamForwarder2 = RemoteDiscoveryHelper.GetStreamForwarder <DebugRecord>(action4, true);
                pSObjects.DataAdded += streamForwarder;
                powerShell.Streams.Error.DataAdded   += eventHandler;
                powerShell.Streams.Warning.DataAdded += streamForwarder1;
                powerShell.Streams.Verbose.DataAdded += eventHandler1;
                powerShell.Streams.Debug.DataAdded   += streamForwarder2;
                try
                {
                    PowerShell powerShell1 = powerShell;
                    object     obj         = null;
                    PSDataCollection <PSObject> pSObjects1          = pSObjects;
                    PSInvocationSettings        pSInvocationSetting = invocationSettings;
                    if (asyncCallback == null)
                    {
                        asyncCallback = (IAsyncResult param0) =>
                        {
                            try
                            {
                                funcs.CompleteAdding();
                            }
                            catch (InvalidOperationException invalidOperationException)
                            {
                            }
                        }
                        ;
                    }
                    IAsyncResult asyncResult = powerShell1.BeginInvoke <PSObject, PSObject>((PSDataCollection <PSObject>)obj, pSObjects1, pSInvocationSetting, asyncCallback, null);
                    CancellationTokenRegistration cancellationTokenRegistration = cancellationToken.Register(new Action(powerShell.Stop));
                    try
                    {
                        try
                        {
                            foreach (Func <PSCmdlet, IEnumerable <PSObject> > func in funcs)
                            {
                                IEnumerator <PSObject> enumerator = func(cmdlet).GetEnumerator();
                                using (enumerator)
                                {
                                    while (enumerator.MoveNext())
                                    {
                                        yield return(enumerator.Current);
                                    }
                                }
                            }
                        }
                        finally
                        {
                            funcs.CompleteAdding();
                            powerShell.EndInvoke(asyncResult);
                        }
                    }
                    finally
                    {
                        cancellationTokenRegistration.Dispose();
                    }
                }
                finally
                {
                    pSObjects.DataAdded -= streamForwarder;
                    powerShell.Streams.Error.DataAdded   -= eventHandler;
                    powerShell.Streams.Warning.DataAdded -= streamForwarder1;
                    powerShell.Streams.Verbose.DataAdded -= eventHandler1;
                    powerShell.Streams.Debug.DataAdded   -= streamForwarder2;
                }
            }
        }