sql-server - Powershell + SMO 问题:如果 ScriptTransfer 没有先运行一次,则 ScriptTransfer 会跳过 EXECUTE AS?
问题描述
在尝试编写数据库脚本时,我遇到了一个奇怪的结果。该脚本有效,但我看到没有包含在 CLR SP 上的 EXECUTE AS CALLER 的位置,除非 ScriptTransfer() 首先运行一次,即使它被重定向到空值。
- 对带有 SP 的数据库运行以下脚本(我们正在使用带有 EXECUTE AS CALLER 的过程,该过程调用带有 EXTERNAL NAME 的 CLR)。
- 用 ScriptTransfer 和 OUT-NULL 注释掉该行并更改文件名
- 再次运行
- 比较文件。你会看到一个有 EXECUTE AS CALLER 而另一个没有。
这是我正在运行的脚本:
import-module sqlserver
$sql_server = "myservername"
$SMOServer = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList "$sql_server"
$DBToScript = "mydbname"
$db = $SMOServer.Databases[$DBToScript]
$create_database = $db.Script() -replace("\d+KB","100mb")
##### And now the rest of the database
$transfer = new-object -TypeName Microsoft.SqlServer.Management.Smo.Transfer #-ArgumentList $db
$transfer.Database = $db
$transfer.CopyAllObjects = $true
$transfer.CopySchema = $true
$transfer.ScriptTransfer() |out-null # for whatever reason this is necessary to script out the WITH EXECUTE AS CALLER in a CLR SP
cls
$transfer.CopyAllObjects = $true
$transfer.CopySchema = $true
$transfer.CopyAllSchemas= $true
$transfer.Options.Indexes = $true
$transfer.Options.ScriptBatchTerminator = $true # this only goes to the file
$transfer.Options.filename = "c:\temp\backup\resultsa.sql"
$transfer.Options.ExtendedProperties= $true # yes, we want these
$transfer.Options.DRIAll= $true # and all the constraints
$transfer.Options.Indexes= $true # Yup, these would be nice
$transfer.Options.Triggers= $true # This should be included when scripting a database
$transfer.Options.ScriptBatchTerminator = $true # this only goes to the file
$transfer.Options.IncludeHeaders = $false #pshaw
$transfer.Options.ToFileOnly = $true #no need of string output as well
$transfer.Options.IncludeIfNotExists = $false # not necessary but it means the script can be more versatile
$transfer.ScriptTransfer()
解决方案
推荐阅读
- python - 运行 for 循环以求和不同字典的元素
- javascript - IE11 中的 javascript setInterval() 语法错误
- javascript - iframe 调整大小问题
- node.js - 保护 csrf Nodejs 表单
- vba - VBA 在两个不同的工作簿中复制和过去
- ios - '未找到 WebView' - 混合应用程序 - 量角器 - iPad 真实设备
- android - 如何刷新其活动的 MenuitemSelected 上的片段?
- ubuntu - 停止容器时 ubuntu 16.04 上的 docker 错误
- json - 遍历 JSON
- oop - 覆盖方法调用运算符或其他方法来捕获方法名称解析错误