CreateDB-MSSQL-UsingSMO


posted by Richard Giles
10-06-2008

Downloads: 519
File size: 4kB
Views: 2,587

Embed
CreateDB-MSSQL-UsingSMO
  1. ## ===================================================================== 
  2. ## Title       : CreateDB-MSSQL-UsingSMO 
  3. ## Description : Create and empty database, drop if existing 
  4. ## Author      : Idera 
  5. ## Date        : 6/27/2008 
  6. ## Input       : -server <server\instance> 
  7. ##               -dbName <database name> 
  8. ##               -verbose  
  9. ##               -debug     
  10. ## Output      : Formatted table with database name and creation date 
  11. ## Usage            : PS> . CreateDB-MSSQL-UsingSMO -server MyServer -dbName SMOTestDB -v -d 
  12. ## Notes            : Adapted from Allen White script 
  13. ## Tag            : SQL Server, SMO, Create database 
  14. ## Change Log  : 
  15. ## ===================================================================== 
  16.  
  17. param 
  18.       [string]$server = "(local)"
  19.     [string]$dbName = "SMOTestDB"
  20.     [switch]$verbose
  21.     [switch]$debug 
  22.  
  23. function main() 
  24.     if ($verbose) {$VerbosePreference = "Continue"
  25.     if ($debug) {$DebugPreference = "Continue"
  26.     CreateDB-MSSQL-UsingSMO $server $dbName  
  27.  
  28. function CreateDB-MSSQL-UsingSMO($server, $dbName
  29.     trap [Exception]  
  30.     
  31.         write-error $("TRAPPED: " + $_.Exception.Message); 
  32.         continue
  33.     
  34.      
  35.     # Load-SMO assemblies 
  36.     [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo"
  37.     [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlEnum"
  38.     [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum")  
  39.     [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo")  
  40.      
  41.     # Get a server object for the server instance 
  42.     Write-Debug "Creating SMO Server object for $server" 
  43.     $NamedInstance = New-Object -typeName Microsoft.SqlServer.Management.Smo.Server
  44.         -argumentList $server 
  45.      
  46.     cls 
  47.      
  48.     # Connect to the server with Windows Authentication and drop database if exist 
  49.     # TIP: using PowerShell "not equal" operator 
  50.     if ($NamedInstance.Databases[$dbName] -ne $null)  
  51.     
  52.         Write-Debug "The test database already exists on $DefaultServer" 
  53.         Write-Debug "Deleting it now..." 
  54.         $NamedInstance.Databases[$dbName].drop() 
  55.     
  56.      
  57.     # Instantiate a database object 
  58.     Write-Debug "Createing SMO server, database and filegroup objects..." 
  59.     $namedInstance = new-object -typename Microsoft.SqlServer.Management.Smo.Server
  60.         -argumentlist $server 
  61.     $database = new-object -typename Microsoft.SqlServer.Management.Smo.Database
  62.         -argumentlist $namedInstance, $dbName 
  63.     $filegroup = new-object -typename Microsoft.SqlServer.Management.Smo.FileGroup
  64.         -argumentlist $database, "PRIMARY" 
  65.  
  66.     # Add the PRIMARY filegroup to the database 
  67.     $database.FileGroups.Add($filegroup
  68.      
  69.     # Instantiate the data file object and add it to the PRIMARY filegroup 
  70.     $dbfile = $dbName + "_Data" 
  71.     $dbdfile = new-object ('Microsoft.SqlServer.Management.Smo.DataFile') ($filegroup, $dbfile
  72.     $filegroup.Files.Add($dbdfile
  73.      
  74.     Write-Debug "Set properties of the data and log file" 
  75.      
  76.     #Set the properties of the data file 
  77.     $masterDBPath = $namedInstance.Information.MasterDBPath 
  78.     $dbdfile.FileName = $masterDBPath + "\" + $dbfile + ".mdf" 
  79.     $dbdfile.Size = [double](25.0 * 1024.0) 
  80.     $dbdfile.GrowthType = "Percent" 
  81.     $dbdfile.Growth = 25.0 
  82.     $dbdfile.MaxSize = [double](100.0 * 1024.0) 
  83.      
  84.     #Instantiate the log file object and set its properties 
  85.     $masterDBLogPath = $namedInstance.Information.MasterDBLogPath 
  86.     $logfile = $dbName + "_Log" 
  87.     $dblfile = new-object ('Microsoft.SqlServer.Management.Smo.LogFile') ($database, $logfile
  88.     $dblfile.FileName = $masterDBLogPath + "\" + $logfile + ".ldf" 
  89.     $dblfile.Size = [double](10.0 * 1024.0) 
  90.     $dblfile.GrowthType = "Percent" 
  91.     $dblfile.Growth = 25.0 
  92.  
  93.     # Create the new database on the server 
  94.     $Database.Create() 
  95.      
  96.     # List the database on the server that was just added to confirm that it was added 
  97.     # TIP: using PowerShell to pipe an object list to a Where-Object filtering on a variable 
  98.     #      and then building a formated table with properties as output to the console 
  99.     $NamedInstance.Databases | Where-Object {$_.name -eq "$dbName"} | Format-Table -property name, createdate 
  100.  
  101. main 

Create a SQL Server empty database.

Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.