データベースからスクリプトを生成しようとしていますが、それらのオブジェクトにわずかな変更を加えています。たとえば、ストアド プロシージャのスクリプトを生成したいのですが、別のスキーマでスクリプトを作成する必要があります。
私が現在試していることの例を以下に示します。私が完全に間違った方向に進んでいる場合は、正しい方向に向けてください。あらゆる種類の文字列検索と置換を行うことは避けたいと考えています。これには多くの潜在的な危険があるためです。
私がやろうとしたことは、データベース内の既存の関数から関数オブジェクトを作成することです。次に、新しい関数を作成して、作成したばかりの関数のコピーを作成しようとしました。スキーマはメモリ内にある必要があり、データベースに関連付けられていないため、スキーマを変更できるようにするために必要なようです。次に、スキーマを変更してスクリプト化してみます。残念ながら、PowerShell でエラーが発生しない .Script の構文を見つけることができませんでした。
$instance = $args[0] # Gets the instance name from the command line parameters
$database = $args[1] # Gets the database name from the command line parameters
# Import the SMO assembly
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
# Create an instance for the SQL Server
$serverInstance = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $instance
# Create an instance for the Database
$databaseInstance = $serverInstance.Databases[$database]
# Loop through all of the user defined functions (that are not system objects) in the database
foreach ($function in $databaseInstance.UserDefinedFunctions | Where-Object {$_.IsSystemObject -eq $False})
{
$newFunction = New-Object ('Microsoft.SqlServer.Management.Smo.UserDefinedFunction') ($databaseInstance, $function.Name, "tnf_dbo_func")
$newFunction.TextHeader = $function.TextHeader
$newFunction.TextBody = $function.TextBody
$newFunction.TextMode = $False
$newFunction.ChangeSchema("tnf_dbo_func")
$newFunction.TextMode = $True
Write-Host $newFunction.Schema
Write-Host $newFunction.TextHeader
Write-Host $newFunction.TextBody
$newFunction.Script()
}