# 進行連線 if ($s.Version -eq$null ) {Throw"Can't find the instance $Datasource"} $db = $s.Databases[$Database] if ($db.name -ne$Database) {Throw"Can't find the database '$Database' in $Datasource"};
$transfer = new-object ("$My.Transfer") $db
# 匯出設定 $CreationScriptOptions = new-object ("$My.ScriptingOptions") # yes, we want these $CreationScriptOptions.ExtendedProperties = $true # and all the constraints $CreationScriptOptions.DRIAll = $true # Yup, these would be nice $CreationScriptOptions.Indexes = $true # This should be included when scripting a database $CreationScriptOptions.Triggers = $true # this only goes to the file $CreationScriptOptions.ScriptBatchTerminator = $true # of course $CreationScriptOptions.IncludeHeaders = $true; #no need of string output as well $CreationScriptOptions.ToFileOnly = $true # not necessary but it means the script can be more versatile $CreationScriptOptions.IncludeIfNotExists = $true $CreationScriptOptions.Filename = "$($FilePath)\$($Database)_$($today)_Build.sql"; $transfer = new-object ("$My.Transfer") $s.Databases[$Database]
# tell the transfer object of our preferences $transfer.options = $CreationScriptOptions $transfer.ScriptTransfer() "All done"