/********************************************************************++ Copyright (c) Microsoft Corporation. All rights reserved. --********************************************************************/ using Dbg = System.Management.Automation.Diagnostics; using System.Runtime.Serialization; using System.Collections.Generic; namespace System.Management.Automation { /// /// /// Defines a data structure used to represent informational context destined for the host or user. /// /// /// /// /// InformationRecords are passed to , /// which, according to host or user preference, forwards that information on to the host for rendering to the user. /// /// /// [DataContract()] public class InformationRecord { /// /// /// Initializes a new instance of the InformationRecord class. /// /// /// The object to be transmitted to the host. /// The source of the message (i.e.: script path, function name, etc.) public InformationRecord(Object messageData, string source) { this.MessageData = messageData; this.Source = source; this.TimeGenerated = DateTime.Now; this.Tags = new List(); if (Platform.IsWindows) { this.User = System.Security.Principal.WindowsIdentity.GetCurrent().Name; } else { this.User = Platform.NonWindowsGetUserName(); } // Porting note: PsUtils.GetHostName() already handles platform specifics this.Computer = PsUtils.GetHostName(); this.ProcessId = (uint)System.Diagnostics.Process.GetCurrentProcess().Id; this.NativeThreadId = PsUtils.GetNativeThreadId(); this.ManagedThreadId = (uint)System.Threading.Thread.CurrentThread.ManagedThreadId; } /// /// Added to enable ClrFacade.GetUninitializedObject to instantiate an uninitialized version of this class. /// internal InformationRecord() { } /// /// Copy constructor /// internal InformationRecord(InformationRecord baseRecord) { this.MessageData = baseRecord.MessageData; this.Source = baseRecord.Source; this.TimeGenerated = baseRecord.TimeGenerated; this.Tags = baseRecord.Tags; this.User = baseRecord.User; this.Computer = baseRecord.Computer; this.ProcessId = baseRecord.ProcessId; this.NativeThreadId = baseRecord.NativeThreadId; this.ManagedThreadId = baseRecord.ManagedThreadId; } // Some of these setters are internal, while others are public. // The ones that are public are left that way because systems that proxy // the events may need to alter them (i.e.: workflow). The ones that remain internal // are that way because they are fundamental properties of the record itself. /// /// The message data for this informational record /// [DataMember] public Object MessageData { get; internal set; } /// /// The source of this informational record (script path, function name, etc.) /// [DataMember] public string Source { get; set; } /// /// The time this informational record was generated. /// [DataMember] public DateTime TimeGenerated { get; set; } /// /// The tags assocaited with this informational record (if any) /// [DataMember] [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public List Tags { get; internal set; } /// /// The user that generated this informational record /// [DataMember] public string User { get; set; } /// /// The computer that generated this informational record /// [DataMember] public string Computer { get; set; } /// /// The process that generated this informational record /// [DataMember] public uint ProcessId { get; set; } /// /// The native thread that generated this informational record /// public uint NativeThreadId { get; set; } /// /// The managed thread that generated this informational record /// [DataMember] public uint ManagedThreadId { get; set; } /// /// Converts an InformationRecord to a string-based representation. /// /// public override string ToString() { if (MessageData != null) { return MessageData.ToString(); } else { return base.ToString(); } } internal static InformationRecord FromPSObjectForRemoting(PSObject inputObject) { InformationRecord informationRecord = new InformationRecord(); informationRecord.MessageData = RemotingDecoder.GetPropertyValue(inputObject, "MessageData"); informationRecord.Source = RemotingDecoder.GetPropertyValue(inputObject, "Source"); informationRecord.TimeGenerated = RemotingDecoder.GetPropertyValue(inputObject, "TimeGenerated"); informationRecord.Tags = new List(); System.Collections.ArrayList tagsArrayList = RemotingDecoder.GetPropertyValue(inputObject, "Tags"); foreach (string tag in tagsArrayList) { informationRecord.Tags.Add(tag); } informationRecord.User = RemotingDecoder.GetPropertyValue(inputObject, "User"); informationRecord.Computer = RemotingDecoder.GetPropertyValue(inputObject, "Computer"); informationRecord.ProcessId = RemotingDecoder.GetPropertyValue(inputObject, "ProcessId"); informationRecord.NativeThreadId = RemotingDecoder.GetPropertyValue(inputObject, "NativeThreadId"); informationRecord.ManagedThreadId = RemotingDecoder.GetPropertyValue(inputObject, "ManagedThreadId"); return informationRecord; } /// /// Returns this object as a PSObject property bag /// that can be used in a remoting protocol data object. /// /// This object as a PSObject property bag internal PSObject ToPSObjectForRemoting() { PSObject informationAsPSObject = RemotingEncoder.CreateEmptyPSObject(); informationAsPSObject.Properties.Add(new PSNoteProperty("MessageData", this.MessageData)); informationAsPSObject.Properties.Add(new PSNoteProperty("Source", this.Source)); informationAsPSObject.Properties.Add(new PSNoteProperty("TimeGenerated", this.TimeGenerated)); informationAsPSObject.Properties.Add(new PSNoteProperty("Tags", this.Tags)); informationAsPSObject.Properties.Add(new PSNoteProperty("User", this.User)); informationAsPSObject.Properties.Add(new PSNoteProperty("Computer", this.Computer)); informationAsPSObject.Properties.Add(new PSNoteProperty("ProcessId", this.ProcessId)); informationAsPSObject.Properties.Add(new PSNoteProperty("NativeThreadId", this.NativeThreadId)); informationAsPSObject.Properties.Add(new PSNoteProperty("ManagedThreadId", this.ManagedThreadId)); return informationAsPSObject; } } /// /// Class that holds informational messages to represent output created by the /// Write-Host cmdlet. /// public class HostInformationMessage { /// /// The message being output by the host /// public string Message { get; set; } /// /// 'True' if the host should not append a NewLine to the message output /// public bool? NoNewLine { get; set; } /// /// The foreground color of the message /// public ConsoleColor? ForegroundColor { get; set; } /// /// The background color of the message /// public ConsoleColor? BackgroundColor { get; set; } /// /// Returns a string-based representation of the host information message /// /// public override string ToString() { return Message; } } }