notes/sql_server/mssql_csv.ps1

15 lines
661 B
PowerShell

# Check if the SqlServer module is installed
if (-not (Get-Module -Name SqlServer -ListAvailable)) {
# If not installed, install the SqlServer module
Install-Module -Name SqlServer -Force
}
#import module
Import-Module -Name SqlServer
# Define variables for SQL command and destination file path
$SqlQuery = "SELECT top 1000 * FROM rlarp.osm_stack WHERE version = 'Actual'"
$DestinationFilePath = "C:\Users\ptrowbridge\Downloads\osm_stack.csv"
# Execute the SQL query and export to CSV
Invoke-Sqlcmd -ServerInstance "usmidsql01" -Database "fanalysis" -Query $SqlQuery -TrustServerCertificate | Export-Csv -Path $DestinationFilePath -NoTypeInformation