Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Implementing SMO in Powershell

Reply
 
Old 09-20-2007   #1 (permalink)
NJC


 
 

Implementing SMO in Powershell

Hello,

I am fairly new to Powershell and certainly not to familiar with SMO, so
really need some help with this problem I’ve encountered.

I’m essentially trying to script the creation of a SQL Server 2005 database
with the applicable entities using SMO. I have successfully created a
database using SMO, but not too sure on how to script the creation of new
entities and add them to a particular database. I have searched high and low
but can’t find any relevant information on the subject.

I have found a useful add-on called PowerSMO but this can only be used for
test purposes whereas I will be implementing my work into production.

I guess I’ll have to use something like
‘Microsoft.SqlServer.Management.Smo.Table’ but I’m not entirely sure of the
syntax or how to implement this object.

If anyone can answer my question or point me in the right direction (any
direction) I’d really appreciate it.

Thanks,




My System SpecsSystem Spec
Old 09-20-2007   #2 (permalink)
Shay Levi


 
 

Re: Implementing SMO in Powershell

Try

http://www.databasejournal.com/featu...le.php/3691566


Shay
http://scriptolog.blogspot.com


Quote:

> Hello,
>
> I am fairly new to Powershell and certainly not to familiar with SMO,
> so really need some help with this problem I’ve encountered.
>
> I’m essentially trying to script the creation of a SQL Server 2005
> database with the applicable entities using SMO. I have successfully
> created a database using SMO, but not too sure on how to script the
> creation of new entities and add them to a particular database. I have
> searched high and low but can’t find any relevant information on the
> subject.
>
> I have found a useful add-on called PowerSMO but this can only be used
> for test purposes whereas I will be implementing my work into
> production.
>
> I guess I’ll have to use something like
> ‘Microsoft.SqlServer.Management.Smo.Table’ but I’m not entirely sure
> of the syntax or how to implement this object.
>
> If anyone can answer my question or point me in the right direction
> (any direction) I’d really appreciate it.
>
> Thanks,
>

My System SpecsSystem Spec
Old 09-20-2007   #3 (permalink)
RichS


 
 

Re: Implementing SMO in Powershell

I got this to work. Obviously need to add more columns as required

$null =
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo")
$null =
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum")
$null = [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
$Server = new-object Microsoft.SqlServer.Management.Smo.Server("RSSQL1")
$db = $server.Databases["Test"]

$table = New-Object Microsoft.SqlServer.Management.Smo.Table($db, "NewTable")


$dt1 = [Microsoft.SqlServer.Management.Smo.Datatype]::Nchar(50)
$col1 = New-Object Microsoft.SqlServer.Management.Smo.Column($table, "Name",
$dt1)
$col1.Collation = "Latin1_General_CI_AS"
$col1.Nullable = $false

$table.Columns.Add($col1)
$table.Create()
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"Shay Levi" wrote:
Quote:

> Try
>
> http://www.databasejournal.com/featu...le.php/3691566
>
>
> Shay
> http://scriptolog.blogspot.com
>
>
>
Quote:

> > Hello,
> >
> > I am fairly new to Powershell and certainly not to familiar with SMO,
> > so really need some help with this problem I’ve encountered.
> >
> > I’m essentially trying to script the creation of a SQL Server 2005
> > database with the applicable entities using SMO. I have successfully
> > created a database using SMO, but not too sure on how to script the
> > creation of new entities and add them to a particular database. I have
> > searched high and low but can’t find any relevant information on the
> > subject.
> >
> > I have found a useful add-on called PowerSMO but this can only be used
> > for test purposes whereas I will be implementing my work into
> > production.
> >
> > I guess I’ll have to use something like
> > ‘Microsoft.SqlServer.Management.Smo.Table’ but I’m not entirely sure
> > of the syntax or how to implement this object.
> >
> > If anyone can answer my question or point me in the right direction
> > (any direction) I’d really appreciate it.
> >
> > Thanks,
> >
>
>
>
My System SpecsSystem Spec
Old 09-20-2007   #4 (permalink)
NJC


 
 

Re: Implementing SMO in Powershell

Thanks Shay and Rich for you quick replies!

Shay...
Thanks for the link but i'll already used that info to create my database.
Its about the only useful information out there on SMO in Powershell.

Rich...
Many Thanks and a huge thumbs up! The code snippet you've written works
great and given me a lot to play with

"RichS" wrote:
Quote:

> I got this to work. Obviously need to add more columns as required
>
> $null =
> [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo")
> $null =
> [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum")
> $null = [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
> $Server = new-object Microsoft.SqlServer.Management.Smo.Server("RSSQL1")
> $db = $server.Databases["Test"]
>
> $table = New-Object Microsoft.SqlServer.Management.Smo.Table($db, "NewTable")
>
>
> $dt1 = [Microsoft.SqlServer.Management.Smo.Datatype]::Nchar(50)
> $col1 = New-Object Microsoft.SqlServer.Management.Smo.Column($table, "Name",
> $dt1)
> $col1.Collation = "Latin1_General_CI_AS"
> $col1.Nullable = $false
>
> $table.Columns.Add($col1)
> $table.Create()
> --
> Richard Siddaway
> Please note that all scripts are supplied "as is" and with no warranty
> Blog: http://richardsiddaway.spaces.live.com/
> PowerShell User Group: http://www.get-psuguk.org.uk
>
>
> "Shay Levi" wrote:
>
Quote:

> > Try
> >
> > http://www.databasejournal.com/featu...le.php/3691566
> >
> >
> > Shay
> > http://scriptolog.blogspot.com
> >
> >
> >
Quote:

> > > Hello,
> > >
> > > I am fairly new to Powershell and certainly not to familiar with SMO,
> > > so really need some help with this problem I’ve encountered.
> > >
> > > I’m essentially trying to script the creation of a SQL Server 2005
> > > database with the applicable entities using SMO. I have successfully
> > > created a database using SMO, but not too sure on how to script the
> > > creation of new entities and add them to a particular database. I have
> > > searched high and low but can’t find any relevant information on the
> > > subject.
> > >
> > > I have found a useful add-on called PowerSMO but this can only be used
> > > for test purposes whereas I will be implementing my work into
> > > production.
> > >
> > > I guess I’ll have to use something like
> > > ‘Microsoft.SqlServer.Management.Smo.Table’ but I’m not entirely sure
> > > of the syntax or how to implement this object.
> > >
> > > If anyone can answer my question or point me in the right direction
> > > (any direction) I’d really appreciate it.
> > >
> > > Thanks,
> > >
> >
> >
> >
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Implementing Sql Object Dependency? .NET General
Implementing the Factory pattern .NET General
implementing get-item PowerShell
Implementing Unmanaged Interface in C# .NET General
Implementing SMO using Powershell (Part 2) PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46