<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title> &#187; mysql.net</title>
	<atom:link href="http://www.jaredperry.ca/tag/mysql-net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jaredperry.ca</link>
	<description>Entrepreneur. Programmer. Gamer.</description>
	<lastBuildDate>Wed, 05 Oct 2011 00:39:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Powershell and MySQL can be Friends</title>
		<link>http://www.jaredperry.ca/2009/06/powershell-and-mysql/</link>
		<comments>http://www.jaredperry.ca/2009/06/powershell-and-mysql/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 18:12:09 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[database]]></category>
		<category><![CDATA[friends]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[mysql.net]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[servers]]></category>
		<category><![CDATA[wmi]]></category>

		<guid isPermaLink="false">http://www.jaredperry.ca/?p=27</guid>
		<description><![CDATA[While using Powershell to collect data from a group of windows servers, I realized I would need a database to store the information collected. I was happy to learn that Powershell can utilize the MySQL.NET connector, so I could install MySQL on a server and have Powershell funnel the data with standard SQL commands into [...]]]></description>
			<content:encoded><![CDATA[<p>While using Powershell to collect data from a group of windows servers, I realized I would need a database to store the information collected. I was happy to learn that Powershell can utilize the MySQL.NET connector, so I could install MySQL on a server and have Powershell funnel the data with standard SQL commands into the database. All that I needed to do was install the .NET connector for MySQL on any machine I wished to connect to the database, in my case I installed it on all my servers. If you are worried about installing this connector, it has a small footprint and is nothing like some other database connectors like Oracles bulky ODBC driver. I have compiled a step by step list of how to configure and use MySQL within Powershell:</p>
<ol>
<li>Install MySQL.NET 5.0.9, there is newer versions, but I choose 5.0.9 because it works with all my scripts.
<ul>
<li>http://dev.mysql.com/downloads/connector/net/5.0.html</li>
</ul>
</li>
<li>If you are running a Powershell script as a task, you will need to change the execution policy in Powershell
<ul>
<li>Set-ExecutionPolicy unrestricted</li>
</ul>
</li>
<li>On servers if you want to run a Powershell script from a network share without prompt, you will need to add the share to Internet Explorers trusted sites list, apparently IE policy effects file share script execution</li>
<li>You will need to now load the connector into your powershell script, first is for 32-bit windows, the second is for 64-bit version of windows
<ul>
<li>[void][system.reflection.Assembly]::LoadFrom(&#8220;C:\Program Files\MySQL\MySQL Connector Net 5.0.9\Binaries\.NET 2.0\MySQL.Data.dll&#8221;)</li>
</ul>
<ul>
<li>[void][system.reflection.Assembly]::LoadFrom(&#8220;C:\Program Files (x86)\MySQL\MySQL Connector Net 5.0.9\Binaries\.NET 2.0\MySQL.Data.dll&#8221;)</li>
</ul>
</li>
<li>Now we will want to connect to our database
<ul>
<li>$dbconnect = New-Object MySql.Data.MySqlClient.MySqlConnection</li>
<li>$dbconnect.ConnectionString = &#8220;server=servername;user id=dbusername;password=dbpassword;database=dbname;pooling=false&#8221;</li>
<li>$dbconnect.Open()</li>
</ul>
</li>
<li>Once we have connected, we can now create a MySQL command object, below is an example SQL command
<ul>
<li>$sql = New-Object MySql.Data.MySqlClient.MySqlCommand</li>
<li>$sql.Connection = $dbconnect</li>
<li>$sql.CommandText = &#8220;INSERT INTO computer_details (computer_id, mac, dhcp, model, domain, manufacturer, type, memory, ip, servicetag, lastimagedate, servicepack, os, biosrev, scriptversion, lastrun, ou) VALUES (&#8216;$resultID&#8217;, &#8216;$macAddress&#8217;, &#8216;$dhcp&#8217;, &#8216;$model&#8217;, &#8216;$domain&#8217;, &#8216;$manufacturer&#8217;, &#8216;$systemType&#8217;, &#8216;$memory&#8217;, &#8216;$ipAddress&#8217;, &#8216;$servicetag&#8217;, NOW(), &#8216;$servicePack&#8217;, &#8216;$operatingSystem&#8217;, &#8216;$biosrev&#8217;, &#8216;$version&#8217;, NOW(), &#8216;$ou&#8217; )&#8221;</li>
<li>$sql.ExecuteNonQuery()</li>
<li>$dbconnect.Close()</li>
</ul>
</li>
</ol>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 399px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">$sq= New-Object MySql.Data.MySqlClient.MySqlCommand</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 399px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>$sql.Connection = $dbconnect</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 399px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">#Insert our computer details using the above id and all the data from the WMI objects</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 399px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>$sql.CommandText = &#8220;INSERT INTO computer_details (computer_id, mac, dhcp, model, domain, manufacturer, type, memory, ip, servicetag, lastimagedate, servicepack, os, biosrev, scriptversion, lastrun, ou) VALUES (&#8216;$resultID&#8217;, &#8216;$macAddress&#8217;, &#8216;$dhcp&#8217;, &#8216;$model&#8217;, &#8216;$domain&#8217;, &#8216;$manufacturer&#8217;, &#8216;$systemType&#8217;, &#8216;$memory&#8217;, &#8216;$ipAddress&#8217;, &#8216;$servicetag&#8217;, NOW(), &#8216;$servicePack&#8217;, &#8216;$operatingSystem&#8217;, &#8216;$biosrev&#8217;, &#8216;$version&#8217;, NOW(), &#8216;$ou&#8217; )&#8221;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 399px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>$sql.ExecuteNonQuery()</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 399px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">#Here we are making sure the database connection closes as this script has finished doing what it needs to do at this time</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 399px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>$dbconnect.Close()</div>
<p>Using this example you can now easily collect data from your servers and insert it into a database. You can as well use any SQL statement you wish, including querying a database, and using Powershell to output/manipulate this data as needed. In future posts I will get into some examples of what I have done with powershell and mysql.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jaredperry.ca/2009/06/powershell-and-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

