how to automate smo task to generate sql database object via powershell

Solutions on MaxInterview for how to automate smo task to generate sql database object via powershell by the best coders in the world

showing results for - "how to automate smo task to generate sql database object via powershell"
Alina
06 Jan 2020
1[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO')| out-null
2
3# Create an SMO connection to the instance
4$s = new-object ('Microsoft.SqlServer.Management.Smo.Server') "LOCALHOST\SQL2005_1" 
5
6$dbs = $s.Databases
7$dbs["Northwind"].Script() | Out-File D:\PSScripts\NWind.SQL
8
9$scrp = new-object ('Microsoft.SqlServer.Management.Smo.Scripter') ($s)
10
11$scrp.Options.AppendToFile = $True
12$scrp.Options.ClusteredIndexes = $True
13$scrp.Options.DriAll = $True
14$scrp.Options.ScriptDrops = $True
15$scrp.Options.IncludeIfNotExists = $True
16$scrp.Options.IncludeHeaders = $True
17$scrp.Options.ToFileOnly = $True
18$scrp.Options.Indexes = $True
19$scrp.Options.WithDependencies = $True
20
21$scrp.Options.FileName = "D:\PSScripts\NWind.SQL"
22$scrp.Script($dbs["Northwind"].Tables) 
23