Skip to content

Instantly share code, notes, and snippets.

@sergejusb
Last active December 16, 2015 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sergejusb/5370816 to your computer and use it in GitHub Desktop.
Save sergejusb/5370816 to your computer and use it in GitHub Desktop.
Basic Power-Shell cmdlets to work with SQL Server (via sqlcmd.exe)
# -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# SQL Server functions
function Execute-Sql($sql, $username, $password, $file) {
Write-Host -ForegroundColor Cyan "Executing $file on $sql with user $username"
& sqlcmd -S $sql -U $username -P $password -i $file
}
function Execute-SqlDir($sql, $username, $password, $dir) {
Get-ChildItem $dir | Select-Object FullName | foreach {
Write-Host -ForegroundColor Cyan "Executing $_.FullName on $sql with user $username"
Execute-Sql $sql $username $password $_.FullName
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment