From 8935f63123b2300413c6d07b96ee047227eade08 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Mon, 18 Sep 2023 08:45:03 -0400 Subject: [PATCH] scripts --- postgres-odbc.ps1 | 35 +++++++++++++++++++++++++++++++++++ sql_server/mssql_csv.ps1 | 14 ++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 postgres-odbc.ps1 create mode 100644 sql_server/mssql_csv.ps1 diff --git a/postgres-odbc.ps1 b/postgres-odbc.ps1 new file mode 100644 index 0000000..79c08e9 --- /dev/null +++ b/postgres-odbc.ps1 @@ -0,0 +1,35 @@ +# Check if the script is running as administrator +$isAdmin = ([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544" +if (-not $isAdmin) { + # Re-launch the script with elevated privileges + Start-Process powershell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs + exit +} + +# note elevation +Write-Host "Running with administrator privileges" + +# setup sources and destinations +$zipUrl = "https://ftp.postgresql.org/pub/odbc/versions/msi/psqlodbc_15_00_0000.zip" +$outputPath = Join-Path $env:USERPROFILE "Downloads\psqlodbc.zip" +$extractPath = Join-Path $env:USERPROFILE "Downloads\psqlodbc" + +# Download the ZIP file +Invoke-WebRequest -Uri $zipUrl -OutFile $outputPath + +# Extract the contents +Expand-Archive -Path $outputPath -DestinationPath $extractPath -Force + +# Find and run the MSI file +$msiFile = Get-ChildItem -Path $extractPath -Filter "psqlodbc-setup*" | Where-Object { ! $_.PSIsContainer } +if ($msiFile) { + Start-Process -FilePath $msiFile.FullName -Wait +} + +# Remove downloaded files and extracted contents +Remove-Item $outputPath -Force +Remove-Item $extractPath -Recurse -Force + +# setup odbc dsn +Add-OdbcDsn -Name "usmidsap02" -DriverName "PostgreSQL Unicode(x64)" -DsnType "System" -SetPropertyValue @("Server=usmidsap02", "Database=ubm", "UserName=report","Password=report") + diff --git a/sql_server/mssql_csv.ps1 b/sql_server/mssql_csv.ps1 new file mode 100644 index 0000000..18d3988 --- /dev/null +++ b/sql_server/mssql_csv.ps1 @@ -0,0 +1,14 @@ +# 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