mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Add -UseMinimalHeader to Start-Transcript to minimize transcript header (#8402)
This commit is contained in:
@@ -137,6 +137,15 @@ namespace Microsoft.PowerShell.Commands
|
||||
get; set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets whether to use minimal transcript header.
|
||||
/// </summary>
|
||||
[Parameter]
|
||||
public SwitchParameter UseMinimalHeader
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts the transcription
|
||||
/// </summary>
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -203,6 +203,12 @@ Machine: {4} ({5})
|
||||
Host Application: {6}
|
||||
Process ID: {7}
|
||||
{8}
|
||||
**********************</value>
|
||||
</data>
|
||||
<data name="MinimalTranscriptPrologue" xml:space="preserve">
|
||||
<value>**********************
|
||||
PowerShell transcript start
|
||||
Start time: {0:yyyyMMddHHmmss}
|
||||
**********************</value>
|
||||
</data>
|
||||
<data name="TranscriptEpilogue" xml:space="preserve">
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user