|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Opens a connection to a MSSQL server |
| 4 | +.DESCRIPTION |
| 5 | + This Cmdlet will open a connection to a MSSQL server |
| 6 | + and returns that connection object. |
| 7 | +.FUNCTIONALITY |
| 8 | + Opens a connection to a MSSQL server |
| 9 | +.EXAMPLE |
| 10 | + PS>Open-IcingaMSSQLConnection -IntegratedSecurity -Address localhost; |
| 11 | +.EXAMPLE |
| 12 | + PS>Open-IcingaMSSQLConnection -Username Exampleuser -Password (ConvertTo-IcingaSecureString 'examplePassword') -Address 123.125.123.2; |
| 13 | +.PARAMETER Username |
| 14 | + The username for connecting to the MSSQL database |
| 15 | +.PARAMETER Password |
| 16 | + The password for connecting to the MSSQL database as secure string |
| 17 | +.PARAMETER Address |
| 18 | + The IP address or FQDN to the MSSQL server to connect to (default: localhost) |
| 19 | +.PARAMETER Port |
| 20 | + The port of the MSSQL server/instance to connect to with the provided credentials (default: 1433) |
| 21 | +.PARAMETER SqlDatabase |
| 22 | + The name of a specific database to connect to. Leave empty to connect "globaly" |
| 23 | +.PARAMETER IntegratedSecurity |
| 24 | + Allows this plugin to use the credentials of the current PowerShell session inherited by |
| 25 | + the user the PowerShell is running with. If this is set and the user the PowerShell is |
| 26 | + running with can access to the MSSQL database you will not require to provide username |
| 27 | + and password |
| 28 | +.OUTPUTS |
| 29 | + System.Data.SqlClient.SqlConnection |
| 30 | +.LINK |
| 31 | + https://github.com/Icinga/icinga-powershell-framework |
| 32 | +#> |
| 33 | +function Open-IcingaMSSQLConnection() |
| 34 | +{ |
| 35 | + param ( |
| 36 | + [string]$Username, |
| 37 | + [securestring]$Password, |
| 38 | + [string]$Address = "localhost", |
| 39 | + [int]$Port = 1433, |
| 40 | + [string]$SqlDatabase, |
| 41 | + [switch]$IntegratedSecurity = $FALSE |
| 42 | + ); |
| 43 | + |
| 44 | + if ($IntegratedSecurity -eq $FALSE) { |
| 45 | + if ([string]::IsNullOrEmpty($Username)) { |
| 46 | + Exit-IcingaThrowException ` |
| 47 | + -ExceptionType 'Input' ` |
| 48 | + -ExceptionThrown $IcingaExceptions.Inputs.MSSQLCredentialHandling ` |
| 49 | + -CustomMessage '-Username not set and -IntegratedSecurity is false' ` |
| 50 | + -Force; |
| 51 | + } elseif ($null -eq $Password) { |
| 52 | + Exit-IcingaThrowException ` |
| 53 | + -ExceptionType 'Input' ` |
| 54 | + -ExceptionThrown $IcingaExceptions.Inputs.MSSQLCredentialHandling ` |
| 55 | + -CustomMessage '-Password not set and -IntegratedSecurity is false' ` |
| 56 | + -Force; |
| 57 | + } |
| 58 | + |
| 59 | + $Password.MakeReadOnly(); |
| 60 | + $SqlCredential = New-Object System.Data.SqlClient.SqlCredential($Username, $Password); |
| 61 | + } |
| 62 | + |
| 63 | + try { |
| 64 | + $SqlConnection = New-Object System.Data.SqlClient.SqlConnection; |
| 65 | + $SqlConnection.ConnectionString = "Server=$Address,$Port;"; |
| 66 | + |
| 67 | + if ($null -ne $SqlDatabase) { |
| 68 | + $SqlConnection.ConnectionString += "Database=$SqlDatabase;"; |
| 69 | + } |
| 70 | + |
| 71 | + if ($IntegratedSecurity -eq $TRUE) { |
| 72 | + $SqlConnection.ConnectionString += "Integrated Security=True;"; |
| 73 | + } |
| 74 | + |
| 75 | + $SqlConnection.Credential = $SqlCredential; |
| 76 | + |
| 77 | + Write-IcingaDebugMessage ` |
| 78 | + -Message 'Open client connection for endpoint {0}' ` |
| 79 | + -Objects $SqlConnection; |
| 80 | + |
| 81 | + $SqlConnection.Open(); |
| 82 | + } catch { |
| 83 | + Exit-IcingaThrowException ` |
| 84 | + -InputString $_.Exception.Message ` |
| 85 | + -StringPattern $Username ` |
| 86 | + -ExceptionType 'Input' ` |
| 87 | + -ExceptionThrown $IcingaExceptions.Inputs.MSSQLCredentialHandling; |
| 88 | + |
| 89 | + Exit-IcingaThrowException ` |
| 90 | + -InputString $_.Exception.Message ` |
| 91 | + -StringPattern 'error: 40' ` |
| 92 | + -ExceptionType 'Connection' ` |
| 93 | + -ExceptionThrown $IcingaExceptions.Connection.MSSQLConnectionError; |
| 94 | + |
| 95 | + Exit-IcingaThrowException ` |
| 96 | + -InputString $_.Exception.Message ` |
| 97 | + -StringPattern 'error: 0' ` |
| 98 | + -ExceptionType 'Connection' ` |
| 99 | + -ExceptionThrown $IcingaExceptions.Connection.MSSQLConnectionError; |
| 100 | + |
| 101 | + Exit-IcingaThrowException ` |
| 102 | + -InputString $_.Exception.Message ` |
| 103 | + -StringPattern 'error: 25' ` |
| 104 | + -ExceptionType 'Connection' ` |
| 105 | + -ExceptionThrown $IcingaExceptions.Connection.MSSQLConnectionError; |
| 106 | + # Last resort |
| 107 | + Exit-IcingaThrowException ` |
| 108 | + -InputString $_.Exception.Message ` |
| 109 | + -ExceptionType 'Custom' ` |
| 110 | + -Force; |
| 111 | + } |
| 112 | + |
| 113 | + return $SqlConnection; |
| 114 | +} |
0 commit comments