<# FPS:Containers:DataResources:Get-mySQLData:ACTIVE
#>
function Get-mySQLData {
<#
.DESCRIPTION
Module: DataReources
Public CmdLet:
Get-mySQLData.ps1
Has Dependencies: See notes
This CmdLet encapsulates the
execution of an SQL Query on the selected MySQL DB
.EXAMPLE
if (Push-NeededFor Get-mySQLData) { . (Get-ActiveFileNm -PS1 Get-mySQLData) }
$SQLResult=Get-mySQLData -SQLDB "local" -SQLQuery
"SELECT * FROM wp_term_relationships"
-or-
if (Push-NeededFor Get-mySQLData) { . (Get-ActiveFileNm -PS1 Get-mySQLData) }
$SQLResult=Get-mySQLData -SQLQuery "SELECT
* FROM wp_term_relationships"
$ptr=0
while ($ptr -lt $SQLResult.length) {
if ($SQLResult.term_taxonomy_id[$ptr]
-eq 3) {
Write-Host
('object_id = '+$SQLResult.object_id[$ptr]+', term_taxonomy_id = '+$SQLResult.term_taxonomy_id[$ptr])
}
$ptr++
}
WP Base Tables:
wp_commentmeta
wp_comments
wp_links
wp_options
wp_postmeta
wp_posts
wp_term_relationships
wp_term_taxonomy
wp_termmeta
wp_terms
wp_usermeta
wp_users
.CC
[2024.10.04 JD] Original code release. Will likely get tweeked with use.
.NOTES
NuGet repository has the 64 bit MySql.Data.Dll
# To see if NuGet is there, likely just PSGallery
get-psrepository
# If not, install it
Install-PackageProvider -Name
NuGet
# Then register it
Register-PackageSource
-provider NuGet -name nugetRepository -location
https://www.nuget.org/api/v2
# You should see the NuGet repository here
Get-PackageSource
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)] [string]$SQLDB = $global:Pro_CI.SQLDB,
[Parameter(Mandatory=$false)] [string]$SQLSvr = "localhost",
# Default to host CI
[Parameter(Mandatory=$false)] [string]$SQLUID = $global:Pro_CI.SQLUID,
[Parameter(Mandatory=$false)] [string]$SQLPwd = $global:Pro_CI.SQLPwd,
[Parameter(Mandatory=$false)] [string]$SQLPort = $global:Pro_CI.SQLPort, # Default to "local's" port on
the server
[Parameter(Mandatory=$false)] [string]$SQLQuery
)
# Load in Connector/Net
$Fnm = "C:\Program
Files\PackageManagement\NuGet\Packages\MySql.Data.9.0.0\lib\net6.0\MySql.Data.dll"
#$Fnm =
"D:\FPS\Media\MySql.Data.9.0.0\lib\net6.0\MySql.Data.dll"
[void][System.Reflection.Assembly]::LoadFrom($Fnm)
$ConnectionString = "server=$SQLSvr;uid=$SQLUID;pwd=$SQLPwd;database=$SQLDB;port=$SQLPort;AllowZeroDateTime=True"
$Connection = New-Object -TypeName MySql.Data.MySqlClient.MySqlConnection
$Connection.ConnectionString = $ConnectionString
try {
$Connection.Open()
$Cmd = New-Object MySql.Data.MySqlClient.MySqlCommand($SqlQuery, $Connection)
$DataAdapter = New-Object MySql.Data.MySqlClient.MySqlDataAdapter($Cmd)
$TmpTable = New-Object System.Data.DataTable
$RecCnt = $DataAdapter.Fill($TmpTable)
if ($Connection.ServerThread) {
$Connection.Close()
}
}
catch { Write-Error ("Error: $_") }
#Write-Host ("Length = "+$RecCnt)
#Write-Host ("Result Table = "+$TmpTable)
return $TmpTable
}