使用dtutil部署SSIS包

时间:2017-05-24 08:08:19

标签: powershell ssis

我正在尝试使用dtutil部署一个包,但是我收到的错误是:

Option "-xxxx" is not valid.

这与我提供的SOURCESERVER参数相关,其中有一个夸大其词。

$conString = "xxx-xxxx";
$dtUtilQueryToExecute = '/SOURCESERVER "' + $conString + '" /sql "my path" /exists';
$result = dtutil $dtUtilQueryToExecute

我认为dtutil在到达连字符时期望一个新参数,并且即使它是双引号/单引号也无法转义连字符。

我使用dtutil的原因是我需要能够从一个位置将同一个软件包部署到许多其他服务器。

因此,我需要弄清楚如何在服务器名称中转义连字符,或者我需要使用替代部署方法。

如何逃避连字符,或者我可以使用哪种替代部署方法?

2 个答案:

答案 0 :(得分:1)

我相信PowerShell正在解释变量中的连字符。看起来互联网的智慧暗示了像

这样的排列
  • $ conString =' xxx-xxxx'
  • $ conString =" xxx`-xxxx"

此外,PowerShell

中不需要使用尾随分号

答案 1 :(得分:0)

作为替代方法,为什么不将SMO与Powershell一起使用。这样,您可以创建一个脚本来部署您的包,还可以创建foders,项目,环境和SQL Agent任务。

以下是我将使用的一个例子:

$here = Split-Path -Parent $MyInvocation.MyCommand.Path

$target_server_name = 'target_server_name'
$source_server_name = 'source_server_name'
$target_database_name = 'target_database_name'
$source_database_name = 'source_database_name'
$environment_name = 'enviornment_name'
$folder_name = 'folder_name'
$project_name = 'project_name'
$ispac_name = 'ispac_name'

$variables = @{
    email_alert = 'xx@xx.com'
    email_error = 'xx@xx.com'
    email_reply = 'noreply@xx.com'
    smtp_server = 'outlook.xx.local'
    tax_rate = '20'
}

function Write-Message ($msg) {
    Write-Host ' [+] ' -ForegroundColor Yellow -NoNewline
    Write-Host $msg
}

Write-Host 'Starting deployment' -ForegroundColor DarkGray

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Management.IntegrationServices") | Out-Null

Write-Message ("Connecting to integration services on '$source_server_name'")
$conn_str = "Data Source={0};Initial Catalog=master;Integrated Security=SSPI;" -f $source_server_name
$conn = New-Object System.Data.SqlClient.SqlConnection $conn_str

$namespace = 'Microsoft.SqlServer.Management.IntegrationServices'
$intg_serv = New-Object "$namespace.IntegrationServices" $conn

$catalog = $intg_serv.Catalogs['SSISDB']

$folder = $catalog.Folders[$folder_name]
if(!$folder) {
    Write-Message 'Creating folder ...'
    $folder = New-Object "$($namespace).CatalogFolder" ($catalog, $Folder_name, $Folder_name)            
    $folder.Create()  
} else {
    Write-Message 'Folder found ...'
}

Write-Message 'Deploying project file ...'

$path = "$here\..\path_to_your_project\bin\Development\$($ispac_name).ispac"
if(Test-Path $path) {Copy-Item (Resolve-Path $path) $here}

[byte[]]$project_file = [System.IO.File]::ReadAllBytes("$here\crm_imports.ispac")
$dummy = $folder.DeployProject($project_name, $project_file)
$project = $folder.Projects[$project_name]

Write-Message 'Removing old environment ...'
$old = $folder.Environments | Select -ExpandProperty name 
$old | % {$folder.Environments[$_].Drop()}

Write-Message "Creating environment '$envionment_name'"
$environment = New-Object "$namespace.EnvironmentInfo" ($folder, $environment_name, $environment_name)
$environment.Create()

$variables.GetEnumerator() | % {
    $n = $_.Name
    $v = $_.Value
    Write-Message "  -> adding environment variable $($n): $v" 
    $environment.Variables.Add($n,[System.TypeCode]::String, $v, $false, $n)
    $environment.Alter()
    $project.Packages | % {
        if($_.Parameters[$n] -ne $null)
        {
            $_.Parameters[$n].Set([Microsoft.SqlServer.Management.IntegrationServices.ParameterInfo+ParameterValueType]::Referenced, $n)
            $_.Alter()
        }
    }
}

$reference = $project.References[$environment.name, $folder.Name]

if(!$reference) {
    $project.References.Add($environment.name, $folder.Name)
    $project.Alter()
}

$environment_ref_id = $project.References[0].ReferenceId

$job_name = 'job_name'
$server = New-Object microsoft.sqlserver.management.smo.server $source_server_name
$Job_server = $server.JobServer
$job = $job_server.Jobs[$job_name]
if($job) {$job.Drop()}

Write-Message "Creating SQL agent job '$job_name'"
$job = New-Object Microsoft.SqlServer.Management.SMO.Agent.Job -ArgumentList $Job_server, $job_name
$job.Create()
$job.OwnerLoginName = 'sa'
$job.ApplyToTargetServer($source_server_name)
$job.Alter()

$job_step = New-Object Microsoft.SqlServer.Management.SMO.Agent.JobStep -ArgumentList $job, 'Run SSIS import_donations package'
$job_step.SubSystem = 'Ssis'
$job_step.command = ('/ISSERVER "\"\SSISDB\{3}\{4}\{5}.dtsx\"" ' +
    '/SERVER {0} /ENVREFERENCE {1} ' + 
    '/Par "\"CM.source.ConnectionString\"";' + 
    '"\"Data Source={0};Initial Catalog={6};Integrated Security=SSPI;Application Name=my_app;\"" ' + 
    '/Par "\"CM.source.InitialCatalog\"";"\"{6}\"" ' +
    '/Par "\"CM.source.ServerName\"";{0} ' + 
    '/Par "\"CM.target.ConnectionString\"";' +
    '"\"Data Source={2};Initial Catalog={7};Integrated Security=SSPI;Application Name=my_app;\"" ' +
    '/Par "\"CM.target.InitialCatalog\"";"\"{7}\"" ' +
    '/Par "\"CM.target.ServerName\"";{2} ' +
    '/Par "\"$ServerOption::LOGGING_LEVEL(Int16)\"";3 ' +
    '/Par "\"$ServerOption::SYNCHRONIZED(Boolean)\"";True /CALLERINFO SQLAGENT /REPORTING E') -f $source_server_name, $reference.ReferenceId, $target_server_name, $folder_name, $project_name, $ispac_name, $source_database_name, $target_database_name
$job_step.Create()

$conn.Close()