{"id":976,"date":"2017-05-17T17:22:18","date_gmt":"2017-05-17T15:22:18","guid":{"rendered":"http:\/\/dety.net.ua\/?p=976"},"modified":"2017-05-17T17:22:18","modified_gmt":"2017-05-17T15:22:18","slug":"powershell-mysql","status":"publish","type":"post","link":"https:\/\/dety.net.ua\/?p=976","title":{"rendered":"Powershell + MySQL"},"content":{"rendered":"<p>Interesting script and MySQL connector for powershell:<\/p>\n<p>Script:<\/p>\n<pre class=\"code\">Param(\r\n  [Parameter(\r\n  Mandatory = $true,\r\n  ParameterSetName = '',\r\n  ValueFromPipeline = $true)]\r\n  [string]$Query\r\n  )\r\n\r\n$MySQLAdminUserName = 'username'\r\n$MySQLAdminPassword = 'password'\r\n$MySQLDatabase = 'MySQL-DB'\r\n$MySQLHost = 'MySQL-Host'\r\n$ConnectionString = \"server=\" + $MySQLHost + \";port=3306;uid=\" + $MySQLAdminUserName + \";pwd=\" + $MySQLAdminPassword + \";database=\"+$MySQLDatabase\r\n\r\nTry {\r\n  [void][System.Reflection.Assembly]::LoadWithPartialName(\"MySql.Data\")\r\n  $Connection = New-Object MySql.Data.MySqlClient.MySqlConnection\r\n  $Connection.ConnectionString = $ConnectionString\r\n  $Connection.Open()\r\n\r\n  $Command = New-Object MySql.Data.MySqlClient.MySqlCommand($Query, $Connection)\r\n  $DataAdapter = New-Object MySql.Data.MySqlClient.MySqlDataAdapter($Command)\r\n  $DataSet = New-Object System.Data.DataSet\r\n  $RecordCount = $dataAdapter.Fill($dataSet, \"data\")\r\n  $DataSet.Tables[0]\r\n  }\r\n\r\nCatch {\r\n  Write-Host \"ERROR : Unable to run query : $query `n$Error[0]\"\r\n }\r\n\r\nFinally {\r\n  $Connection.Close()\r\n  }\r\n\r\nThe connector: <a href=\"http:\/\/dev.mysql.com\/downloads\/connector\/net\/\">http:\/\/dev.mysql.com\/downloads\/connector\/net\/\r\n\r\n<\/a>The source: <a href=\"https:\/\/www.cogmotive.com\/blog\/powershell\/querying-mysql-from-powershell\">https:\/\/www.cogmotive.com\/blog\/powershell\/querying-mysql-from-powershell<\/a><\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Interesting script and MySQL connector for powershell: Script: Param( [Parameter( Mandatory = $true, ParameterSetName = &#8221;, ValueFromPipeline = $true)] [string]$Query ) $MySQLAdminUserName = &#8216;username&#8217; $MySQLAdminPassword = &#8216;password&#8217; $MySQLDatabase = &#8216;MySQL-DB&#8217; $MySQLHost = &#8216;MySQL-Host&#8217; $ConnectionString = &#8220;server=&#8221; + $MySQLHost + &#8220;;port=3306;uid=&#8221; + $MySQLAdminUserName + &#8220;;pwd=&#8221; + $MySQLAdminPassword + &#8220;;database=&#8221;+$MySQLDatabase Try { [void][System.Reflection.Assembly]::LoadWithPartialName(&#8220;MySql.Data&#8221;) $Connection = New-Object MySql.Data.MySqlClient.MySqlConnection [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,18],"tags":[],"class_list":["post-976","post","type-post","status-publish","format-standard","hentry","category-novosti","category-powershell"],"_links":{"self":[{"href":"https:\/\/dety.net.ua\/index.php?rest_route=\/wp\/v2\/posts\/976","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dety.net.ua\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dety.net.ua\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dety.net.ua\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dety.net.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=976"}],"version-history":[{"count":1,"href":"https:\/\/dety.net.ua\/index.php?rest_route=\/wp\/v2\/posts\/976\/revisions"}],"predecessor-version":[{"id":977,"href":"https:\/\/dety.net.ua\/index.php?rest_route=\/wp\/v2\/posts\/976\/revisions\/977"}],"wp:attachment":[{"href":"https:\/\/dety.net.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=976"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dety.net.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=976"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dety.net.ua\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=976"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}