View Single Post
Old 09-20-2007   #4 (permalink)
RichS


 
 

RE: Implementing SMO using Powershell (Part 2)

Glad to help. Learning PowerShell and the way to use various .NET features
in it is not an easy task.

be very interested in what else you are doing with SMO
--
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


"NJC" wrote:
Quote:

> Ah cool, works perfectly! Many thanks (again) RichS your really helping me
> out today. I'm still getting used to the Powershell syntax, so as i say your
> help has been much appreciated.
>
> "RichS" wrote:
>
Quote:

> > try
> >
> > $index.IndexKeyType =
> > [Microsoft.SqlServer.Management.Smo.IndexKeyType]:riPrimaryKey
> > --
> > 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
> >
> >
> > "NJC" wrote:
> >
Quote:

> > > Hello,
> > >
> > > I am trying to create a PK for an entity which is being instantiated and
> > > added using SMO in Powershell. However, I have found no useful information on
> > > this subject matter for Powershell so have decided to evaluate SMO in C# and
> > > then convert it to Powershell where possible. I know Powershell uses the .Net
> > > library so in theory the SMO object functionality should be the same.
> > >
> > > The following C# code snippet seems to work fine but I am having difficulty
> > > trying to convert it into Powershell. I’ve had a go but I am struggling how
> > > exactly to make the newly created index of value “DriPrimaryKey”.
> > >
> > > Please can someone have a look over my Powershell conversion and advise me
> > > where I’m going wrong?
> > >
> > > C#
> > > // Create a PK Index for the table
> > > Index index = new Index(newTable, "PK_TestTable");
> > > index.IndexKeyType = IndexKeyType.DriPrimaryKey;
> > >
> > > // The PK index will consist of 1 column, "ID"
> > > index.IndexedColumns.Add(new IndexedColumn(index,"ID"));
> > >
> > > // Add the new index to the table.
> > > newTable.Indexes.Add(index);
> > >
> > > // Physically create the table in the database
> > > newTable.Create();
> > >
> > >
> > > Powershell
> > > // Create a PK Index for the table
> > > $index = New-Object Microsoft.SqlServer.Management.Smo.Index($newTable,
> > > "PK_NewTable")
> > > index.IndexKeyType = IndexKeyType.DriPrimaryKey;????
> > >
> > > // The PK index will consist of 1 column, "ID"
> > > $indexedColumn = New-Object
> > > Microsoft.SqlServer.Management.Smo.IndexedColumn($index, "ID")
> > > $index.IndexedColumns.Add($indexedColumn)
> > >
> > > // Add the new index to the table.
> > > $apTable.Indexes.Add($index)
> > >
> > > Also, I am currently using PowershellAnalyzer as an IDE. Is this best IDE
> > > currently available for Powershell? Ideally it would be easier if there was
> > > IDE with intellisense but I haven’t as of yet come across one.
> > >
> > > Thanks,
> > >
My System SpecsSystem Spec