通过power shell检索SSIS 2012 environmentvariable.name

时间:2012-08-09 23:48:18

标签: powershell ssis

我们正在使用power shell来部署我们的2012 SSIS包,并在SSIS 2012 Server上安装环境变量。现在在项目部署期间,我试图遍历环境变量集合中的每个变量(foreach($ environment.Variables中的$ variable))。那没问题。我可以看到“EnvironmentVariable [@Name ='something']”....但是尝试通过$ variable.Name或$ variable.Key从变量中检索名称(“something”)不起作用。我试过循环$ environment.Variables.Keys,但仍然没有。我的电源外壳技能有点弱,因为我过去几年一直在使用NANT,但是有些东西我只是没有看到吗?

提前致谢, 安东尼

添加现有电源shell脚本的片段。粗体$ variable.Name在CreateETLPackages任务中不起作用。从这个脚本中调用了很多设置和其他脚本,因此我没有包含所有内容。当在debug语句中返回$ variable.Name时,它返回“EnvironmentVariable [@Name ='something']”,正如我在原始帖子中指出的那样:

任务CreateSSISFolder -Depends CreateSSISCatalog {     if(!$ script:SSISCanBeDeployed){return}

# Create the project for the packages in the catalog
$catalog = $script:SSISCatalog
if ($catalog.Folders.Count -eq 0) {
    Write-Host "Creating folder $SSISFolderName ..."
    $script:SSISFolder = New-Object "Microsoft.SqlServer.Management.IntegrationServices.CatalogFolder" ($catalog, $SSISFolderName, "Folder for EDGE ETL packages")            
    $script:SSISFolder.Create()  
    Write-Host "... done"
} else {
    Write-Host "SSIS folder $SSISFolderName already exists; skipping create"
}

}

任务CreateSSISEnvironment --Depends CreateSSISFolder {     if(!$ script:SSISCanBeDeployed){return}

# Create the environment in the project
$folder = $script:SSISFolder
$environment = $folder.Environments[$SSISEnvironmentName]
if ($environment -eq $null) {
    # Create the environment
    Write-Host "Creating environment $SSISEnvironmentName ..."
    $environment = New-Object "Microsoft.SqlServer.Management.IntegrationServices.EnvironmentInfo" ($folder, $SSISEnvironmentName, "Environment to configure the SSIS packages")
    $environment.Create()
    Write-Host "... done"

    # Now create the variables (Constructor args: variable name, type, default value, sensitivity, description)
    $environment.Variables.Add("TestDatabase", [System.TypeCode]::String, "Data Source=$SSISServerName.TestDatabase;User ID=<USERNAME>;Password=<PASSWORD>;Initial Catalog=EdgeAviTrack;Provider=SQLNCLI11.1;Persist Security Info=True;Auto Translate=False;", $false, "Connection string for TestDatabase database")
    $environment.Alter()

} else {
    Write-Host "Environment $SSISEnvironmentName already exists; skipping create"
}

}

任务CreateETLPackages -Depends CreateSSISFolder,CreateSSISEnvironment {     if(!$ script:SSISCanBeDeployed){return}

# Get list of ETL .ispac files in the solution
$SSISProjects = GetListOfDeploymentFiles "*.ispac"
if ($SSISProjects -ne $null) {
    $folder = $script:SSISFolder
    $environment = $folder.Environments[$SSISEnvironmentName]
    if ($folder -ne $null) {
        foreach ($file in $SSISProjects) {
            # Read the ispac file, and deploy it to the folder            
            [byte[]] $projectFile = [System.IO.File]::ReadAllBytes($file.FullName)
            $nameParts = $file.Name.split(".")
            $curProjectName = [string]::join(".", $nameParts[0..($nameParts.length - 2)])
            Write-Debug "Deploying SSIS project $curProjectName"
            $project = $folder.DeployProject($curProjectName, $projectFile)

            if ($project.Status -ne "Success") {
                Write-Error "SSIS packages did not deploy correctly!"
            } else {
                # Get the full information set, rather than the short version returned from DeployProject
                $project = $folder.Projects[$curProjectName]
            }

            # Connect the project to the environment to stitch up all the connection strings
            if ($project.References.Item($SSISEnvironmentName, ".") -eq $null) {
                Write-Host "Adding environment reference to $SSISEnvironmentName ..."
                $project.References.Add($SSISEnvironmentName)
                $project.Alter()
                Write-Host "... done"
            }

            # Connect all the project parameters to the environment variables
            Write-Host "Adding connection string references to environment variables ..."

            foreach($varialble in $environment.Variables) {
                try {
                    $project.Parameters["CM." + **$varialble.Name**  + ".ConnectionString"].Set([Microsoft.SqlServer.Management.IntegrationServices.ParameterInfo+ParameterValueType]::Referenced, **$variable.Name**)
                }
                catch {
                    Write-Debug "Unable to set connection string **$variable.Name** on SSIS project $curProjectName"
                }
            }
            $project.Alter()
            Write-Host "... done"
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

好的,我发现了我的问题。看起来我正在尝试我需要使用$($ object.Name)来获取我需要的东西。感谢那些伸出援手的人。

谢谢, 安东尼