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 'password1' -AsPlainText -Force
$RepositoryCredential = New-Object System.Management.Automation.PSCredential 'Wuhan@myemail.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-Verbose -Message ("[ START: {0}:{1} (v.{2}) ]" -f $Local:MyInvocation.MyCommand.Source, $Local:MyInvocation.MyCommand.Name, $Local:MyInvocation.MyCommand.Version)
  foreach ($arg in $PSBoundParameters.GetEnumerator()) {
    if ([string]::IsNullOrEmpty($arg.Value)) {
      Write-Debug -Message ("[null] {0}: {1}" -f $arg.Key, $arg.Value) -ErrorAction SilentlyContinue 
    }
    else {
      Write-Debug -Message ("[{2}] {0}: {1}" -f $arg.Key, $arg.Value, $arg.Value.GetType().Name) -ErrorAction SilentlyContinue 
    }
  }
  Write-Debug "-- Invoke-Commnad on machine [$($env:COMPUTERNAME)]"


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

    if ([string]::IsNullOrEmpty($PackageToDownload)) {
      Write-Debug "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-Verbose "-- End of Invoke-Commnad on machine [$($env:COMPUTERNAME)]"
  Write-Verbose -Message ("[ END: {0} ]" -f $Local:MyInvocation.MyCommand.Name)


}