Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
PowerShellStreams.cs260 linesDownload Raw Back to client
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4namespace System.Management.Automation5{6    /// <summary>7    /// Define all the output streams and one input stream for a workflow.8    /// </summary>9    public sealed class PowerShellStreams<TInput, TOutput> : IDisposable10    {11        /// <summary>12        /// Input stream for incoming objects.13        /// </summary>14        private PSDataCollection<TInput> _inputStream;15 16        /// <summary>17        /// Output stream for returned objects.18        /// </summary>19        private PSDataCollection<TOutput> _outputStream;20 21        /// <summary>22        /// Error stream for error messages.23        /// </summary>24        private PSDataCollection<ErrorRecord> _errorStream;25 26        /// <summary>27        /// Warning stream for warning messages.28        /// </summary>29        private PSDataCollection<WarningRecord> _warningStream;30 31        /// <summary>32        /// Progress stream for progress messages.33        /// </summary>34        private PSDataCollection<ProgressRecord> _progressStream;35 36        /// <summary>37        /// Verbose stream for verbose messages.38        /// </summary>39        private PSDataCollection<VerboseRecord> _verboseStream;40 41        /// <summary>42        /// Debug stream for debug messages.43        /// </summary>44        private PSDataCollection<DebugRecord> _debugStream;45 46        /// <summary>47        /// Information stream for Information messages.48        /// </summary>49        private PSDataCollection<InformationRecord> _informationStream;50 51        /// <summary>52        /// If the object is already disposed or not.53        /// </summary>54        private bool _disposed;55 56        /// <summary>57        /// Private object for thread-safe execution.58        /// </summary>59        private readonly object _syncLock = new object();60 61        /// <summary>62        /// Default constructor.63        /// </summary>64        public PowerShellStreams()65        {66            _inputStream = null;67            _outputStream = null;68            _errorStream = null;69            _warningStream = null;70            _progressStream = null;71            _verboseStream = null;72            _debugStream = null;73            _informationStream = null;74 75            _disposed = false;76        }77 78        /// <summary>79        /// Default constructor.80        /// </summary>81        public PowerShellStreams(PSDataCollection<TInput> pipelineInput)82        {83            // Populate the input collection if there is any...84            _inputStream = pipelineInput ?? new PSDataCollection<TInput>();85 86            _inputStream.Complete();87 88            _outputStream = new PSDataCollection<TOutput>();89            _errorStream = new PSDataCollection<ErrorRecord>();90            _warningStream = new PSDataCollection<WarningRecord>();91            _progressStream = new PSDataCollection<ProgressRecord>();92            _verboseStream = new PSDataCollection<VerboseRecord>();93            _debugStream = new PSDataCollection<DebugRecord>();94            _informationStream = new PSDataCollection<InformationRecord>();95 96            _disposed = false;97        }98 99        /// <summary>100        /// Dispose implementation.101        /// </summary>102        public void Dispose()103        {104            this.Dispose(true);105            GC.SuppressFinalize(this);106        }107 108        /// <summary>109        /// Protected virtual implementation of Dispose.110        /// </summary>111        /// <param name="disposing"></param>112        private void Dispose(bool disposing)113        {114            if (_disposed)115                return;116 117            lock (_syncLock)118            {119                if (!_disposed)120                {121                    if (disposing)122                    {123                        _inputStream.Dispose();124                        _outputStream.Dispose();125                        _errorStream.Dispose();126                        _warningStream.Dispose();127                        _progressStream.Dispose();128                        _verboseStream.Dispose();129                        _debugStream.Dispose();130                        _informationStream.Dispose();131 132                        _inputStream = null;133                        _outputStream = null;134                        _errorStream = null;135                        _warningStream = null;136                        _progressStream = null;137                        _verboseStream = null;138                        _debugStream = null;139                        _informationStream = null;140                    }141 142                    _disposed = true;143                }144            }145        }146 147        /// <summary>148        /// Gets input stream.149        /// </summary>150        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]151        public PSDataCollection<TInput> InputStream152        {153            get { return _inputStream; }154 155            set { _inputStream = value; }156        }157 158        /// <summary>159        /// Gets output stream.160        /// </summary>161        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]162        public PSDataCollection<TOutput> OutputStream163        {164            get { return _outputStream; }165 166            set { _outputStream = value; }167        }168 169        /// <summary>170        /// Gets error stream.171        /// </summary>172        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]173        public PSDataCollection<ErrorRecord> ErrorStream174        {175            get { return _errorStream; }176 177            set { _errorStream = value; }178        }179 180        /// <summary>181        /// Gets warning stream.182        /// </summary>183        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]184        public PSDataCollection<WarningRecord> WarningStream185        {186            get { return _warningStream; }187 188            set { _warningStream = value; }189        }190 191        /// <summary>192        /// Gets progress stream.193        /// </summary>194        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]195        public PSDataCollection<ProgressRecord> ProgressStream196        {197            get { return _progressStream; }198 199            set { _progressStream = value; }200        }201 202        /// <summary>203        /// Gets verbose stream.204        /// </summary>205        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]206        public PSDataCollection<VerboseRecord> VerboseStream207        {208            get { return _verboseStream; }209 210            set { _verboseStream = value; }211        }212 213        /// <summary>214        /// Get debug stream.215        /// </summary>216        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]217        public PSDataCollection<DebugRecord> DebugStream218        {219            get { return _debugStream; }220 221            set { _debugStream = value; }222        }223 224        /// <summary>225        /// Gets Information stream.226        /// </summary>227        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]228        public PSDataCollection<InformationRecord> InformationStream229        {230            get { return _informationStream; }231 232            set { _informationStream = value; }233        }234 235        /// <summary>236        /// Marking all the streams as completed so that no further data can be added and237        /// jobs will know that there is no more data coming in.238        /// </summary>239        public void CloseAll()240        {241            if (!_disposed)242            {243                lock (_syncLock)244                {245                    if (!_disposed)246                    {247                        _outputStream.Complete();248                        _errorStream.Complete();249                        _warningStream.Complete();250                        _progressStream.Complete();251                        _verboseStream.Complete();252                        _debugStream.Complete();253                        _informationStream.Complete();254                    }255                }256            }257        }258    }259}260