diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 5fe799ae0b2..62ccb423a78 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -6366,6 +6366,14 @@ public IContentReader GetContentReader(string path) try { + if (Directory.Exists(path)) + { + string errMsg = StringUtil.Format(SessionStateStrings.GetContainerContentException, path); + ErrorRecord error = new ErrorRecord(new InvalidOperationException(errMsg), "GetContainerContentException", ErrorCategory.InvalidOperation, null); + WriteError(error); + return stream; + } + // Users can't both read as bytes, and specify a delimiter if (delimiterSpecified) { @@ -6514,6 +6522,14 @@ public IContentWriter GetContentWriter(string path) try { + if (Directory.Exists(path)) + { + string errMsg = StringUtil.Format(SessionStateStrings.WriteContainerContentException, path); + ErrorRecord error = new ErrorRecord(new InvalidOperationException(errMsg), "WriteContainerContentException", ErrorCategory.InvalidOperation, null); + WriteError(error); + return stream; + } + stream = new FileSystemContentReaderWriter(path, streamName, filemode, FileAccess.Write, FileShare.Write, encoding, usingByteEncoding, false, this, false, suppressNewline); } catch (PathTooLongException pathTooLong) diff --git a/src/System.Management.Automation/resources/SessionStateStrings.resx b/src/System.Management.Automation/resources/SessionStateStrings.resx index 855c7975e30..c0ced013dea 100644 --- a/src/System.Management.Automation/resources/SessionStateStrings.resx +++ b/src/System.Management.Automation/resources/SessionStateStrings.resx @@ -279,6 +279,12 @@ The dynamic parameters for the GetContentWriter operation cannot be retrieved for the '{0}' provider for path '{1}'. {2} + + Unable to get content because it is a directory: '{0}'. Please use 'Get-ChildItem' instead. + + + Unable to write content because it is a directory: '{0}'. + There is no location history left to navigate backwards. diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Add-Content.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Add-Content.Tests.ps1 index f6f76089a7b..53ad2499bdd 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Add-Content.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Add-Content.Tests.ps1 @@ -40,6 +40,10 @@ Describe "Add-Content cmdlet tests" -Tags "CI" { { Add-Content -Path $() -Value "ShouldNotWorkBecausePathIsInvalid" -ErrorAction Stop } | Should -Throw -ErrorId "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentCommand" } + It "Should throw an error on a directory" { + { Add-Content -Path . -Value "WriteContainerContentException" -ErrorAction Stop } | Should -Throw -ErrorId "WriteContainerContentException,Microsoft.PowerShell.Commands.AddContentCommand" + } + #[BugId(BugDatabase.WindowsOutOfBandReleases, 906022)] It "should throw 'NotSupportedException' when you add-content to an unsupported provider" -Skip:($IsLinux -Or $IsMacOS) { { Add-Content -Path HKLM:\\software\\microsoft -Value "ShouldNotWorkBecausePathIsUnsupported" -ErrorAction Stop } | Should -Throw -ErrorId "NotSupported,Microsoft.PowerShell.Commands.AddContentCommand" diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Content.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Content.Tests.ps1 index dfb389e024f..aa1650e0711 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Content.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Content.Tests.ps1 @@ -24,9 +24,9 @@ Describe "Get-Content" -Tags "CI" { Remove-Item -Path $testPath2 -Force } - It "Should throw an error on a directory " { + It "Should throw an error on a directory" { { Get-Content . -ErrorAction Stop } | - Should -Throw -ErrorId "GetContentReaderUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetContentCommand" + Should -Throw -ErrorId "GetContainerContentException,Microsoft.PowerShell.Commands.GetContentCommand" } It "Should return an Object when listing only a single line and the correct information from a file" {