I have a PowerShell script CSV2JSON.ps1with the following code:
param(
[String]$FieldSeparator=",",
[String[]]$Header
)
$input | ConvertFrom-Csv -Delimiter $FieldSeparator -Header $Header | ConvertTo-JSON
If I call it .\CSV2JSON.ps1 -FieldSeparator "|" -Header "name|age", it works fine. However, if I omit the optional parameter Header, the cmdlet ConvertFrom-Csvcomplains that Headerit cannot be null:
ConvertFrom-Csv : Cannot validate argument on parameter 'Header'.
The argument is null. Supply a non-null argument and try the command again.
I do not want to pass the parameter -Headerat all if it is not specified. Is there a neat way to pass optional parameters without going into instructions If?
source
share