{"id":2461,"date":"2018-04-25T15:22:32","date_gmt":"2018-04-25T13:22:32","guid":{"rendered":"http:\/\/www.mariotti.de\/?p=2461"},"modified":"2018-04-25T15:22:32","modified_gmt":"2018-04-25T13:22:32","slug":"simple-sql-abfragen-in-powershell","status":"publish","type":"post","link":"https:\/\/dev.mariotti.de\/?p=2461","title":{"rendered":"Simple SQL Abfragen in PowerShell"},"content":{"rendered":"<p>Mit Hilfe dieser kleine PowerShell Funktion, die die SQL Client Funktionalit\u00e4t des .NET Frameworks nutzt, lassen sich in PowerShell einfache SQL Abfragen bewerkstelligen, ohne das z.B. das SQL Server Modul ben\u00f6tigt wird<\/p>\n<pre><code class=\"language-powershell\">function Invoke-SQL {\n    Param(\n        [string]$DataSource,\n        [string]$Database,\n        [string]$SqlCommand,\n        [string]$User,\n        [string]$Password\n    )\n    if (($User) -and ($Password)) {\n        $connectionString = \"Data Source=$dataSource;User ID=$User;Password=$Password;Initial Catalog=$Database\"\n    } else { \n        $connectionString = \"Data Source=$dataSource; \" + \"Integrated Security=SSPI; \" + \"Initial Catalog=$database\"\n     }\n    $connection = new-object system.data.SqlClient.SQLConnection($connectionString)\n    $command = new-object system.data.sqlclient.sqlcommand($sqlCommand,$connection)\n    $connection.Open()\n    $adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command\n    $dataset = New-Object System.Data.DataSet\n    $adapter.Fill($dataSet) | Out-Null\n    $connection.Close()\n    $dataSet.Tables\n}\n# Example usage (sql auth):\nInvoke-SQL -dataSource 192.168.178.100 -database Master -user sa -password Geheim -sqlCommand 'Select * from sysdatabases'\n\n# Example usage (Integrated auth):\nInvoke-SQL -dataSource 192.168.178.100 -database Master -sqlCommand 'Select * from sysdatabases'<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Ich zeige euch meine Invoke-SQL Funktion mit der ihr einfache SQL Abfragen aus PowerShell Heraus bewerkstelligen k\u00f6nnt.<\/p>\n","protected":false},"author":2,"featured_media":2425,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[],"class_list":["post-2461","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"_links":{"self":[{"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=\/wp\/v2\/posts\/2461","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2461"}],"version-history":[{"count":0,"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=\/wp\/v2\/posts\/2461\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=\/wp\/v2\/media\/2425"}],"wp:attachment":[{"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}