public/library/Update-PSTSSecureFiles.ps1

Function Update-PSTSSecureFiles
{
    [CmdletBinding()]
    param(

        [Parameter(Mandatory=$true)][string] $project,
        [Parameter(Mandatory=$true)][string] $path
    )

    process {
                try {

                    If(Test-Path -Path $Path) {

                            Write-Host "---Updating secure files---"
                            $managerConfig = Get-Content $path | Out-String | ConvertFrom-Json
                            $remoteSecureFiles = Get-PSTSSecureFile -project $project

                            $AllReleaseDefinitionsIds = $($(Get-PSTSReleaseDefinition -project $project).value | select @{Name = "id"; Expression = {$_.id}}).id

                            foreach ($env in $managerConfig.Environnements)
                            {
                                foreach ($item in $managerConfig.SecureFileLocations)
                                {
                                    $files= $item.Path | Get-ChildItem | Where-Object { !$_.PSIsContainer }

                                    foreach ($file in $files)
                                    {
                                        $fileToRemove= $remoteSecureFiles | Where-Object {$_.name.EndsWith($env) -and $_.name -eq $file}
                                        if ($null -ne $fileToRemove )
                                        {   
                                            Write-Host "File :" $fileToRemove.Name
                                            Remove-PSTSSecureFile -project $project -id $fileToRemove.id  -Verbose
                                            $fileToAdd= Add-PSTSSecureFile -project $project -name $file.Name  -file "$($item.Path)\$($file.Name)"  -Verbose

                                            foreach ($releaseId in $AllReleaseDefinitionsIds)
                                            {
                                                $rd = Get-PSTSReleaseDefinition -project $project -releaseId $releaseId
                                                $rdSecureFileId =$rd.environments.deployPhases.workflowTasks.inputs.secureFile

                                                if ($null -ne $rd.environments.deployPhases.workflowTasks.inputs.secureFile)
                                                {
                                                    
                                                    if($fileToRemove.id -eq $rdSecureFileId)
                                                    {
                                                        write-host "Updating Release :" $($rd.name)
                                                        $rd.environments.deployPhases.workflowTasks.inputs | where {$_.Keys -like "secureFile" } | foreach {$_.secureFile =  $fileToAdd.id} 
                                                        Update-PSTSReleaseDefinition -project $project -releaseDefinition $rd  | Out-Null
                                                    }
                                                }
                                            }             
                                        }
                                    }
                                }
                            }
                        }
                        else {
                            Write-Host "Local path is not valid"
                        }
                    }
                Catch {
                    Throw $_
            }
            }
}