这是一个powershell脚本:
[string] $server = "devserver\mssql"; # SQL Server Instance
[string] $database = "ftg"; # Database with the tables to script out.
[string] $folder = "d:\FT\FTProject\"; # Path to export to
[string] $SQLLogin = "Sa";
[string] $SQLPass = "Sa1234";
# Reference to SMO
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO');
Write-Output ((Get-Date -format yyyy-MM-dd_HH-mm-ss) + ": Started ...");
$con = New-Object Microsoft.SqlServer.Management.Common.ServerConnection($server, $SQLLogin, $SQLPass);
$srv = New-Object Microsoft.SqlServer.Management.SMO.Server($con);
$db = New-Object Microsoft.SqlServer.Management.SMO.Database($srv, $database);
#Use SMO Transfer Class by specifying source database
#you can specify properties you want either brought over or excluded, when the copy happens
$ObjTransfer = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Transfer;
$ObjTransfer.Database = $db
最后一行出现错误:
异常设置“数据库”:“无法将类型“Microsoft.SqlServer.Management.Smo.Database”的“[ftg]”值转换为类型“Microsoft.SqlServer.Management.Smo.Database”。在 D:\FT\FTProject\Setup\transfer.ps1:31 char:1 + $ObjTransfer.Database = [Microsoft.SqlServer.Management.SMO.Database] $db + ~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], SetValueInvocationException + FullyQualifiedErrorId : ExceptionWhenSetting
我还尝试将 $db 设置如下:
$db = $srv.Databases[$database]
怎么了?