-
-
Save andyhuey/5cc37ffcb6c2120f269799249b44334d to your computer and use it in GitHub Desktop.
run a bunch of SQL exports, save to CSV, and ZIP them
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# CustInvAll-export.ps1 | |
#Requires -Version 7 | |
#Requires -Modules SqlServer | |
$dateFmt = 'yyyy-MM-dd' | |
$sqlServer = "MyServer" | |
$dbName = "myDB" | |
$curDate = [DateTime]::Today | |
$startDate = [DateTime]'01/01/2021' | |
while ($startDate -lt $curDate) { | |
$endDate = $startDate.AddMonths(1) | |
$startDateFmt = $startDate.ToString($dateFmt) | |
$endDateFmt = $endDate.ToString($dateFmt) | |
$exportSQL = @" | |
SELECT * | |
FROM MyTable | |
where [INVOICEDATE] >= '$startDateFmt' and [INVOICEDATE] < '$endDateFmt' | |
"@ | |
$exportFile = "CustInvAll-$startDateFmt.csv" | |
$exportFileZip = "CustInvAll-$startDateFmt-csv.zip" | |
echo "Exporting from $startDateFmt to $EndDateFmt to file $exportFile" | |
# Invoke-Sqlcmd -ServerInstance $sqlServer -Database $dbName -Query $exportSQL ` | |
# | Export-CSV -Path $exportFile -NoTypeInformation -UseQuotes AsNeeded | |
# Compress-Archive -LiteralPath $exportFile -DestinationPath $exportFileZip | |
$startDate = $endDate | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment