Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7973827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:12:53+00:00 2026-06-04T08:12:53+00:00

I’m creating a set of databases on a SQL Server 2008R2 instance using powershell,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T08:12:55+00:00Added an answer on June 4, 2026 at 8:12 am

    You couldn’t wrap this in SQL in a transaction either, since CREATE DATABASE cannot be enrolled in a transaction:

    The CREATE DATABASE statement must run in autocommit mode (the default
    transaction management mode) and is not allowed in an explicit or
    implicit transaction.

    So the capability cannot be achieved in PS, SMO nor any other way. In lack of transactions you need to handle errors carefully. It would be very difficult to achieve atomic behavior for ‘all or none’ databases to be created. If you hit an error, you are not guaranteed that you can drop the databases already created.

    If possible, is better to thrive for idempotency: if you run the script and hit an error on database 5, next time you run it it skips databases 1-4 which were already created. Or, alternatively, it drops the databases already created if exist and create them again (this is risky, as the script may end up dropping the wrong DBs due to operator error).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.