Retrieve value from powershell script in jenkins file - powershell

I try to retrieve a string from a powershell script and use it in Jenkins file, in order to change the displayed version in SonarQube.
I started to implement functionality for taking the project version from package.json. Basically I give a Workspace directory as param and I ask for the full path if he finds a package.json in Workspace itself or in child folders. If the file is found, parse it and return version:
updateDisplayedVersionInSonar.ps1
param (
[Parameter(Mandatory = $true)]
[string]$Workspace
)
try{
$packageFullPath = ""
$pcgVersion = ""
Get-ChildItem -Path ${Workspace} -Filter package.json
-Recurse -ErrorAction SilentlyContinue -Force | % {$packageFullPath = $_.FullName}
try {
Test-Path $packageFullPath -PathType leaf
$json = Get-Content $packageFullPath | Out-String | ConvertFrom-Json
if($json.PSobject.Properties.Name -contains "version"){
$pcgVersion = $json.version
}
else{
$pcgVersion = "unknown"
}
Write-Output $pcgVersion
}
catch {
Write-Output "There is no package.json file!"
}
}
catch{
$ErrorMessage = $_.Exception.Message
write-host "An error has occured: ${ErrorMessage}"
exit 1
}
Now I want to use the version returned from ps script in a Jenkins file:
stage('SonarQube Frontend') {
environment {
sonarqubeScannerHome = tool name: 'SonarQube Scanner', type: hudson.plugins.sonar.SonarRunnerInstallation'
sonarQubeId = 'SonarQubeServer'
sonarProjectName = "\"SPACE ${REPOSITORY_NAME}\""
sonarProjectKey = "${REPOSITORY_NAME}"
testsPaths = 'app/myProject-ui/webapp/TEST/unit/utils'
testExecutionReportPaths = 'app/myProject-ui/reports/sonar/TESTS-qunit.xml'
javascriptLcovReportPaths = 'app/myProject-ui/reports/coverage/lcov.info'
}
steps {
withSonarQubeEnv(env.sonarQubeId) {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: sonarQubeId, usernameVariable: 'SONAR_USER', passwordVariable: 'SONAR_PASSWORD']]) {
script{
sonarProperties = " -Dsonar.projectName=${env.sonarProjectName}" +
" -Dsonar.projectKey=${env.sonarProjectKey}" +
" -Dsonar.login=${SONAR_USER}" +
" -Dsonar.password=${SONAR_PASSWORD}" +
" -Dsonar.sources=./" +
" -Dsonar.exclusions=**/*.java"
//some other conditions
//this line will be executed and i will see in Jenkins Console output the version found in package.json
powershell "powershell -File C:/Automation/updateDisplayedVersionInSonar.ps1 -Workspace '${env.WORKSPACE}/app'"
//I try to make it as a variable, but it will print "echo version here - null -" :(
pcgVersion = powershell "powershell -File C:/Automation/updateDisplayedVersionInSonar.ps1 -Workspace '${env.WORKSPACE}/app'"
echo "echo version here - ${pcgVersion} -"
//I want to use it here in order to be displayed the correct version of the app in Sonar
sonarProperties = sonarProperties + " -Dsonar.projectVersion= ${pcgVersion}"
}
bat "${env.sonarqubeScannerHome}/bin/sonar-scanner" + " -Dsonar.host.url=${SONAR_HOST_URL}" + sonarProperties
}
}
}
} //end of SonarQube Frontend stage
I tried the solution from How to execute powershell script from jenkins by passing parameters but without any result.
I tried also to do this way:
version = powershell(returnStdout: true, script:'powershell -File C:/SCP/Automation/updateDisplayedVersionInSonar.ps1 -Workspace "${env.WORKSPACE}"')
version = powershell(returnStdout: true, script:'C:/SCP/Automation/updateDisplayedVersionInSonar.ps1 -Workspace "${env.WORKSPACE}"')
I found quite a lot of examples of how to use Jenkins variable in Powershell, but not vice versa :(
What I am doing wrong? It is possible to achieve this?
Thank you!

You could use Jenkins Pipeline Utility instead.
def getPackageVersion() {
def package = readJSON file: '${env.WORKSPACE}/app/package.json'
echo package.version
return package.version
}
Then you should be able to access it like this.
def pcgVersion = getPackageVersion()

Related

Powershell NUnit console seems to hang after tests via Octopus/Powershell (Chrome driver still open)

I seem to have an issue when running a PowerShell script as a part of a step in our deployment Octopus pipeline. The tests run against a few websites, and each website has a range of tests. These are just locally setup websites on our environment.
The website with the most tests runs has about 100 tests, and the smallest website is about 5. The issue can occur with the website with 5 or 100. These are SpecFlow tests.
The results are then exported to an xml file. According to the xml file it does look like it has ran all the tests, as it shows the ones that succeeded and failed. It doesn't seem like this issue always occurs. Although it happens if I leave it on the nightly build. It won't always occur if I trigger a manual build on Octopus.
The version of NUnit is 3.1.1
Here is so far an example line of what I am passing in powershell for the NUnit console.
param(
[string]$configurationDescription = $OctopusParameters['environment.Transform'] + " Env Test Run",
[string]$site,
[string]$environmentName,
[string]$environmentSiteTag,
[string]$testDllName,
[string]$testDllFolder
)
$folderPath = $OctopusParameters['tests.rootFolder']
Set-Location $folderPath
$configurationDescription = $configurationDescription.Replace(" ", "-")
$testResultsFolderName = "testResults"
$testResultsPath = "$folderPath\$testResultsFolderName\$site"
$currentTimeFormatted = Get-Date -Format "dd-MM-yyyy_hh_mm_ss"
if(!(Test-Path -Path $testResultsPath))
{
New-Item -Path $testResultsPath -ItemType Directory
}
$testDllPath = $folderPath + "\tests\$testDllFolder\$testDllName"
$environmentWipTag = $OctopusParameters["tests.environment.wipname"]
#Change the location of "bin\XXX" So it will pick up the respective dll file and use the respective App.XXX.config
$testResultsXmlPath = "$testResultsPath\TestResult_$currentTimeFormatted.xml"
$args = "$testDllPath --where `"cat != $environmentWipTag && (cat == $environmentName && cat == $environmentSiteTag)`" --result=$testResultsXmlPath --framework=net-4.0 --timeout=20000"
# SECTION: Update Chrome driver
# Update Chrome Driver for testing to match the machine.
#1. Get the version of Chrome driver installed.
$chromeDriverOutputPath = $folderPath + "\tests\$testDllFolder"
# Store original preference to revert back later
$OriginalProgressPreference = $ProgressPreference;
# Increases the performance of downloading the ChromeDriver.
$ProgressPreference = 'SilentlyContinue';
try
{
$chromeVersion = (Get-Item (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe').'(Default)').VersionInfo.FileVersion
$chromeVersion = $chromeVersion.Substring(0, $chromeVersion.LastIndexOf("."));
} catch
{
"Could not find Google Chrome in the registry."
}
#2. Get the latest version of chrome driver available.
$chromeDriverVersion = (Invoke-WebRequest "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_$chromeVersion" -UseBasicParsing).Content;
Write-Host "Latest matching version of Chrome Driver is $chromeDriverVersion";
#3.Create a temp folder to store the chrome driver to be downloaded.
$tempFilePath = [System.IO.Path]::GetTempFileName();
$tempZipFilePath = $tempFilePath.Replace(".tmp", ".zip");
Rename-Item -Path $tempFilePath -NewName $tempZipFilePath;
$tempFileUnzipPath = $tempFilePath.Replace(".tmp", "");
# 4. Download correct chrome drive, unzip the archive and move the chromedriver to the correct location.
Invoke-WebRequest "https://chromedriver.storage.googleapis.com/$chromeDriverVersion/chromedriver_win32.zip" -OutFile $tempZipFilePath -UseBasicParsing;
Expand-Archive $tempZipFilePath -DestinationPath $tempFileUnzipPath;
Move-Item "$tempFileUnzipPath/chromedriver.exe" -Destination $chromeDriverOutputPath -Force;
# 5. Clean up files.
Remove-Item $tempZipFilePath;
Remove-Item $tempFileUnzipPath -Recurse;
# Set back to default ProgressPreference.
$ProgressPreference = $OriginalProgressPreference
#END SECTION: Update Chrome driver
Write-Host "Chrome Driver now matches the version installed on the machine. Beginning Test run."
#Begin Test Run.
$nunitRunnerResult = (Start-Process -FilePath "$folderPath\runner\nunit3-console.exe" -ArgumentList $args -PassThru -Wait).ExitCode
if(!(Test-Path -Path $testResultsXmlPath))
{
Write-Host "Args:$args FilePath:$folderPath\runner\nunit3-console.exe"
Write-Error "Unable to find $testResultsXmlPath"
return;
}
$testsTestResultHtmlPagePath = "$testResultsPath\$configurationDescription-$currentTimeFormatted\ExtentReport"
$args = "-i $testResultsXmlPath -o $testsTestResultHtmlPagePath -r html"
Start-Process -FilePath "$folderPath\extentreport\extent.exe" -ArgumentList $args -Wait
$extentReportPath = $testsTestResultHtmlPagePath + "\index.html"
$extentSummaryPath = $testsTestResultHtmlPagePath + "\dashboard.html"
$testsTestResultPath = "$testResultsFolderName\testsTestResult-$currentTimeFormatted.xml"
$args = [string]::Format("-f=Features/ -o=$testResultsFolderName/{0}-{1}/PicklesReport -trfmt=nunit3 -df=dhtml -lr={2}", $configurationDescription, $currentTimeFormatted, $testsTestResultPath)
Exit 0
The C# ran after each test.
using BoDi;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Configuration;
using TechTalk.SpecFlow;
namespace MyTestFramework.Helpers
{
[Binding]
public sealed class Hooks
{
private readonly IObjectContainer _container;
private IWebDriver _webDriver = default;
public Hooks(IObjectContainer objectContainer)
{
_container = objectContainer;
}
[BeforeScenario]
public void BeforeTestRun()
{
InstantiateDriver();
}
[AfterScenario]
public void AfterScenario()
{
if (!(_webDriver is null))
{
//Close any additional tabs open and then delete all cookies when one tab is open
var tabs = _webDriver.WindowHandles;
int numberOfTabs = _webDriver.WindowHandles.Count;
while (numberOfTabs > 1)
{
_webDriver.SwitchTo().Window(tabs[numberOfTabs - 1]);
_webDriver.Close();
numberOfTabs = _webDriver.WindowHandles.Count;
_webDriver.SwitchTo().Window(tabs[numberOfTabs - 1]);
}
_webDriver.Manage().Cookies.DeleteAllCookies();
_webDriver.Close();
_webDriver.Quit();
_webDriver.Dispose();
}
}
[AfterTestRun]
public static void After()
{
}
private void InstantiateDriver()
{
var selectedDriver = ConfigurationManager.AppSettings.Get("selectedWebDriver");
if (string.IsNullOrEmpty(selectedDriver))
{
throw new ArgumentException(Constants.ExceptionMessages.SelectedWebdriverCouldNotBeFound, selectedDriver);
}
switch (selectedDriver)
{
case "Chrome":
_webDriver = new ChromeDriver();
break;
case "Chrome Headless":
var chromeOption = new ChromeOptions();
chromeOption.AddArguments("headless");
chromeOption.AddArguments("--window-size=1680,1050");
_webDriver = new ChromeDriver(chromeOption);
break;
default:
throw new ArgumentException("Webdriver could not be found", selectedDriver);
}
_webDriver.Manage().Window.Maximize();
_container.RegisterInstanceAs<IWebDriver>(_webDriver);
}
}
}
If I close the chrome driver it must close everything as the Octopus step then seems to fail. Worth noting as well this does not happen if directly using visual studio.
Changing -Wait to - WaitProcess worked and has fixed the issue.
$testResults = (Start-Process -FilePath "$folderPath\runner\nunit3-console.exe" -ArgumentList $args -PassThru)
Wait-Process -Id $testResults.Id $testExitCode = $testResults.ExitCode
#Greg Burghardt, you were right about the zombie processes, so I eventually for some reason had a look at the -Wait argument. I noticed there was another PowerShell argument called -WaitProcess. It seems there is a difference in this case.
Unlike Start-Process -Wait, Wait-Process only waits for the processes identified. Start-Process -Wait waits for the process tree (the
process and all its descendants) to exit before returning control.
Source:
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/wait-process?view=powershell-7.2
I suspect that as these chrome processes were not actually ending sometimes, even though the main process had ended it was waiting for all these other ones to exit. It would explain why I manually had shut down all the processes via Task Manager for it to continue beforehand as it was waiting for the child processes to end.

Locate MSTest.exe using powershell

I'm in the process of automating my .Net solution build to be completely in PowerShell. I want to locate MSTest.exe using PowerShell.
I used the following script to locate MSBuild.exe and I hope that I can have something similar to locate MSTest.exe
$msBuildQueryResult = reg.exe query "HKLM\SOFTWARE\Microsoft\MSBuild\ToolsVersions\4.0" /v MSBuildToolsPath
$msBuildQueryResult = $msBuildQueryResult[2]
$msBuildQueryResult = $msBuildQueryResult.Split(" ")
$msBuildLocation = $msBuildQueryResult[12] + "MSBuild.exe"
Any directions ?
The following works with Visual Studio 2010 and higher[1]:
# Get the tools folder location:
# Option A: Target the *highest version installed*:
$vsToolsDir = (
Get-Item env:VS*COMNTOOLS | Sort-Object {[int]($_.Name -replace '[^\d]')}
)[-1].Value
# Option B: Target a *specific version*; e.g., Visual Studio 2010,
# internally known as version 10.0.
# (See https://en.wikipedia.org/wiki/Microsoft_Visual_Studio#History)
$vsToolsDir = $env:VS100COMNTOOLS
# Now locate msbuild.exe in the "IDE" sibling folder.
$msTestExe = Convert-Path -EA Stop (Join-Path $vsToolsDir '..\IDE\MSTest.exe')
The approach is based on this answer and is generalized and adapted to PowerShell.
It is based on system environment variables VS*COMNTOOLS, created by Visual Studio setup, where * represents the VS version number (e.g., 100 for VS 2010).
Re option A: Sort-Object is used to ensure that the most recent Visual Studio installation is targeted, should multiple ones be installed side by side:
The script block used for sorting first extracts only the embedded version number from the variable name ($_.Name -replace '[^\d]'; e.g., 100 from VS100COMNTOOLS) and converts the result to an integer ([int]); [-1] then extracts the last element from the sorted array - i.e., the variable object whose names has the highest embedded version number - and accesses its value (.Value).
The IDE subfolder, in which MSTest.exe is located is a sibling folder of the tools folder that VS*COMNTOOLS points to.
If MSTest.exe is NOT in the expected location, Convert-Path will throw a non-terminating error by default; adding -EA Stop (short for: -ErrorAction Stop) ensures that the script is aborted instead.
[1]
- I've tried up to Visual Studio 2015; do let me know whether or not it works on higher versions.
- Potentially also works with VS 2008.
Perhaps you are wanting something like this?
$regPath = "HKLM:\SOFTWARE\Microsoft\MSBuild\ToolsVersions\4.0"
$regValueName = "MSBuildToolsPath"
$msBuildFilename = "MSBUild.exe"
if ( Test-Path $regPath ) {
$toolsPath = (Get-ItemProperty $regPath).$regValueName
if ( $toolsPath ) {
$msBuild = Join-Path $toolsPath $msBuildFilename
if ( -not (Test-Path $msBuild -PathType Leaf) ) {
Write-Error "File not found - '$msBuild'"
}
}
}
# Full path and filename of MSBuild.exe in $msBuild variable
My way of getting mstest path.
GetMSTestPath function is main function which you call and then if first GetMsTestPathFromVswhere function will find something it returns path if not your will be making a long search for mstest.exe. Usually, it takes approximately 10 sec. I know that this is not the best but at least it is something when you struggle to find mstest.exe. Hope it will be helpful for somebody. :)))
function GetMSTestPath
{
function GetTime()
{
$time_now = Get-Date -format "HH:mm:ss"
return $time_now;
}
function GetMsTestPathFromVswhere {
$vswhere = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"
$path = & $vswhere -latest -prerelease -products * -requires Microsoft.Component.MSBuild -property installationPath
#write-host $path
if ($path) {
$tool = join-path $path 'Common7\IDE\MSTest.exe'
if (test-path $tool) {
return $tool
}
return ""
}
}
function SeachForMsTestPath
{
write-host $(GetTime)
$path = Get-ChildItem C:\ -Filter MSTest.exe -Recurse -ErrorAction Ignore | ? { $_.VersionInfo.FileDescription -eq 'Test Execution Command Line Tool' } | Select -First 1
write-host $(GetTime)
return $path
}
$msTestExePath = GetMsTestPathFromVswhere
if ([string]::IsNullOrEmpty($msTestExePath))
{
$msTestExePath = SeachForMsTestPath;
if ([string]::IsNullOrEmpty($msTestExePath))
{
Write-host "MsTest path is not found. Exiting with error"
Exit -1
}
}
return $msTestExePath;
}
Thanks #Bill_Stewart , I used your comments to write this working function:
function Get-MSTest-Location {
$msTests = #()
$searchResults = Get-ChildItem C:\* -Filter MSTest.exe -Recurse -ErrorAction Ignore
foreach($searchResult in $searchResults) {
try{
if(($searchResult.VersionInfo -ne $null) -and ($searchResult.VersionInfo.FileDescription -eq "Test Execution Command Line Tool"))
{ $msTests = $msTests + $searchResult.FullName }
}
catch{}
}
if($msTests.Length -eq 0)
{return "MSTest not found."}
return $msTests[0]
}

Save file in PowerShell script access denied

I have a PowerShell script that is intended to modify a web config transform as a pre-build event in a build definition. I've gotten it working for the most part, however when it goes to save the updated file I am getting access denied.
Is there a way to give the right access, without opening a window as this is done via the TFS build agent?
Here is the script:
param(
[string]$buildTarget="Dev",
[string]$projectName="SalesTools"
)
$VerbosePreference = "continue"
Write-Verbose "Params: buildTarget = '$($buildTarget)', projectName = '$($projectName)'"
# Make sure path to source code directory is available
if (-not $Env:TF_BUILD_SOURCESDIRECTORY)
{
Write-Error ("TF_BUILD_SOURCESDIRECTORY environment variable is missing.")
exit 1
}
elseif (-not (Test-Path $Env:TF_BUILD_SOURCESDIRECTORY))
{
Write-Error "TF_BUILD_SOURCESDIRECTORY does not exist: $Env:TF_BUILD_SOURCESDIRECTORY"
exit 1
}
Write-Verbose "TF_BUILD_SOURCESDIRECTORY: $Env:TF_BUILD_SOURCESDIRECTORY"
$webConfig = "$($Env:TF_BUILD_SOURCESDIRECTORY)\$($buildTarget)\SalesTools.Web\$($projectName)\web.$($buildTarget).config"
#$webConfig = "$($Env:TF_BUILD_SOURCESDIRECTORY)\$($buildTarget)\SalesTools.Web\ARCTools\web.$($buildTarget).config"
Write-Verbose "File Path: $($webConfig)"
$doc = (gc $webConfig) -as [xml]
$versionNumber = $doc.SelectSingleNode('//appSettings/add[#key="versionNumber"]/#value').'#text'
Write-Verbose "Current Version Number: $($versionNumber)"
if (($versionNumber))
{
$versionInfo = $versionNumber.Split(".")
$versionIteration = $versionInfo[1]
$minorVersion = $versionInfo[2] -as [int]
$minorVersion = $minorVersion + 1
$currentIteration = Get-Iteration
$newVersionInfo = ("v: 1.$($currentIteration).$($minorVersion)")
}
else
{
Write-Error "Could not get version info from config."
exit 1
}
$doc.SelectSingleNode('//appSettings/add[#key="versionNumber"]/#value').'#text' = $newVersionInfo
$doc.Save($webConfig)
Before you read & update the web.config, try to change the "Read-Only" attribute of web.config file. Because by default, all the source files are "Read-Only".
Add this line before "$doc = ....":
attrib -R $webConfig /S

Update Variable in TeamCity powershell script

I am trying to update an enviroment variable in TeamCity using Powershell script. However, it does not update the value of the variable. How can I do this?
Below is my current code which gets the currentBuildNumber fine:
$currentBuildNumber = "%env.currentBuildNumber%"
$newBuildNumber = ""
Write-Output $currentBuildNumber
If ($currentBuildNumber.StartsWith("%MajorVersion%") -eq "True")
{
$parts = $currentBuildNumber.Split(".")
$parts[2] = ([int]::Parse($parts[2]) + 1) + ""
$newBuildNumber = $parts -join "."
}
Else
{
$newBuildNumber = '%MajorVersion%.1'
}
//What I have tried
$env:currentBuildNumber = $newBuildNumber
Write-Host "##teamcity[env.currentBuildNumber '$newBuildNumber']"
Write-Host "##teamcity[setParameter name='currentBuildNumber' value='$newBuildNumber']"
Try
"##teamcity[setParameter name='env.currentBuildNumber' value='$newBuildNumber']"
(note the env. prefix in the name)
Also, you can try to increase the PowerShell std out column default (80 using TeamCity's command runner). If your service message is longer than that, then TeamCity will fail to parse it.
if ($env:TEAMCITY_VERSION) {
$host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.Size(8192,50)
}

Retrieving SSIS 2012 environmentvariable.name via power shell

We are using power shell to deploy our 2012 SSIS packages and have environment variables on a SSIS 2012 Server. Now during project deployment I am attempting to loop through eachvariable in the environment variables collection (foreach($variable in $environment.Variables)). That is no problem. I can see "EnvironmentVariable[#Name = 'something']"....however attempting to retrieve the name ("something") from the variable via $variable.Name or $variable.Key doesn't work. I've tried looping through $environment.Variables.Keys and still nothing. My power shell skills are a little weak since I've been using NANT the past several years but is there something I'm just not seeing?
Thanks in advance,
Anthony
Adding snippet of existing power shell script. The bolded $variable.Name is not working within the CreateETLPackages task. There is a lot of setup and other scripts called from this scripts so I haven't included everything. When $variable.Name is returned in a debug statement it returns "EnvironmentVariable[#Name = 'something']" as I mentoned in my original post:
Task 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"
}
}
Task 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"
}
}
Task 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"
}
}
}
}
Ok I found my issue. Looks like I was trying I need to use the $($object.Name) to get what I need out. Appreciate those that reached out to for their help.
Thanks,
Anthony