Advertisement
Guest User

Untitled

a guest
Mar 9th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. Param
  2. (
  3.  
  4. [string]$ServerHostName,
  5. [int]$ServerPort = 5432,
  6. [string]$User,
  7. [string]$Password,
  8. [string]$DBName,
  9. [string]$SQLFilePath = "",
  10. [string]$SQLCmd = ""
  11. )
  12.  
  13. # Load ODBC Driver
  14. [Reflection.Assembly]::LoadFrom('C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Npgsql\v4.0_3.2.1.0__5d8b90d52f46fda7\Npgsql.dll ') > $null
  15.  
  16. # Function to execute command
  17. function ExecuteSQL ($SQLCmd,$ConnectionString)
  18. {
  19. $DBConn = New-Object Npgsql.NpgsqlConnection
  20. $DBConn.ConnectionString = $ConnectionString;
  21. $DBConn.Open();
  22. $DBCmd = $DBConn.CreateCommand();
  23. $DBCmd.CommandText = $SQLCmd
  24. $DBCmd.ExecuteReader();
  25. $DBConn.Close();
  26. }
  27.  
  28. # set connection string
  29. $ConnectionString = "Host=$ServerHostName;Database=$DBName;Username=$User;Password=$Password;Command Timeout=300;Keepalive=30"
  30.  
  31. # if a command sent
  32. if($SQLCmd -ne ""){
  33.  
  34. ExecuteSQL -SQLCmd $SQLCmd -ConnectionString $ConnectionString
  35.  
  36. # if a file path
  37. }else{
  38.  
  39. ExecuteSQL -SQLCmd (Get-Content ($SQLFilePath)) -ConnectionString $ConnectionString
  40.  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement