Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -OutputFile option #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions Invoke-SqlCmd2/Public/Invoke-SqlCmd2.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,11 @@ function Invoke-Sqlcmd2 {
[String]$ApplicationName,
[Parameter(Position = 13,
Mandatory = $false)]
[switch]$MessagesToOutput
[switch]$MessagesToOutput,
[Parameter(Position = 14,
Mandatory = $false)]
[ValidateScript({ Test-Path $_ -IsValid })]
[string]$OutputFile
)

begin {
Expand Down Expand Up @@ -441,6 +445,11 @@ function Invoke-Sqlcmd2 {

}
process {

if(-Not([string]::isNullOrEmpty($OutputFile)) -and $MessagesToOutput) {
throw "Parameters OutputFile and MessagesToOutput are mutually exclusive"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most probably it can be achieved by using parameter sets

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not used to parameter sets, but does this means that there should be 4 parameter sets for the function?
Con-Que
Con-Fil
Con-Que-Output
Con-Fil-Output
?

In that case, isn't it too heavy, just for adding one parameter?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

take a look to the current param sets: [Parameter(ParameterSetName = 'Ins-Que'

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's too much code addition and duplication (the param zone would double) for just adding one parameter while a 3 line code works well.

}

foreach ($SQLInstance in $ServerInstance) {
Write-Debug -Message "Querying ServerInstance '$SQLInstance'"

Expand Down Expand Up @@ -574,7 +583,12 @@ function Invoke-Sqlcmd2 {
$pool.Dispose()
}
else {
#Following EventHandler is used for PRINT and RAISERROR T-SQL statements. Executed when -Verbose parameter specified by caller and no -MessageToOutput
if($OutputFile) {
$conn.FireInfoMessageEventOnUserErrors=$false # Shiyang, $true will change the SQL exception to information
$OutToFileHandler = [System.Data.SqlClient.SqlInfoMessageEventHandler] { Out-File -FilePath $OutputFile -InputObject "$($_)" -Append }
$conn.add_InfoMessage($OutToFileHandler)
}
#Following EventHandler is used for PRINT and RAISERROR T-SQL statements. Executed when -Verbose parameter specified by caller and no -MessagesToOutput
if ($PSBoundParameters.Verbose) {
$conn.FireInfoMessageEventOnUserErrors = $false
$handler = [System.Data.SqlClient.SqlInfoMessageEventHandler] { Write-Verbose "$($_)" }
Expand All @@ -590,6 +604,10 @@ function Invoke-Sqlcmd2 {
if ($PSBoundParameters.Verbose) {
$conn.remove_InfoMessage($handler)
}
if($OutToFileHandler -ne $null) {
$conn.remove_InfoMessage($OutToFileHandler)
}

}
Resolve-SqlError $Err
}
Expand Down Expand Up @@ -640,4 +658,4 @@ function Invoke-Sqlcmd2 {
} #foreach ($piece in $Pieces)
}
}
} #Invoke-Sqlcmd2
} #Invoke-Sqlcmd2