MySQL with PowerShell

MySQL is a common database amongst many organizations. As such, when building an automation script that needs to query data from somewhere, you might run into the need to query a MySQL database. By using PowerShell and by taking a shortcut and using an existing MySQL module, you can make this happen.

PowerShell Module for Querying MySQL

The module found that easily allows querying MySQL with PowerShell is the SimplySQL module.

SimplySQL is easily installed as it is a module that can be pulled down and installed right from within PowerShell itself. To install SimplySQL, run the following command to install the module:

Install-Module -Name SimplySql Get-Module SimplySQL ModuleType Version Name ExportedCommands ---------- ------- ---- ---------------- Script 1.6.2 SimplySql {Clear-SqlMessage, Close-SqlConnection...}

How To Query a MySQL Database with PowerShell

For SimplySQL MySQL queries with PowerShell, you have the Open-MySQLConnection and Invoke-SQLQuery cmdlet that can be used to connect to your MySQL server, pass along credentials, and also run a specified query for the cmdlet.

Build Connection

Open-MySQLConnection -Server "localhost" -Port "3360" -Credential "root" -Database "identityiq"

Using the Invoke-SqlQuery you can pass along the MySQL query to the connection you open with the Open-MySQLConnection cmdlet.

Invoke-SqlQuery -Query "select count(*) from spt_identity" count(*) -------- 21