Skip to content

Instantly share code, notes, and snippets.

@m-wild
Created July 4, 2017 11:16
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 m-wild/7b50c4259d63620e528dac363700ec2b to your computer and use it in GitHub Desktop.
Save m-wild/7b50c4259d63620e528dac363700ec2b to your computer and use it in GitHub Desktop.
function New-SqlDatabase ($server, $database, $path) {
$sw = Start-Timer
Write-Host "Creating database '$($database)'"
Write-Host "SQL Instance: $($server)"
if (!(Test-Path $path)) { New-Item -ItemType Directory -Path $path | Out-Null }
$query = "CREATE DATABASE [$($database)]
ON PRIMARY ( NAME = '$($database)', FILENAME = '$($path)\$($database).mdf' )
LOG ON ( NAME = '$($database)_log', FILENAME = '$($path)\$($database)_log.ldf' );"
Invoke-Sqlcmd -ServerInstance $server -Database master -Query $query
Write-Host "Successfully created database '$($database)' $($sw | Stop-Timer | Get-ExecutionTime)."
}
function Remove-SqlDatabase ($server, $database) {
$sw = Start-Timer
Write-Host "Dropping database '$($database)'"
Write-Host "SQL Instance: $($server)"
$sqlQuery = "ALTER DATABASE [$($database)] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; DROP DATABASE [$($database)];"
Invoke-Sqlcmd -ServerInstance $server -Database master -Query $sqlQuery
Write-Host "Successfully dropped database '$($database)' $($sw | Stop-Timer | Get-ExecutionTime)."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment