Public/Invoke-OriAzBopDownloadPackage.ps1

<#
.DESCRIPTION
This function Download artifact
.SYNOPSIS
This function Download artifact
 
       
.PARAMETER RepositoryCredential
Credentials for connection to repository
 
.PARAMETER Artifacts
String array of Artifacts to download. e.g. @(@{Name = 'Ori.Online.Website'; Version='4.0.0-UAT-236612'}, @{Name = 'Ori.Online.Common.Web'; Version='4.0.0-UAT-236612'})
 
 
.EXAMPLE
$password = ConvertTo-SecureString 'heslo1' -AsPlainText -Force
$RepositoryCredential = New-Object System.Management.Automation.PSCredential 'Wuhan@oriflame.com',$password
 
Invoke-OriAzBopDownloadPackage `
-RepositoryCredential $RepositoryCredential `
-Artifacts @(@{Name = 'Ori.Online.Website'; RequiredVersion = '4.250.6224.1-UAT-2003141613'})
#>


function Invoke-OriAzBopDownloadPackage {
    
  [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = "Low")]
  param
  (         
    [parameter(Mandatory = $true, HelpMessage = 'Credentials for connection to repository')]
    [PSCredential] $RepositoryCredential,

    [parameter(Mandatory = $false, HelpMessage = "String array of Artifacts to download. e.g. @(@{Name = 'Ori.Online.Website'; Version='4.0.0-UAT-236612'}, @{Name = 'Ori.Online.Common.Web'; Version='4.0.0-UAT-236612'})")]
    [PSObject[]] $Artifacts

  )
  
  #in case of any error we want to stop execution, in stderr having the error. Use try-catch to handle errors if needed.
  $ErrorActionPreference = "Stop";
  Write-Output "-- Invoke-OriAzBopDownloadPackage --"
  Write-Output "RepositoryCredential: $(ConvertTo-Json $RepositoryCredential) "
  Write-Output "Artifacts: $(ConvertTo-Json $Artifacts) "
  Write-Output "-- Invoke-Commnad on machine [$($env:COMPUTERNAME)]"


  $failInTheEnd = $false
  foreach ($OneArtifact in $Artifacts) {
    $OneArtifact += @{Credential = $RepositoryCredential }
    $PackageToDownload = Find-Package @OneArtifact 

    if ([string]::IsNullOrEmpty($PackageToDownload)) {
      Write-Output "Such artifact not found $(Convertto-Json $OneArtifact)"
      $failInTheEnd = $true
    }
    else {
      Install-Package @OneArtifact 
    }
  }
          
  if ($failInTheEnd) {
    Write-Error "Some artifacts was not found. See previous log."
  }
  Write-Output "-- End of Invoke-Commnad on machine [$($env:COMPUTERNAME)]"
  Write-Output "-- End of Invoke-OriAzBopDownloadPackage --"


}