-
Notifications
You must be signed in to change notification settings - Fork 61
/
create-csv-from-mysql-table.ps1
34 lines (34 loc) · 1.06 KB
/
create-csv-from-mysql-table.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Install-Module InvokeQuery
# Run the above command if you do not have this module
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
}