`
java-mans
  • 浏览: 11416106 次
文章分类
社区版块
存档分类
最新评论

SQL SERVER 2012 SSIS 之 POWERSHELL

 
阅读更多

一,连接服务器,执行PACKAGE

1,建立连接

2,取回对象

3,创造执行对象

4,调度计划,执行PACKAGE

# Load the IntegrationServices Assembly            
$loadStatus = [Reflection.Assembly]::Load("Microsoft"+            
".SqlServer.Management.IntegrationServices" +            
", Version=11.0.0.0, Culture=neutral" +            
", PublicKeyToken=89845dcd8080cc91")            
            
# Store the IntegrationServices Assembly namespace to avoid typing it every time            
$ISNamespace = "Microsoft.SqlServer.Management.IntegrationServices"            
            
Write-Host "Connecting to server ..."            
            
# Create a connection to the server            
$constr = "Data Source=localhost;Initial Catalog=master;Integrated Security=SSPI;"            
            
$con = New-Object System.Data.SqlClient.SqlConnection $constr            
            
# Create the Integration Services object            
$ssis = New-Object $ISNamespace".IntegrationServices" $con            
            
## Drop the existing catalog if it exists            
# Write-Host "Removing previous catalog ..."            
# if ($ssis.Catalogs.Count -gt 0)             
# {             
#    $ssis.Catalogs["SSISDB"].Drop()             
# }            
            
# Provision a new SSIS Catalog            
Write-Host "Creating new SSISDB Catalog ..."            
$cat = New-Object $ISNamespace".Catalog" ($ssis, "SSISDB", "#PASSWORD1")            
$cat.Create()            
            
# Create a new folder            
Write-Host "Creating Folder ..."            
$folder = New-Object $ISNamespace".CatalogFolder" ($cat, "Folder", "Description")            
$folder.Create()            
            
# Read the project file, and deploy it to the folder            
Write-Host "Deploying ExecutionDemo project ..."            
[byte[]] $projectFile = [System.IO.File]::ReadAllBytes("C:\Demos\Demo.ispac")            
$folder.DeployProject("ExecutionDemo", $projectFile)            
            
# Run the package                        
Write-Host "Running package ..."                        
                        
# When executing, we need to specify two parameters                        
# 1 arg is a bool representing whether we want to run             
# 32bit runtime on 64 bit server                        
# 2 arg is a reference to an environment if this package depends on it                        
$executionId = $package.Execute("false", $null)                        
                        
Write-Host "Package Execution ID: " $executionId    


二,执行参数复杂PACKAGE

1,重复上述步骤,调度计划

2,设置常量或环境参数

# Load the IntegrationServices Assembly            
$loadStatus = [Reflection.Assembly]::Load("Microsoft"+            
".SqlServer.Management.IntegrationServices" +            
", Version=11.0.0.0, Culture=neutral" +            
", PublicKeyToken=89845dcd8080cc91")            
            
# Store the IntegrationServices Assembly namespace to avoid typing it every time            
$ISNamespace = "Microsoft.SqlServer.Management.IntegrationServices"            
            
Write-Host "Connecting to server ..."            
            
# Create a connection to the server            
$constr = "Data Source=localhost;Initial Catalog=master;Integrated Security=SSPI;"            
            
$con = New-Object System.Data.SqlClient.SqlConnection $constr            
            
# Create the Integration Services object            
$ssis = New-Object $ISNamespace".IntegrationServices" $con            
            
## Drop the existing catalog if it exists            
# Write-Host "Removing previous catalog ..."            
# if ($ssis.Catalogs.Count -gt 0)             
# {             
#    $ssis.Catalogs["SSISDB"].Drop()             
# }            
            
# Provision a new SSIS Catalog            
Write-Host "Creating new SSISDB Catalog ..."            
$cat = New-Object $ISNamespace".Catalog" ($ssis, "SSISDB", "#PASSWORD1")            
$cat.Create()            
            
# Create a new folder            
Write-Host "Creating Folder ..."            
$folder = New-Object $ISNamespace".CatalogFolder" ($cat, "Folder", "Description")            
$folder.Create()            
            
# Read the project file, and deploy it to the folder            
Write-Host "Deploying ExecutionDemo project ..."            
[byte[]] $projectFile = [System.IO.File]::ReadAllBytes("C:\Demos\Demo.ispac")            
$folder.DeployProject("ExecutionDemo", $projectFile)            
            
#### NEW STUFF STARTS FROM HERE ####            
            
# we can specify the value of parameters to be either constants or             
# to take the value from  environment variables            
            
$package = $project.Packages[“ComplexPackage.dtsx”]            
            
# setting value of parameter to constant            
$package.Parameters["Servername"].Set(            
    [Microsoft.SqlServer.Management.IntegrationServices.ParameterInfo+ParameterValueType]::Literal,            
    "Foobar");            
$package.Alter()            
            
# binding value of parameter to value of an env variable is a little more complex            
# 1) create environment            
# 2) add variable to environment            
# 3) make project refer to this environment            
# 4) make package parameter refer to this environment variable            
# These steps are shown below            
            
# 1) creating an environment            
$environment = New-Object $ISNamespace".EnvironmentInfo" ($folder, “Env1”, “Env1 Desc.”)            
$environment.Create()            
            
# 2) adding variable to our environment             
# Constructor args: variable name, type, default value, sensitivity, description            
$environment.Variables.Add(“Variable1”, [System.TypeCode]::Int32, “10”, “false”, “Desc.”)            
$environment.Alter()            
            
# 3) making project refer to this environment            
$project = $folder.Projects[$SSISProjectName]            
$project.References.Add($SSISEnv, $folder.Name)            
$project.Alter()            
            
# 4) making package parameter refer to this  environment variable            
$package.Parameters["CoolParam"].Set(            
    [Microsoft.SqlServer.Management.IntegrationServices.ParameterInfo+ParameterValueType]::Referenced,            
    $SSISEnvVar)            
$package.Alter()            
            
# retrieving environment reference            
$environmentReference = $project.References.Item($SSISEnv, $folder.Name)            
$environmentReference.Refresh()            
            
# executing with environment reference – Note: if you don’t have any env reference,            
# then you specify null as the second argument            
$package.Execute("false", $environmentReference)            
            
Write-Host "Package Execution ID: " $executionId            
            


分享到:
评论

相关推荐

    SQL Server 2008高级程序设计 4/6

     2.5 SQL Server集成服务(SSIS)  2.6 Reporting Services  2.7 BulkCopy Program(bcp)  2.8 SQL Server Profiler  2.9 sqlcmd  2.10 小结 第3章 提出更好的问题:高级查询  3.1 子查询概述  3.2 ...

    SQL Server 2008高级程序设计 2/6

     2.5 SQL Server集成服务(SSIS)  2.6 Reporting Services  2.7 BulkCopy Program(bcp)  2.8 SQL Server Profiler  2.9 sqlcmd  2.10 小结 第3章 提出更好的问题:高级查询  3.1 子查询概述  3.2 ...

    SQL Server 2008高级程序设计 3/6

     2.5 SQL Server集成服务(SSIS)  2.6 Reporting Services  2.7 BulkCopy Program(bcp)  2.8 SQL Server Profiler  2.9 sqlcmd  2.10 小结 第3章 提出更好的问题:高级查询  3.1 子查询概述  3.2 ...

    SQL Server 2008高级程序设计 5/6

     2.5 SQL Server集成服务(SSIS)  2.6 Reporting Services  2.7 BulkCopy Program(bcp)  2.8 SQL Server Profiler  2.9 sqlcmd  2.10 小结 第3章 提出更好的问题:高级查询  3.1 子查询概述  3.2 ...

    SQL Server 2008高级程序设计 6/6

     2.5 SQL Server集成服务(SSIS)  2.6 Reporting Services  2.7 BulkCopy Program(bcp)  2.8 SQL Server Profiler  2.9 sqlcmd  2.10 小结 第3章 提出更好的问题:高级查询  3.1 子查询概述  3.2 ...

    SQL Server 2008高级程序设计 1/6

     2.5 SQL Server集成服务(SSIS)  2.6 Reporting Services  2.7 BulkCopy Program(bcp)  2.8 SQL Server Profiler  2.9 sqlcmd  2.10 小结 第3章 提出更好的问题:高级查询  3.1 子查询概述  3.2 ...

    PSIS:用于 SqlServer 的基于 Powershell 的工具 - 并行批量加载、并行星型模式填充、数据库源控制和迁移

    对于 ETL,我们专注于在没有 SSIS 的情况下完成最常见的 ETL 任务。 支持以下 ELT 功能 批量数据传输 通过 ADO.Net 来自 Excel 从 IEnumberable 单通道、多线程星型模式填充器 将数据提取到 .csv 文件 遵循构建...

    smo的matlab代码-SQLPSX:SQLPowerShell扩展

    SQLServer - 导入模块代理 - 导入模块 Repl - 导入模块 SSIS - 导入模块 SQLParser - 导入模块 Showmbrs - 导入模块 adolib - 导入模块 sqlmaint - 导入模块 sqlise -导入模块 oracleise 应该在 sqlps mini-shell 中...

    Sams.Teach.Yourself.Big.Data.Analytics.with.Microsoft.HDInsight

    Using Sqoop or SSIS (SQL Server Integration Services) to move data to/from HDInsight and build data integration workflows for transferring data Using Oozie for scheduling, co-ordination and managing ...

    MSSQL-CICD-帮助器

    MSSQL-CICD-帮助器

Global site tag (gtag.js) - Google Analytics