Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.