I'm creating a set of databases on a SQL Server 2008R2 instance using powershell, like this:
foreach ($database in $databases) {
# Create DB
$db = New-Object Microsoft.SqlServer.Management.SMO.Database
$db.Parent = Get-Item "SQLSERVER:\SQL\$DBServer\$Instance"
$db.Name = $database
$db.Create()
#
# Create tables, fill in data, etc
#
}
Here I'm assuming that none of the databases to be created already exist. If it turns out that one does, I want to abort and rollback. In SQL I would just wrap this in a BEGIN TRAN and ROLLBACK or COMMIT, but can I do this in Powershell? There is a cmdlet Start-Transaction, but it requires support from the commands used in the transactions, and $db.Create() does not.
Is there a way in the SMO object to do transactions? I could of course just check all the databases in advance, but the entire script is quite complicated, and I feel it would be more understandable if I can have the logic closer to the creation.