mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Make Export-Csv -Append and -NoHeader mutually exclusive (#26472)
This commit is contained in:
@@ -270,6 +270,14 @@ namespace Microsoft.PowerShell.Commands
|
||||
this.ThrowTerminatingError(errorRecord);
|
||||
}
|
||||
|
||||
// Validate that Append and NoHeader are not specified together.
|
||||
if (Append && NoHeader)
|
||||
{
|
||||
InvalidOperationException exception = new(CsvCommandStrings.CannotSpecifyAppendAndNoHeader);
|
||||
ErrorRecord errorRecord = new(exception, "CannotSpecifyBothAppendAndNoHeader", ErrorCategory.InvalidData, null);
|
||||
this.ThrowTerminatingError(errorRecord);
|
||||
}
|
||||
|
||||
_shouldProcess = ShouldProcess(Path);
|
||||
if (!_shouldProcess)
|
||||
{
|
||||
|
||||
@@ -157,4 +157,7 @@
|
||||
<data name="EOFIsReached" xml:space="preserve">
|
||||
<value>EOF is reached.</value>
|
||||
</data>
|
||||
<data name="CannotSpecifyAppendAndNoHeader" xml:space="preserve">
|
||||
<value>You must specify either the -Append or -NoHeader parameters, but not both.</value>
|
||||
</data>
|
||||
</root>
|
||||
|
||||
@@ -187,6 +187,10 @@ Describe "Export-Csv" -Tags "CI" {
|
||||
$results[1].PSObject.properties.Name | Should -Not -Contain 'third'
|
||||
}
|
||||
|
||||
It "Should throw when -Append and -NoHeader are specified together" {
|
||||
{ $P1 | Export-Csv -Path $testCsv -Append -NoHeader -ErrorAction Stop } | Should -Throw -ErrorId "CannotSpecifyBothAppendAndNoHeader,Microsoft.PowerShell.Commands.ExportCsvCommand"
|
||||
}
|
||||
|
||||
It "First line should be #TYPE if -IncludeTypeInformation used and pstypenames object property is empty" {
|
||||
$object = [PSCustomObject]@{first = 1}
|
||||
$pstypenames = $object.pstypenames | ForEach-Object -Process {$_}
|
||||
|
||||
Reference in New Issue
Block a user