Jump to content
Sign in to follow this  
support

Validate Password for MySQL Account (by Click Studios)

Recommended Posts

Script Name

Validate Password for MySQL Account

 

Description

Checks if a MySQL Account Password is correct

 

System Requirements

  • Firewall to allow access

 

Successfully Tested Against

  • MySQL Server 5.6.16

 

Failed Testing Against

  • None Reported

 
PowerShell Script

<#
.SYNOPSIS
Connect to a MySQL server and validate the password for a local account.
.NOTES
Requires database connections on in-use Port to be allowed through Firewall
#>
function Validate-MySQLPassword
{
	[CmdletBinding()]
	param (
		[String]$HostName,
		[String]$SQLPort,
		[String]$UserName,
		[String]$CurrentPassword
	)
	
	try
	{
		#Declare some connection string variables
		[String]$MySQLPortString = ''
		
		#Construct the Port Number section of the connection string if required
		if ($SQLPort -ne '') {
			$MySQLPortString = ';port=' + $SQLPort
		}
		
		[void][System.Reflection.Assembly]::LoadFrom("[PasswordstateBinFolderPath]\MySQL.Data.dll")
		$MySQLConnection = New-Object MySql.Data.MySqlClient.MySqlConnection
		$MySQLConnection.ConnectionString = "server=" + $HostName + $SQLPortString + ";uid=" + $UserName + ";pwd=" + $CurrentPassword
		$MySQLConnection.Open()
		$MySQLConnection.Close()
		Write-Output "Success"
	}
	catch
	{
		switch -wildcard ($error[0].Exception.ToString().ToLower())
		{
			"*Unable to connect*" { Write-Output "Failed to connect to the Host '$HostName' to validate the password for the account '$UserName'. Please check the Host is online, or if a Firewall is blocking access - default Port is 3306."; break }
			"*Access denied for user*" { Write-Output "Failed to validate the password for the SQL account '$UserName' on Host '$HostName'. UserName or Password is incorrect."; break }
			#Add other wildcard matches here as required
			default { Write-Output "Failed to validate the password for the MySQL account '$UserName' on Host '$HostName'. Error = " + $error[0].Exception }
		}		
	}
}

#Make a call to the Validate-MySQLPassword function
Validate-MySQLPassword -HostName '[HostName]' -SQLPort '[DatabasePort]' -Username '[UserName]' -CurrentPassword '[CurrentPassword]'

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×