diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/StartTranscriptCmdlet.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/StartTranscriptCmdlet.cs index 52ff019906..5c1de82aad 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/StartTranscriptCmdlet.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/StartTranscriptCmdlet.cs @@ -137,6 +137,15 @@ namespace Microsoft.PowerShell.Commands get; set; } + /// + /// Gets or sets whether to use minimal transcript header. + /// + [Parameter] + public SwitchParameter UseMinimalHeader + { + get; set; + } + /// /// Starts the transcription /// @@ -221,7 +230,7 @@ namespace Microsoft.PowerShell.Commands System.Management.Automation.Remoting.PSSenderInfo psSenderInfo = this.SessionState.PSVariable.GetValue("PSSenderInfo") as System.Management.Automation.Remoting.PSSenderInfo; - Host.UI.StartTranscribing(effectiveFilePath, psSenderInfo, IncludeInvocationHeader.ToBool()); + Host.UI.StartTranscribing(effectiveFilePath, psSenderInfo, IncludeInvocationHeader.ToBool(), UseMinimalHeader.IsPresent); // ch.StartTranscribing(effectiveFilePath, Append); diff --git a/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs b/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs index 71a82b3f0a..632952c370 100644 --- a/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs +++ b/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs @@ -440,66 +440,79 @@ namespace System.Management.Automation.Host } } - internal void StartTranscribing(string path, System.Management.Automation.Remoting.PSSenderInfo senderInfo, bool includeInvocationHeader) + internal void StartTranscribing(string path, System.Management.Automation.Remoting.PSSenderInfo senderInfo, bool includeInvocationHeader, bool useMinimalHeader) { TranscriptionOption transcript = new TranscriptionOption(); transcript.Path = path; transcript.IncludeInvocationHeader = includeInvocationHeader; TranscriptionData.Transcripts.Add(transcript); - LogTranscriptHeader(senderInfo, transcript); + LogTranscriptHeader(senderInfo, transcript, useMinimalHeader); } - private void LogTranscriptHeader(System.Management.Automation.Remoting.PSSenderInfo senderInfo, TranscriptionOption transcript) + private void LogTranscriptHeader(System.Management.Automation.Remoting.PSSenderInfo senderInfo, TranscriptionOption transcript, bool useMinimalHeader = false) { - string username = Environment.UserDomainName + "\\" + Environment.UserName; - string runAsUser = username; - - if (senderInfo != null) - { - username = senderInfo.UserInfo.Identity.Name; - } - - // Add bits from PSVersionTable - StringBuilder psVersionInfo = new StringBuilder(); - Hashtable versionInfo = PSVersionInfo.GetPSVersionTable(); - foreach (string versionKey in versionInfo.Keys) - { - Object value = versionInfo[versionKey]; - - if (value != null) - { - var arrayValue = value as object[]; - string valueString = arrayValue != null ? string.Join(", ", arrayValue) : value.ToString(); - psVersionInfo.AppendLine(versionKey + ": " + valueString); - } - } - string psConfigurationName = string.Empty; - if (senderInfo != null && !string.IsNullOrEmpty(senderInfo.ConfigurationName)) - { - psConfigurationName = senderInfo.ConfigurationName; - } // Transcribe the transcript header - string format = InternalHostUserInterfaceStrings.TranscriptPrologue; - string line = - String.Format( - Globalization.CultureInfo.InvariantCulture, - format, - DateTime.Now, - username, - runAsUser, - psConfigurationName, - Environment.MachineName, - Environment.OSVersion.VersionString, - String.Join(" ", Environment.GetCommandLineArgs()), - System.Diagnostics.Process.GetCurrentProcess().Id, - psVersionInfo.ToString().TrimEnd() - ); + string line; + if (useMinimalHeader) + { + line = + string.Format( + Globalization.CultureInfo.InvariantCulture, + InternalHostUserInterfaceStrings.MinimalTranscriptPrologue, + DateTime.Now); + } + else + { + string username = Environment.UserDomainName + "\\" + Environment.UserName; + string runAsUser = username; + + if (senderInfo != null) + { + username = senderInfo.UserInfo.Identity.Name; + } + + // Add bits from PSVersionTable + StringBuilder versionInfoFooter = new StringBuilder(); + Hashtable versionInfo = PSVersionInfo.GetPSVersionTable(); + foreach (string versionKey in versionInfo.Keys) + { + object value = versionInfo[versionKey]; + + if (value != null) + { + var arrayValue = value as object[]; + string valueString = arrayValue != null ? string.Join(", ", arrayValue) : value.ToString(); + versionInfoFooter.AppendLine(versionKey + ": " + valueString); + } + } + + string configurationName = string.Empty; + if (senderInfo != null && !string.IsNullOrEmpty(senderInfo.ConfigurationName)) + { + configurationName = senderInfo.ConfigurationName; + } + + line = + string.Format( + Globalization.CultureInfo.InvariantCulture, + InternalHostUserInterfaceStrings.TranscriptPrologue, + DateTime.Now, + username, + runAsUser, + configurationName, + Environment.MachineName, + Environment.OSVersion.VersionString, + string.Join(" ", Environment.GetCommandLineArgs()), + System.Diagnostics.Process.GetCurrentProcess().Id, + versionInfoFooter.ToString().TrimEnd()); + } lock (transcript.OutputToLog) { transcript.OutputToLog.Add(line); } + TranscribeCommandComplete(null); } diff --git a/src/System.Management.Automation/resources/InternalHostUserInterfaceStrings.resx b/src/System.Management.Automation/resources/InternalHostUserInterfaceStrings.resx index a40b58cebc..7a8882643d 100644 --- a/src/System.Management.Automation/resources/InternalHostUserInterfaceStrings.resx +++ b/src/System.Management.Automation/resources/InternalHostUserInterfaceStrings.resx @@ -203,6 +203,12 @@ Machine: {4} ({5}) Host Application: {6} Process ID: {7} {8} +********************** + + + ********************** +PowerShell transcript start +Start time: {0:yyyyMMddHHmmss} ********************** diff --git a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 index ade669dcc9..28f6dbc261 100644 --- a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 @@ -293,4 +293,28 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { $transcriptFilePath | Should -Exist $transcriptFilePath | Should -FileContentMatchMultiline $expectedContent } + + It "UseMinimalHeader should reduce length of transcript header" { + $script = { + Start-Transcript -Path $transcriptFilePath + Stop-Transcript + } + + $transcriptMinHeaderFilePath = $transcriptFilePath + "_minimal" + $scriptMinHeader = { + Start-Transcript -Path $transcriptMinHeaderFilePath -UseMinimalHeader + Stop-Transcript + } + + & $script + $transcriptFilePath | Should -Exist + $transcriptLength = (Get-Content -Path $transcriptFilePath -Raw).Length + + & $scriptMinHeader + $transcriptMinHeaderFilePath | Should -Exist + $transcriptMinHeaderLength = (Get-Content -Path $transcriptMinHeaderFilePath -Raw).Length + Remove-Item $transcriptMinHeaderFilePath -ErrorAction SilentlyContinue + + $transcriptMinHeaderLength | Should -BeLessThan $transcriptLength + } }