Creating Databases with PowerShell

One of the great new features in SQL Server 2008 is the PowerShell extension that interacts with the SQL Server. In order to create a database in PowerShell, you'll first need to create a PowerShell script that you can call. Listing 8.2 provides just such a script for you.

Listing 8.2: The CreateDB.ps1 script code
 # Get the command line options passed to the script Param($dbInstance, $dbName) [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") $dbServer = new-object Microsoft.SqlServer.Management.Smo.Server ($dbInstance) $db = new-object Microsoft.SqlServer.Management.Smo.Database "Now creating database $dbName..." $db = new-object Microsoft.SqlServer.Management.Smo.Database ($dbServer, ...

Get SQL Server® 2008 Administration: Real World Skills for MCITP Certification and Beyond now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.