mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-03-26 06:18:17 +01:00
Add convert-sql2csv.ps1
This commit is contained in:
parent
38f7ddb157
commit
d3e82db87a
@ -29,6 +29,7 @@ close-windows-terminal.ps1, closes Windows Terminal gracefully
|
||||
configure-git.ps1, sets up the Git user configuration
|
||||
convert-csv2txt.ps1, converts the given CSV file into a text list
|
||||
convert-mysql2csv.ps1, converts the MySQL database table to a CSV file
|
||||
convert-sql2csv.ps1, converts the SQL 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
|
||||
|
|
@ -150,6 +150,7 @@ Mega Collection of PowerShell Scripts
|
||||
* [check-mac-address.ps1](Scripts/check-mac-address.ps1) - checks the given MAC address for validity
|
||||
* [convert-csv2txt.ps1](Scripts/convert-csv2txt.ps1) - converts the given CSV file into a text list
|
||||
* [convert-mysql2csv.ps1](Scripts/convert-mysql2csv.ps1) - converts the MySQL database table to a CSV file
|
||||
* [convert-sql2csv.ps1](Scripts/convert-sql2csv.ps1) - converts the SQL database table to a CSV file
|
||||
* [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
|
||||
* [list-anagrams.ps1](Scripts/list-anagrams.ps1) - lists all anagrams of the given word
|
||||
|
@ -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
|
||||
}
|
||||
|
26
Scripts/convert-sql2csv.ps1
Normal file
26
Scripts/convert-sql2csv.ps1
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user