我的PowerShell脚本接受一个参数来指示输出是否应该有一个头,所以我目前这样做:
if ($null -eq $noHeader) {
Send-SQLDataToExcel -Connection $connection -mssqlserver -SQL $SQL -Path $output
} else {
Send-SQLDataToExcel -Connection $connection -mssqlserver -SQL $SQL -Path $output -NoHeader
}
有没有更好的不重复代码的方法?
谢谢。
是的,看看如何处理不同的参数场景,例如:
$splatHt = @{
connection=$connection
mssqlserver=$true
sql=$sql
path=$output
}
If ($noHeader){
$splatHt.add('NoHeader',$true)
}
Send-SQLDataToExcel @splatHt
由于目前唯一的区别是一个SWITCH参数,您还可以这样做:
$header = $false
If ($noHeader){
$header = $true
}
Send-SQLDataToExcel -Connection $connection -mssqlserver -SQL $SQL -Path $output -NoHeader:$header