Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
InformationRecord.cs265 linesDownload Raw Back to engine
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.Collections.Generic;5using System.Runtime.Serialization;6 7namespace System.Management.Automation8{9    /// <summary>10    /// Defines a data structure used to represent informational context destined for the host or user.11    /// </summary>12    /// <remarks>13    /// InformationRecords are passed to <see cref="System.Management.Automation.Cmdlet.WriteInformation(object, string[])"/>,14    /// which, according to host or user preference, forwards that information on to the host for rendering to the user.15    /// </remarks>16    /// <seealso cref="System.Management.Automation.Cmdlet.WriteInformation(object, string[])"/>17    [DataContract]18    public class InformationRecord19    {20        /// <summary>21        /// Initializes a new instance of the InformationRecord class.22        /// </summary>23        /// <param name="messageData">The object to be transmitted to the host.</param>24        /// <param name="source">The source of the message (i.e.: script path, function name, etc.).</param>25        public InformationRecord(object messageData, string source)26        {27            this.MessageData = messageData;28            this.Source = source;29 30            this.TimeGenerated = DateTime.Now;31            this.NativeThreadId = PsUtils.GetNativeThreadId();32            this.ManagedThreadId = (uint)Environment.CurrentManagedThreadId;33        }34 35        private InformationRecord() { }36 37        /// <summary>38        /// Copy constructor.39        /// </summary>40        internal InformationRecord(InformationRecord baseRecord)41        {42            this.MessageData = baseRecord.MessageData;43            this.Source = baseRecord.Source;44 45            this.TimeGenerated = baseRecord.TimeGenerated;46            this.Tags = baseRecord.Tags;47            this.User = baseRecord.User;48            this.Computer = baseRecord.Computer;49            this.ProcessId = baseRecord.ProcessId;50            this.NativeThreadId = baseRecord.NativeThreadId;51            this.ManagedThreadId = baseRecord.ManagedThreadId;52        }53 54        // Some of these setters are internal, while others are public.55        // The ones that are public are left that way because systems that proxy56        // the events may need to alter them (i.e.: workflow). The ones that remain internal57        // are that way because they are fundamental properties of the record itself.58 59        /// <summary>60        /// The message data for this informational record.61        /// </summary>62        [DataMember]63        public object MessageData { get; internal set; }64 65        /// <summary>66        /// The source of this informational record (script path, function name, etc.)67        /// </summary>68        [DataMember]69        public string Source { get; set; }70 71        /// <summary>72        /// The time this informational record was generated.73        /// </summary>74        [DataMember]75        public DateTime TimeGenerated { get; set; }76 77        /// <summary>78        /// The tags associated with this informational record (if any)79        /// </summary>80        [DataMember]81        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]82        public List<string> Tags83        {84            get { return _tags ??= new List<string>(); }85 86            internal set { _tags = value; }87        }88 89        private List<string> _tags;90 91        /// <summary>92        /// The user that generated this informational record.93        /// </summary>94        [DataMember]95        public string User96        {97            get98            {99                // domain\user on Windows, just user on Unix100                this._user ??=101#if UNIX102                    Environment.UserName;103#else104                    Environment.UserDomainName + "\\" + Environment.UserName;105#endif106 107                return _user;108            }109 110            set111            {112                _user = value;113            }114        }115 116        private string _user;117 118        /// <summary>119        /// The computer that generated this informational record.120        /// </summary>121        [DataMember]122        public string Computer123        {124            get { return this._computerName ??= PsUtils.GetHostName(); }125 126            set { this._computerName = value; }127        }128 129        private string _computerName;130 131        /// <summary>132        /// The process that generated this informational record.133        /// </summary>134        [DataMember]135        public uint ProcessId136        {137            get138            {139                if (!this._processId.HasValue)140                {141                    this._processId = (uint)Environment.ProcessId;142                }143 144                return this._processId.Value;145            }146 147            set148            {149                _processId = value;150            }151        }152 153        private uint? _processId;154 155        /// <summary>156        /// The native thread that generated this informational record.157        /// </summary>158        public uint NativeThreadId { get; set; }159 160        /// <summary>161        /// The managed thread that generated this informational record.162        /// </summary>163        [DataMember]164        public uint ManagedThreadId { get; set; }165 166        /// <summary>167        /// Converts an InformationRecord to a string-based representation.168        /// </summary>169        /// <returns></returns>170        public override string ToString()171        {172            if (MessageData != null)173            {174                return MessageData.ToString();175            }176            else177            {178                return base.ToString();179            }180        }181 182        internal static InformationRecord FromPSObjectForRemoting(PSObject inputObject)183        {184            InformationRecord informationRecord = new InformationRecord();185 186            informationRecord.MessageData = RemotingDecoder.GetPropertyValue<object>(inputObject, "MessageData");187            informationRecord.Source = RemotingDecoder.GetPropertyValue<string>(inputObject, "Source");188            informationRecord.TimeGenerated = RemotingDecoder.GetPropertyValue<DateTime>(inputObject, "TimeGenerated");189 190            informationRecord.Tags = new List<string>();191            System.Collections.ArrayList tagsArrayList = RemotingDecoder.GetPropertyValue<System.Collections.ArrayList>(inputObject, "Tags");192            foreach (string tag in tagsArrayList)193            {194                informationRecord.Tags.Add(tag);195            }196 197            informationRecord.User = RemotingDecoder.GetPropertyValue<string>(inputObject, "User");198            informationRecord.Computer = RemotingDecoder.GetPropertyValue<string>(inputObject, "Computer");199            informationRecord.ProcessId = RemotingDecoder.GetPropertyValue<uint>(inputObject, "ProcessId");200            informationRecord.NativeThreadId = RemotingDecoder.GetPropertyValue<uint>(inputObject, "NativeThreadId");201            informationRecord.ManagedThreadId = RemotingDecoder.GetPropertyValue<uint>(inputObject, "ManagedThreadId");202 203            return informationRecord;204        }205 206        /// <summary>207        /// Returns this object as a PSObject property bag208        /// that can be used in a remoting protocol data object.209        /// </summary>210        /// <returns>This object as a PSObject property bag.</returns>211        internal PSObject ToPSObjectForRemoting()212        {213            PSObject informationAsPSObject = RemotingEncoder.CreateEmptyPSObject();214 215            informationAsPSObject.Properties.Add(new PSNoteProperty("MessageData", this.MessageData));216            informationAsPSObject.Properties.Add(new PSNoteProperty("Source", this.Source));217            informationAsPSObject.Properties.Add(new PSNoteProperty("TimeGenerated", this.TimeGenerated));218            informationAsPSObject.Properties.Add(new PSNoteProperty("Tags", this.Tags));219            informationAsPSObject.Properties.Add(new PSNoteProperty("User", this.User));220            informationAsPSObject.Properties.Add(new PSNoteProperty("Computer", this.Computer));221            informationAsPSObject.Properties.Add(new PSNoteProperty("ProcessId", this.ProcessId));222            informationAsPSObject.Properties.Add(new PSNoteProperty("NativeThreadId", this.NativeThreadId));223            informationAsPSObject.Properties.Add(new PSNoteProperty("ManagedThreadId", this.ManagedThreadId));224 225            return informationAsPSObject;226        }227    }228 229    /// <summary>230    /// Class that holds informational messages to represent output created by the231    /// Write-Host cmdlet.232    /// </summary>233    public class HostInformationMessage234    {235        /// <summary>236        /// The message being output by the host.237        /// </summary>238        public string Message { get; set; }239 240        /// <summary>241        /// 'True' if the host should not append a NewLine to the message output.242        /// </summary>243        public bool? NoNewLine { get; set; }244 245        /// <summary>246        /// The foreground color of the message.247        /// </summary>248        public ConsoleColor? ForegroundColor { get; set; }249 250        /// <summary>251        /// The background color of the message.252        /// </summary>253        public ConsoleColor? BackgroundColor { get; set; }254 255        /// <summary>256        /// Returns a string-based representation of the host information message.257        /// </summary>258        /// <returns></returns>259        public override string ToString()260        {261            return Message;262        }263    }264}265