Powershell SQL 服务器存储过程备份

Powershell SQL Server stored procedure backup

我试图通过从 Python 程序传递参数来从 SQL 服务器数据库备份一个特定的存储过程。这是我试过的代码,但我一直收到错误。

param([string]$server='dbsed0898', [string]$dbname='global_hub',[string]$sp='dbo.gs_eligibility')

[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SqlServer.SMO”) | out-null

$SMOserver = 'Microsoft.SqlServer.Management.Smo' #-argumentlist $server
$srv = New-Object("$SMOserver.Server") $server
$db = $srv.databases[$dbname]

$Objects = $db.storedprocedures[$sp]
$scripter = new-object ("$SMOserver.Scripter") $srv

$Scripter.Script($Objects) | Out-File 
" C:\Users\fthoma15\Documents\backup_03212020.sql"

$db = $SMOserver.databases[$dbname]

$Objects = $db.storedprocedures[$sp]
$Scripter.Script($Objects) | Out-File 

"C:\Users\fthoma15\Documents\backup_03212020.sql"

错误:

Multiple ambiguous overloads found for "Script" and the argument count: "1".
At line:12 char:5
+ $Scripter.Script($Objects) | Out-File "C:\Users\fthoma15\Document ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest

有人可以帮助我吗?

这是我做的。

param([string]$server='test', [string]$dbname='test',[string[]]$sp=('test','test'))

[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SqlServer.SMO”) | out- 
null

$SMOserver = new-object ("Microsoft.SqlServer.Management.Smo.Scripter") #-argumentlist 
$server
$srv = new-Object Microsoft.SqlServer.Management.Smo.Server("$server")
$db = New-Object Microsoft.SqlServer.Management.Smo.Database
$db = $srv.Databases.Item("$dbname")
$Objects = $db.storedprocedures[$sp[1,3]]
$scripter = new-object ("$SMOserver") $srv

$Scripter.Script($Objects) | Out-File 
"C:\Users\fthoma15\Documents\backup_03212020.sql"

按照 AlwaysLearning 的建议,我将 sp 变量更改为数组列表,拆分了模式和 sp 名称。