Powershell script to script out database objects.
1.Create the below powershell file(.ps1). This powershell file takes servername,database name and type of object(tables/stored procedures/functions..) you want to script out as input parameters. The output will be written to a file.
param(
[string]$Server,
[string]$db,
[string]$Object
)
import-module sqlps
$a = new-object microsoft.sqlserver.management.smo.server $Server
$db1=$a.databases|where-object {$_.Name -like $db} | select-object $object
$db1.$object|where-object {$_.issystemobject -like “False”}|foreach {$_.script()} |out-file “c:\users\sqlwhisper\Desktop\Scripts.txt” -append
Syntax : ./Scripts.ps1 “servername” “databasename” “tables”
Example : ./Scripts.ps1 “anulu” “yahoo” “tables” – This will script out all the tables from yahoo db on anulu server.