{"id":380,"date":"2015-07-01T16:12:58","date_gmt":"2015-07-01T15:12:58","guid":{"rendered":"http:\/\/snakenet.eu\/blog\/?p=380"},"modified":"2016-01-29T12:11:53","modified_gmt":"2016-01-29T11:11:53","slug":"interroger-une-db-oraclesql-avec-powershell","status":"publish","type":"post","link":"https:\/\/snakenet.eu\/blog\/interroger-une-db-oraclesql-avec-powershell\/380\/","title":{"rendered":"Interroger une DB Oracle\/SQL avec Powershell"},"content":{"rendered":"<p>Vous souhaitez interroger vos DBs mais les m?thodes fournies sont <del>chiantes <\/del>peu ergonomiques?<br \/>\nFacile! Voici 2 fonctions qui font le boulot.<br \/>\nRemarque utile : pas besoin d&rsquo;installer un quelconque client! C&rsquo;est natif !<\/p>\n<h1>SQL<\/h1>\n<p><code>Function querySQL ( $query, $database='master', $Server) {<br \/>\n\t$connection = New-Object System.Data.SqlClient.SqlConnection;<br \/>\n\t$connection.ConnectionString = \"Server=$Server;Database=$database;Integrated Security=True;Connect Timeout=$timeOut;\";<br \/>\n \ttry {<br \/>\n\t\t$connection.Open();<br \/>\n\t\t$command = $connection.CreateCommand();<br \/>\n\t\t$command.CommandText  = $query;<br \/>\n \t\ttry{<br \/>\n\t\t\t$result = $command.ExecuteReader();<br \/>\n\t\t\t$SQLData = new-object System.Data.DataTable;<\/p>\n<p>\t\t\t$SQLData.Load($result);<br \/>\n\t\t\t$connection.Close();<br \/>\n\t\t\tRemove-Variable connection, command, result<br \/>\n\t\t\t$SQLData<br \/>\n\t\t}<br \/>\n\t\tcatch{Write-Host \"Oups: Cannot query\" -foregroundcolor Magenta}<br \/>\n\t}<br \/>\n\tcatch{Write-Host \"Oups: Connot open : Do you have the right to connect to this DB?\" -foregroundcolor Magenta}<br \/>\n}<br \/>\n<\/code><\/p>\n<h1>Oracle<\/h1>\n<p><code>function queryOracle ( $TNSName, $Query, $Username, $Password) {<br \/>\n  [System.Reflection.Assembly]::LoadWithPartialName(\"System.Data.OracleClient\") | out-null<\/p>\n<p>  $OracleConnection = New-Object System.Data.OracleClient.OracleConnection(\"Data Source=$TNSName;User Id=$Username;Password=$Password;Integrated Security=no\");<br \/>\n  $OracleConnection.Open() <\/p>\n<p>  $SelectCommand = New-Object System.Data.OracleClient.OracleCommand;<br \/>\n  $SelectCommand.Connection = $OracleConnection<br \/>\n  $SelectCommand.CommandText = $Query<br \/>\n  $SelectCommand.CommandType = [System.Data.CommandType]::Text <\/p>\n<p>  $result = New-Object System.Data.DataTable<br \/>\n  $result.Load($SelectCommand.ExecuteReader()) <\/p>\n<p>  $reader = $SelectCommand.ExecuteReader()<br \/>\n  while ($reader.Read()) {<br \/>\n    $obj = New-Object PSObject<br \/>\n    0..($reader.FieldCount-1) | % {<br \/>\n      $obj | Add-Member -MemberType NoteProperty -Name ($reader.GetName($_) -replace \"\\W\", \"\") -Value ($reader.GetValue($_))<br \/>\n    }<br \/>\n    $obj<br \/>\n  }<br \/>\n}<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Vous souhaitez interroger vos DBs mais les m?thodes fournies sont chiantes peu ergonomiques? Facile! Voici 2 fonctions qui font le boulot. Remarque utile : pas besoin d&rsquo;installer un quelconque client! C&rsquo;est natif ! SQL Function querySQL ( $query, $database=&rsquo;master&rsquo;, $Server) { $connection = New-Object System.Data.SqlClient.SqlConnection; $connection.ConnectionString = \u00ab\u00a0Server=$Server;Database=$database;Integrated Security=True;Connect Timeout=$timeOut;\u00a0\u00bb; try { $connection.Open(); $command = [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62,58],"tags":[],"class_list":["post-380","post","type-post","status-publish","format-standard","hentry","category-oracle","category-powershell"],"_links":{"self":[{"href":"https:\/\/snakenet.eu\/blog\/wp-json\/wp\/v2\/posts\/380","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/snakenet.eu\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/snakenet.eu\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/snakenet.eu\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/snakenet.eu\/blog\/wp-json\/wp\/v2\/comments?post=380"}],"version-history":[{"count":2,"href":"https:\/\/snakenet.eu\/blog\/wp-json\/wp\/v2\/posts\/380\/revisions"}],"predecessor-version":[{"id":382,"href":"https:\/\/snakenet.eu\/blog\/wp-json\/wp\/v2\/posts\/380\/revisions\/382"}],"wp:attachment":[{"href":"https:\/\/snakenet.eu\/blog\/wp-json\/wp\/v2\/media?parent=380"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/snakenet.eu\/blog\/wp-json\/wp\/v2\/categories?post=380"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/snakenet.eu\/blog\/wp-json\/wp\/v2\/tags?post=380"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}