Add convert-mysql2csv.ps1

This commit is contained in:
Markus Fleschutz 2021-04-10 10:10:58 +02:00
parent 99197dfc29
commit a73e6d9885
3 changed files with 42 additions and 0 deletions

View File

@ -27,6 +27,7 @@ close-thunderbird.ps1, closes Mozilla Thunderbird gracefully
close-vlc.ps1, closes the VLC media player gracefully
close-windows-terminal.ps1, closes Windows Terminal gracefully
configure-git.ps1, sets up the Git user configuration
convert-mysql2csv.ps1, converts the MySQL database table to a CSV file
create-branch.ps1, creates a new branch in the current/given Git repository
create-shortcut.ps1, creates a shortcut
create-symlink.ps1, creates a symbolic link

1 Script Description
27 close-vlc.ps1 closes the VLC media player gracefully
28 close-windows-terminal.ps1 closes Windows Terminal gracefully
29 configure-git.ps1 sets up the Git user configuration
30 convert-mysql2csv.ps1 converts the MySQL database table to a CSV file
31 create-branch.ps1 creates a new branch in the current/given Git repository
32 create-shortcut.ps1 creates a shortcut
33 create-symlink.ps1 creates a symbolic link

View File

@ -148,6 +148,7 @@ Mega Collection of PowerShell Scripts
* [check-ipv4-address.ps1](Scripts/check-ipv4-address.ps1) - checks the given IPv4 address for validity
* [check-ipv6-address.ps1](Scripts/check-ipv6-address.ps1) - checks the given IPv6 address for validity
* [check-mac-address.ps1](Scripts/check-mac-address.ps1) - checks the given MAC address for validity
* [convert-mysql2csv.ps1](Scripts/convert-mysql2csv.ps1) - converts the MySQL database table to a CSV file
* [csv-to-text.ps1](Scripts/csv-to-text.ps1) - converts the given CSV file into a text list
* [display-time.ps1](Scripts/display-time.ps1) - displays the current time for 10 seconds by default
* [generate-qrcode.ps1](Scripts/generate-qrcode.ps1) - generates a QR code

View File

@ -0,0 +1,40 @@
#!/usr/bin/pwsh
<#
.SYNTAX convert-mysql2csv.ps1 [<server>] [<database>] [<DBuser>] [<DBpassword>] [<query>]
.DESCRIPTION convert the MySQL database table to a CSV file
.LINK https://github.com/fleschutz/PowerShell
.NOTES Author: Markus Fleschutz / License: CC0
#>
param(
[Parameter(Mandatory=$true)]
$server,
[Parameter(Mandatory=$true)]
$database,
[Parameter(Mandatory=$true)]
$dbuser,
[Parameter(Mandatory=$true)]
$dbpass,
[Parameter(Mandatory=$true)]
$query
)
if (($server -eq "") -or ($server -eq $null)) {
Write-Host "Please specify a server"
}
elif (($database -eq "") -or ($database -eq $null)) {
Write-Host "Please specify a database"
}
elif (($dbuser -eq "") -or ($dbuser -eq $null)) {
Write-Host "Please specify a database user"
}
elif (($dbpass -eq "") -or ($dbpass -eq $null)) {
Write-Host "Please specify a database user password"
}
elif (($query -eq "") -or ($query -eq $null)) {
Write-Host "Please specify a query"
}
else {
$csvfilepath = "$PSScriptRoot\mysql_table.csv"
$result = Invoke-MySqlQuery -ConnectionString "server=$server; database=$database; user=$dbuser; password=$dbpass; pooling = false; convert zero datetime=True" -Sql $query -CommandTimeout 10000
$result | Export-Csv $csvfilepath -NoTypeInformation
}