Function Start-SQLDB-Query-withLogging { <# .SYNOPSIS Function will send a SQL Query to a SQL DB. .DESCRIPTION Function will send a SQL Query to a SQL DB. If you set a read command (SELECT ...), function will return a dataset. .REQUIREMENT General PowerShell V2 and Function "Write-Logfile". .REQUIREMENT Variables DBSQLQuery, DBSQLQueryType, DBSQLConnectServer, DBSQLConnectUser, DBSQLConnectPassword, DBSQLConnectDatabase, DBSQLConnectIntegratedSecurity, DBSQLCommand, DBSQLAdapter, DBSQLConnection .REQUIREMENT Functions Write-Logfile .VERSION Number: 1.0.1.0 / Date: 24.09.2017 .PARAMETER DBSQLQuery Give a Standard T-SQL Query .PARAMETER DBSQLQueryType Give read or write, depends on your planned action ("select" is only read, "insert" and "update" are write) .EXAMPLE Func-DB-SQL-Query -DBSQLQueryType read -DBSQLQuery "SELECT * from TABLE;" .EXAMPLE Func-DB-SQL-Query -DBSQLQueryType write -DBSQLQuery "INSERT INTO TABLE (column1,column2,column3) VALUES ('Text1', Zahl1, Zahl2) WHERE column4 = 'Text2';" .EXAMPLE Func-DB-SQL-Query -DBSQLQueryType write -DBSQLQuery "UPDATE TABLE SET column1='Text1',column2=Zahl1,column3=Zahl2 WHERE column4 = 'Text2';" #> Param ( [Parameter(Position=0,Mandatory=$False,HelpMessage='Give the SQL Servername or Server name incl. Instance name')] [ValidateNotNullOrEmpty()] [STRING]$DBSQLConnectServer=(Get-Variable -Name DBSQLConnectServer -Scope Global -ValueOnly -ErrorAction SilentlyContinue), [Parameter(Position=1,Mandatory=$False,HelpMessage='Give the database user name')] [ValidateNotNullOrEmpty()] [STRING]$DBSQLConnectUser=(Get-Variable -Name DBSQLConnectUser -Scope Global -ValueOnly -ErrorAction SilentlyContinue), [Parameter(Position=2,Mandatory=$False,HelpMessage='Give the database user password')] [ValidateNotNullOrEmpty()] [STRING]$DBSQLConnectPassword=(Get-Variable -Name DBSQLConnectPassword -Scope Global -ValueOnly -ErrorAction SilentlyContinue), [Parameter(Position=3,Mandatory=$False,HelpMessage='Give the database name')] [ValidateNotNullOrEmpty()] [STRING]$DBSQLConnectDatabase=(Get-Variable -Name DBSQLConnectDatabase -Scope Global -ValueOnly -ErrorAction SilentlyContinue), [Parameter(Position=4,Mandatory=$False,HelpMessage='Determ if current login credentials should be used for database login ()')] [ValidateNotNullOrEmpty()] [STRING]$DBSQLConnectIntegratedSecurity=(Get-Variable -Name DBSQLConnectIntegratedSecurity -Scope Global -ValueOnly -ErrorAction SilentlyContinue), [Parameter(Position=5,Mandatory=$True,HelpMessage='Specify the SQL Query')] [ValidateNotNullOrEmpty()] [STRING]$DBSQLQuery, [Parameter(Position=6,Mandatory=$False,HelpMessage='Determ if Query was for read or write operations')] [ValidateSet("read","write")] [STRING]$DBSQLQueryType='read', [Parameter(Position=7,Mandatory=$False,HelpMessage=')')] [ValidateNotNullOrEmpty()] [STRING]$DBSQLErrorAction='continue' ) #end param ValueFromPipeline=$True IF (Get-Module -Name "Write-LogFile") { Write-Logfile -LogLine "" Write-Logfile -LogLine "Connecting with DB:" Write-Logfile -LogLine "Server = $DBSQLConnectServer, uid=$DBSQLConnectUser, pwd=, Database = $DBSQLConnectDatabase, Integrated Security = $DBSQLConnectIntegratedSecurity" Try { $DBSQLConnection = New-Object System.Data.SqlClient.SqlConnection $DBSQLConnection.ConnectionString = "Server = $DBSQLConnectServer; uid=$DBSQLConnectUser; pwd=$DBSQLConnectPassword; Database = $DBSQLConnectDatabase; Integrated Security = $DBSQLConnectIntegratedSecurity" Write-Logfile -LogLine "Processing SQL Query:" Write-Logfile -LogLine $DBSQLQuery IF ($DBSQLQueryType -eq 'read') { Try { $DBSQLCommand = New-Object System.Data.SqlClient.SqlCommand $DBSQLCommand.CommandText = $DBSQLQuery $DBSQLCommand.Connection = $DBSQLConnection $DBSQLAdapter = New-Object System.Data.SqlClient.SqlDataAdapter $DBSQLAdapter.SelectCommand = $DBSQLCommand $DBSQLDataSet = New-Object System.Data.DataSet $DBSQLConnection.Open() $DBSQLAdapter.Fill($DBSQLDataSet) $DBSQLConnection.Close() Write-Logfile -Logline "SQL Query result with $($DBSQLDataSet.Tables[0].Rows.Count) row(s)." #$DBSQLDataSet.Tables[0].Rows | Out-GridView Return $DBSQLDataSet } #end try Catch { Write-Logfile -LogLine "ERROR: Unable to process SQL Query (read)!" IF ($ErrorAction -eq 'continue') { Write-Logfile -LogLine "Program will continue, anyway." } #end if ELSEIF ($ErrorAction -eq 'stop') { Write-Logfile -LogLine "Program will stop." exit } #end elseif ELSE { Write-Logfile -LogLine "Invalid Value for Parameter DBSQLErrorAction detected." Write-Logfile -LogLine "Allowed Values are 'continue' or 'stop'." Write-Logfile -LogLine "Program will continue, anyway." } #end else } #end catch } #end if ELSEIF ($DBSQLQueryType -eq 'write') { Try { $DBSQLCommand = New-Object System.Data.SqlClient.SqlCommand $DBSQLCommand.CommandText = $DBSQLQuery $DBSQLCommand.Connection = $DBSQLConnection $DBSQLConnection.Open() $DBSQLCommand.ExecuteNonQuery() $DBSQLConnection.Close() } #end try Catch { Write-Logfile -LogLine "ERROR: Unable to process SQL Query (write)!" IF ($ErrorAction -eq 'continue') { Write-Logfile -LogLine "Program will continue, anyway." } #end if ELSEIF ($ErrorAction -eq 'stop') { Write-Logfile -LogLine "Program will stop." exit } #end elseif ELSE { Write-Logfile -LogLine "Invalid Value for parameter DBSQLErrorAction detected." Write-Logfile -LogLine "Allowed Values are 'continue' or 'stop'." Write-Logfile -LogLine "Program will continue, anyway." } #end else } #end catch } #end elseif ELSE { Write-Logfile -LogLine "ERROR: Invalid parameter for function!" } #end else } #end try Catch { Write-Logfile -LogLine "ERROR: Unable to etablish a DB Connection!" } #end catch } #end if ELSE { Write-Host "" Write-Host "DEBUG Info: Write-LogFile - Module does not exist!" Write-Host "DEBUG Info: Please load the Module and try again, running this Function/Module!" Write-Host "DEBUG Info: Exiting, because of this Issue." EXIT } #end else } #end function