Add convert-sql2csv.ps1

This commit is contained in:
Markus Fleschutz
2021-04-10 10:40:03 +02:00
parent 38f7ddb157
commit d3e82db87a
4 changed files with 45 additions and 32 deletions

View File

@ -1,40 +1,25 @@
#!/usr/bin/pwsh
<#
.SYNTAX convert-mysql2csv.ps1 [<server>] [<database>] [<DBuser>] [<DBpassword>] [<query>]
.SYNTAX convert-mysql2csv.ps1 [<server>] [<database>] [<username>] [<password>] [<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
param($server = "", $database = "", $username = "", $password = "", $query = "")
if ($server -eq "") { $server = read-host "Enter the hostname/IP address of the MySQL server" }
if ($database -eq "") { $database = read-host "Enter the database name" }
if ($username -eq "") { $username = read-host "Enter the database username" }
if ($password -eq "") { $password = read-host "Enter the database user password" }
if ($query -eq "") { $query = read-host "Enter the database query" }
try {
$csvfilepath = "$PSScriptRoot\mysql_table.csv"
$result = Invoke-MySqlQuery -ConnectionString "server=$server; database=$database; user=$username; password=$password; pooling = false; convert zero datetime=True" -Sql $query -CommandTimeout 10000
$result | Export-Csv $csvfilepath -NoTypeInformation
exit 0
} catch {
write-error "ERROR: line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}

View File

@ -0,0 +1,26 @@
#!/usr/bin/pwsh
<#
.SYNTAX convert-sql2csv.ps1 [<server>] [<database>] [<username>] [<password>] [<query>]
.DESCRIPTION convert the SQL database table to a CSV file
.LINK https://github.com/fleschutz/PowerShell
.NOTES Author: Markus Fleschutz / License: CC0
#>
param($server = "", $database = "", $username = "", $password = "", $query = "")
if ($server -eq "") { $server = read-host "Enter the hostname/IP address of the SQL server" }
if ($database -eq "") { $database = read-host "Enter the database name" }
if ($username -eq "") { $username = read-host "Enter the database username" }
if ($password -eq "") { $password = read-host "Enter the database user password" }
if ($query -eq "") { $query = read-host "Enter the database query" }
try {
$secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential ($username, $secpasswd)
$csvfilepath = "$PSScriptRoot\sqlserver_table.csv"
$result = Invoke-SqlServerQuery -Credential $creds -ConnectionTimeout 10000 -Database $database -Server $server -Sql $query -CommandTimeout 10000
$result | Export-Csv $csvfilepath -NoTypeInformation
exit 0
} catch {
write-error "ERROR: line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}