在PowerShell 2.0上运行SMO时出现异常

时间:2015-04-07 10:44:14

标签: sql-server powershell smo

我正在尝试生成数据库的SQL Server表脚本。我在Microsoft Windows Server 2008 R2上使用PowerShell v2和SMO。

这是我的示例代码......

param(
    [Parameter(Mandatory=$True)] [string[]]$server,
    [Parameter(Mandatory=$True)] [string[]]$user,
    [Parameter(Mandatory=$True)] [string[]]$password,
    [Parameter(Mandatory=$True)] [string[]]$database,
    [Parameter(Mandatory=$True)] [string[]]$output_path,
    [Parameter(Mandatory=$True)] [string[]]$DBSchema
)
$schema             = $dbSchema
$table_path         = "$output_path\"+$schema+"\"+$database+"\Table\"
$storedProcs_path   = "$output_path\"+$schema+"\"+$database+"\StoredProcedure\"
$views_path         = "$output_path\"+$schema+"\"+$database+"\View\"
$udfs_path          = "$output_path\"+$schema+"\"+$database+"\UserDefinedFunction\"
$textCatalog_path   = "$output_path\"+$schema+"\"+$database+"\FullTextCatalog\"
$udtts_path         = "$output_path\"+$schema+"\"+$database+"\UserDefinedTableTypes\"

$db         = New-Object ("Microsoft.SqlServer.Management.SMO.Database")
$tbl        = New-Object ("Microsoft.SqlServer.Management.SMO.Table")
$scripter   = New-Object ("Microsoft.SqlServer.Management.SMO.Scripter") ($server)

# Get the database and table objects
$db = $srv.Databases[$database]

$tbl            = $db.tables | Where-object { $_.schema -eq $schema  -and -not $_.IsSystemObject } 

function CopyObjectsToFiles($objects, $outDir) {

    if (-not (Test-Path $outDir)) {
        [System.IO.Directory]::CreateDirectory($outDir)
    }

    foreach ($o in $objects) { 

        if ($o -ne $null) {

            $schemaPrefix = ""

            if ($o.Schema -ne $null -and $o.Schema -ne "") {
                $schemaPrefix = $o.Schema + "."
            }

            $scripter.Options.FileName = $outDir + $schemaPrefix + $o.Name + ".sql"
            Write-Host "Writing " $scripter.Options.FileName
            $scripter.EnumScript($o)
        }
    }
}

CopyObjectsToFiles $tbl $table_path

我不知道自己哪里出错了。

Powershell 2.0中的EnumScript有问题吗?

enter image description here

我试图捕获异常消息。这是屏幕

enter image description here

enter image description here

0 个答案:

没有答案