2atSql.psm1
#Requires -Version 4.0 -Modules 2atGeneral $PSDefaultParameterValues.Clear() Set-StrictMode -Version 2.0 Import-Module 2atGeneral Function execSql { [CmdletBinding()] param ( [string] $ConnectionString, [Parameter(ValueFromPipeline)] [System.Data.SqlClient.SqlConnection] $SqlConnection, [Parameter(Position=0)] [ValidateNotNullorEmpty()] [string] $CommandText, [bool] $CommandIsQuery, [Parameter(Position=1)] [hashtable] $Parameters=@{}, [scriptblock] $Script, [int] $Timeout ) Write-Verbose "Executing SQL: $CommandText" if ($SqlConnection -and ![string]::IsNullOrEmpty($ConnectionString)) { Write-Warning "Both SqlConnection and ConnectionString set. Using SqlConnection and ignoring ConnectionString value." } if (!$SqlConnection) { if ([string]::IsNullOrEmpty($ConnectionString)) { throw "Either supply a SqlConnection object or a ConnectionString" } $SqlConnection = New-Object System.Data.SqlClient.SqlConnection($ConnectionString) $disposeConnection = $true } $SqlCmd = $SqlConnection.CreateCommand() $SqlCmd.CommandText = $CommandText if ($Timeout -ne -1) { $SqlCmd.CommandTimeout = $Timeout } if (!$CommandIsQuery) { $SqlCmd.CommandType = [System.Data.CommandType]'StoredProcedure' } foreach($p in $parameters.Keys) { if ($parameters[$p] -ne $null) { $v = $Parameters[$p] } else { $v = [DBNull]::Value } Write-Verbose " $p = $v" [void]$SqlCmd.Parameters.AddWithValue("@$p",$v) } if ($SqlConnection.State -eq [System.Data.ConnectionState]::Closed) { $SqlConnection.Open() } $script.Invoke($SqlCmd) $SqlCmd.Dispose() if ($disposeConnection) { $SqlConnection.Dispose() } } Function Get-SqlData { [CmdletBinding()] param ( [string] $ConnectionString, [Parameter(ValueFromPipeline)] [System.Data.SqlClient.SqlConnection] $SqlConnection, [Parameter(Position=0)] [ValidateNotNullorEmpty()] [string] $CommandText, [switch] $CommandIsQuery, [Parameter(Position=1)] [hashtable] $Parameters=@{}, [int] $Timeout=-1 ) Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState execSql -ConnectionString $ConnectionString -SqlConnection $SqlConnection -CommandText $CommandText -CommandIsQuery $CommandIsQuery -Timeout $Timeout -Parameters $Parameters -Script { param ( [System.Data.SqlClient.SqlCommand]$SqlCmd ) $dt=New-Object system.Data.DataTable [void](New-Object system.Data.SqlClient.SqlDataAdapter($SqlCmd)).fill($dt) $dt } } Function Set-SqlData { [CmdletBinding()] param ( [string] $ConnectionString, [Parameter(ValueFromPipeline)] [System.Data.SqlClient.SqlConnection] $SqlConnection, [Parameter(Position=0)] [ValidateNotNullorEmpty()] [string] $CommandText, [switch] $CommandIsQuery, [Parameter(Position=1)] [hashtable] $Parameters=@{}, [int] $Timeout=-1 ) Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState execSql -ConnectionString $ConnectionString -SqlConnection $SqlConnection -CommandText $CommandText -CommandIsQuery $CommandIsQuery -Timeout $Timeout -Parameters $Parameters -Script { param ( [System.Data.SqlClient.SqlCommand]$SqlCmd ) [void]$SqlCmd.ExecuteNonQuery() } } Export-ModuleMember -Function Get-* Export-ModuleMember -Function Set-* # SIG # Begin signature block # MIIWcAYJKoZIhvcNAQcCoIIWYTCCFl0CAQExDzANBglghkgBZQMEAgEFADB5Bgor # BgEEAYI3AgEEoGswaTA0BgorBgEEAYI3AgEeMCYCAwEAAAQQH8w7YFlLCE63JNLG # KX7zUQIBAAIBAAIBAAIBAAIBADAxMA0GCWCGSAFlAwQCAQUABCAQAHPEHsFva1X7 # Cf/iUGoZN0F5e3uk25XNDyWtlZEqOKCCCxswggUzMIIEG6ADAgECAhEAgNHe/U3D # BzyckFGAgIDcJDANBgkqhkiG9w0BAQsFADB9MQswCQYDVQQGEwJHQjEbMBkGA1UE # CBMSR3JlYXRlciBNYW5jaGVzdGVyMRAwDgYDVQQHEwdTYWxmb3JkMRowGAYDVQQK # ExFDT01PRE8gQ0EgTGltaXRlZDEjMCEGA1UEAxMaQ09NT0RPIFJTQSBDb2RlIFNp # Z25pbmcgQ0EwHhcNMTcwMTEzMDAwMDA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