Sign in
|
Join PowerShell.com!
|
Help
Home
PowerTips
Ask the Experts
Forums
Webcasts
Blogs
eBookV2
Script Library
Twitter Grid
Modules
QuickClick
Scripts
Snippets
Videos
Library
»
Script Library
»
SQL Server
»
CreateDB-MSSQL-UsingSMO
CreateDB-MSSQL-UsingSMO
Share
|
Browse Library
Module Library
QuickClick Library
Script Library
Active Directory
BizTalk
Citrix
Clustering
Desktop
Exchange Server 2003
Exchange Server 2007
File System
Group Policy
Internet Information Server (IIS)
Local Accounts
Logs
Microsoft Office
Microsoft Team Foundation Server
MySQL
Networking
Registry
Remote Desktop Services
Remoting
Security
SharePoint
SQL Server
System Center Virtual Machine Manager
System Center Configuration Manager
System Center Operations Manager
Tutorial
Terminal Server
Using .Net
Virtual Server
VMware
Windows 7
Windows HPC
Windows Server 2000
Windows Server 2003
Windows Server 2008
Windows XP
WMI
Misc
Snippet Library
Video Library
Members Only
Tags
Access
AD
ADO
Analysis Server
Attributes
Backup
CheckDB
Connect
Connection String
Create database
Create table
CSV
Database
Port
PowerShell
Properties
Service
SMO
SQL
SQL Authentication
SQL Server
stop jobs
test-connection
Windows Authentication
WMI
View more
Previous
|
Next
|
View all files
|
View Slideshow
Download
posted by
Richard Giles
10-06-2008
Downloads: 519
File size: 4kB
Views: 2,587
Embed
CreateDB-MSSQL-UsingSMO
## =====================================================================
## Title
: CreateDB-MSSQL-UsingSMO
## Description : Create and empty database, drop if existing
## Author
: Idera
## Date
: 6/27/2008
## Input
: -server <server\instance>
##
-dbName <database name>
##
-verbose
##
-debug
## Output
: Formatted table with database name and creation date
## Usage
: PS> . CreateDB-MSSQL-UsingSMO -server MyServer -dbName SMOTestDB -v -d
## Notes
: Adapted from Allen White script
## Tag
: SQL Server, SMO, Create database
## Change Log
:
## =====================================================================
param
(
[
string
]
$server
=
"(local)"
,
[
string
]
$dbName
=
"SMOTestDB"
,
[
switch
]
$verbose
,
[
switch
]
$debug
)
function
main()
{
if
(
$verbose
) {
$VerbosePreference
=
"Continue"
}
if
(
$debug
) {
$DebugPreference
=
"Continue"
}
CreateDB-MSSQL
-UsingSMO
$server
$dbName
}
function
CreateDB-MSSQL
-UsingSMO
(
$server
,
$dbName
)
{
trap
[
Exception
]
{
write-error
$
(
"TRAPPED: "
+
$_
.
Exception.Message
);
continue
;
}
# Load-SMO assemblies
[
void
][
System.Reflection.Assembly
]::
LoadWithPartialName
(
"Microsoft.SqlServer.Smo"
)
[
void
][
System.Reflection.Assembly
]::
LoadWithPartialName
(
"Microsoft.SqlServer.SqlEnum"
)
[
void
][
System.Reflection.Assembly
]::
LoadWithPartialName
(
"Microsoft.SqlServer.SmoEnum"
)
[
void
][
System.Reflection.Assembly
]::
LoadWithPartialName
(
"Microsoft.SqlServer.ConnectionInfo"
)
# Get a server object for the server instance
Write-Debug
"Creating SMO Server object for $server"
$NamedInstance
=
New-Object
-typeName
Microsoft.SqlServer.Management.Smo.Server
`
-argumentList
$server
cls
# Connect to the server with Windows Authentication and drop database if exist
# TIP: using PowerShell "not equal" operator
if
(
$NamedInstance
.
Databases
[
$dbName
]
-ne
$null
)
{
Write-Debug
"The test database already exists on $DefaultServer"
Write-Debug
"Deleting it now..."
$NamedInstance
.
Databases
[
$dbName
].
drop
()
}
# Instantiate a database object
Write-Debug
"Createing SMO server, database and filegroup objects..."
$namedInstance
=
new-object
-typename
Microsoft.SqlServer.Management.Smo.Server
`
-argumentlist
$server
$database
=
new-object
-typename
Microsoft.SqlServer.Management.Smo.Database
`
-argumentlist
$namedInstance
,
$dbName
$filegroup
=
new-object
-typename
Microsoft.SqlServer.Management.Smo.FileGroup
`
-argumentlist
$database
,
"PRIMARY"
# Add the PRIMARY filegroup to the database
$database
.
FileGroups.Add
(
$filegroup
)
# Instantiate the data file object and add it to the PRIMARY filegroup
$dbfile
=
$dbName
+
"_Data"
$dbdfile
=
new-object
(
'Microsoft.SqlServer.Management.Smo.DataFile'
) (
$filegroup
,
$dbfile
)
$filegroup
.
Files.Add
(
$dbdfile
)
Write-Debug
"Set properties of the data and log file"
#Set the properties of the data file
$masterDBPath
=
$namedInstance
.
Information.MasterDBPath
$dbdfile
.
FileName
=
$masterDBPath
+
"\"
+
$dbfile
+
".mdf"
$dbdfile
.
Size
=
[
double
](25.0
*
1024.0)
$dbdfile
.
GrowthType
=
"Percent"
$dbdfile
.
Growth
=
25.0
$dbdfile
.
MaxSize
=
[
double
](100.0
*
1024.0)
#Instantiate the log file object and set its properties
$masterDBLogPath
=
$namedInstance
.
Information.MasterDBLogPath
$logfile
=
$dbName
+
"_Log"
$dblfile
=
new-object
(
'Microsoft.SqlServer.Management.Smo.LogFile'
) (
$database
,
$logfile
)
$dblfile
.
FileName
=
$masterDBLogPath
+
"\"
+
$logfile
+
".ldf"
$dblfile
.
Size
=
[
double
](10.0
*
1024.0)
$dblfile
.
GrowthType
=
"Percent"
$dblfile
.
Growth
=
25.0
# Create the new database on the server
$Database
.
Create
()
# List the database on the server that was just added to confirm that it was added
# TIP: using PowerShell to pipe an object list to a Where-Object filtering on a variable
#
and then building a formated table with properties as output to the console
$NamedInstance
.
Databases
|
Where-Object
{
$_
.
name
-eq
"$dbName"
} |
Format-Table
-property
name, createdate
}
main
Filed under:
SQL Server
,
SMO
,
Create database
Create a SQL Server empty database.
Copyright 2011 PowerShell.com. All rights reserved.