我应该在前言中说我在OOP语言中使用脚本或编程的经验是有限的.
我正在研究一种使用PowerShell以编程方式创建和执行SSIS包的方法.不幸的是,PowerShell和SSIS可用的大部分资源都是用于从SSIS调用PS,而不是相反.
但是,我已经为VB / C#找到了许多用于创建SSIS包的资源.
我通过调用DTS / SSIS程序集成功地转换了大部分代码,但是现在在将TaskHost对象转换为主管道时失败了.
示例代码:
[Void][Reflection.Assembly]::LoadWithPartialName('Microsoft.sqlServer.ManagedDTS') [Void][Reflection.Assembly]::LoadWithPartialName('Microsoft.sqlserver.DTSPipelineWrap') # Create the Package and application,set its generic attributes $Package = New-Object Microsoft.sqlServer.Dts.Runtime.Package $Package.CreatorName = $CreatorName $App = New-Object Microsoft.sqlServer.Dts.Runtime.Application # Set connection info for our package $SourceConn = $package.Connections.Add("OLEDB") $SourceConn.Name = "Source Connection" $SourceConn.set_ConnectionString("Data Source=$SourceServer;Integrated Security=True") $TargetConn = $package.Connections.Add("OLEDB") $TargetConn.Name = "Target Connection" $TargetConn.set_ConnectionString("Data Source=$TargetServer;Integrated Security=True") # Build the tasks # Data Flow Task - actually move the table [Microsoft.sqlServer.DTS.Runtime.Executable]$XferTask = $Package.Executables.Add("STOCK:PipelineTask") $XferTaskTH = [Microsoft.sqlServer.Dts.Runtime.TaskHost]$XferTask $XferTaskTH.Name = "DataFlow" $XferTaskTH.Description = "Dataflow Task Host" $DataPipe = [Microsoft.sqlServer.DTS.pipeline.Wrapper.MainPipeClass]($XferTaskTH.InnerObject)
当我收到错误时,一切正常,直到最后一行:
Cannot convert the
“System.__ComObject” value of type
“System.__ComObject#{}” to
type
“Microsoft.sqlServer.Dts.Pipeline.Wrapper.MainPipeClass”
欢迎任何帮助或想法!
解决方法
Microsoft.sqlServer.DTSPipelineWrap大量使用COM实例.
这篇论坛帖子建议使用CreateWRapperOfType方法:
http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/0f493a31-fbf0-46ac-a6a5-8a10af8822cf/
你可以试试这个:
$DataPipe = [System.Runtime.InteropServices.Marshal]::CreateWrapperOfType($XferTaskTH.InnerObject,[Microsoft.sqlServer.DTS.pipeline.Wrapper.MainPipeClass])
不会出错并产生一个对象 – 我不确定是什么类型.