I am trying to write a Powershell script to generate a SQL script that creates the stored procedures in my database. Here is what I have:
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO')| out-null
Function to-array ($c)
{
foreach($item in $c)
{
$result += @($item)
}
$result
}
$s = new-object ('Microsoft.SqlServer.Management.Smo.Server') "LOCALHOST"
$scrp = new-object ('Microsoft.SqlServer.Management.Smo.Scripter') ($s)
$dbs = $s.Databases
$fa = $dbs["FinancialAid"]
$scrp.Options.ScriptData = $True
$scrp.Options.AllowSystemObjects = $False
$scrp.Options.FileName = 'StoredProcedures.sql'
$scrp.Options.ToFileOnly = $True
# Finally the stored procedures
$sparray = to-array($fa.StoredProcedures)
$scrp.EnumScript($sparray)
This fails with the error:
Exception calling "EnumScript" with "1" argument(s): "Script failed for Server 'LOCALHOST'. "
At H:\Visual Studio 2012\FinancialAidApplication\Financial Aid Application\FinancialAidApplication\main\src\FinancialAidApp
\SQL-Scripts\sp.ps1:28 char:17
+ $scrp.EnumScript <<<< ($sparray)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Looking at the output file, I see that it does have my stored procedures at the beginning, which is great, but then it has a bunch of system procedures. The last one is sys.sp_fulltext_table
.
As I already specified $scrp.Options.AllowSystemObjects = $False
I don't understand why these system procedures are being included. I would like to get rid of the system procedures and the error message.