forked from jrussellfreelance/powershell-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-csv-from-sqlserver-table.ps1
31 lines (31 loc) · 1.18 KB
/
create-csv-from-sqlserver-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
# 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)]$query,
[Parameter(Mandatory=$true)]$username,
[Parameter(Mandatory=$true)]$password
)
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 (($username -eq "") -or ($username -eq $null)) {
Write-Host "Please specify a database user"
}
elif (($password -eq "") -or ($password -eq $null)) {
Write-Host "Please specify a database user password"
}
elif (($query -eq "") -or ($query -eq $null)) {
Write-Host "Please specify a query"
}
else {
$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
}