diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/StartTranscriptCmdlet.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/StartTranscriptCmdlet.cs index 5453731ba6..52ff019906 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/StartTranscriptCmdlet.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/StartTranscriptCmdlet.cs @@ -212,10 +212,10 @@ namespace Microsoft.PowerShell.Commands } } - // If they didn't specify -Append, delete the file + // If they didn't specify -Append, empty the file if (!_shouldAppend) { - System.IO.File.Delete(effectiveFilePath); + System.IO.File.WriteAllText(effectiveFilePath, string.Empty); } } 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 423c4176ea..ade669dcc9 100644 --- a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 @@ -108,6 +108,27 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { $expectedError = "CannotStartTranscription,Microsoft.PowerShell.Commands.StartTranscriptCommand" ValidateTranscription -scriptToExecute $script -outputFilePath $null -expectedError $expectedError } + It "Should not delete the file if it already exist" { + # Create an existing file + $transcriptFilePath = Join-Path $TestDrive ([System.IO.Path]::GetRandomFileName()) + Out-File $transcriptFilePath + + $FileSystemWatcher = [System.IO.FileSystemWatcher]::new((Split-Path -Parent $transcriptFilePath), (Split-Path -Leaf $transcriptFilePath)) + + $Job = Register-ObjectEvent -InputObject $FileSystemWatcher -EventName "Deleted" -SourceIdentifier "FileDeleted" -Action { + return "FileDeleted" + } + + try { + Start-Transcript -Path $transcriptFilePath + Stop-Transcript + } finally { + Unregister-Event -SourceIdentifier "FileDeleted" + } + + # Nothing should have been returned by the FileSystemWatcher + Receive-Job $job | Should -Be $null + } It "Transcription should remain active if other runspace in the host get closed" { try { $ps = [powershell]::Create()