Add Powershell Modules & NuGet Repo/Add Artifactory Nuget Repository.ps1
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
<#
|
||||
Author: Gabe Kerntke
|
||||
Date: 02-04-2026
|
||||
|
||||
.Synopsis
|
||||
#Script to add the Artifactory Nuget Repository to the system
|
||||
|
||||
.Modified
|
||||
02-04-2026 (GabeK) - Original script created
|
||||
02-10-2026 (GabeK) - Updated Artifactory status code error handling
|
||||
02-11-2026 (GabeK) - Added PSRepository check
|
||||
05-08-2026 (GabeK) - Updated repo to prod
|
||||
#>
|
||||
|
||||
#Region Variables
|
||||
[CmdletBinding()]
|
||||
Param(
|
||||
[Parameter(Mandatory = $false)]
|
||||
$Package_Name = "Microsoft.PackageManagement.NuGetProvider.dll",
|
||||
[Parameter(Mandatory = $false)]
|
||||
$Package_Path = "tech-installs/os/Windows",
|
||||
[Parameter(Mandatory = $false)]
|
||||
$Path = "C:\Program Files\PackageManagement\ProviderAssemblies\nuget\2.8.5.208",
|
||||
[Parameter(Mandatory = $false)]
|
||||
$Artifactory_URL = "https://URLHERE/artifactory/",
|
||||
[Parameter(Mandatory = $false)]
|
||||
$NuGet_Repo = "https://URLHERE/artifactory/api/nuget/tech-nuget-prd",
|
||||
[Parameter(Mandatory = $false)]
|
||||
$Repo_Name = "Artifactory"
|
||||
)
|
||||
#EndRegion
|
||||
|
||||
Set-StrictMode -Version 2.0
|
||||
$Creds = Get-Credential -Message "Enter user account and password"
|
||||
$Final_Path = $Artifactory_URL + $Package_Path + "/" + $Package_Name
|
||||
|
||||
#Region Functions
|
||||
#Function to test if communication with the Artifactory is possible
|
||||
Function Test-Artifactory {
|
||||
try {
|
||||
$JFrog_URL = "https://URLHERE"
|
||||
$JFrog_Test = Invoke-WebRequest $JFrog_URL | Select-Object -ExpandProperty StatusCode
|
||||
}
|
||||
catch {
|
||||
#Adds TLS version 1.2 for communication
|
||||
[Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
|
||||
}
|
||||
|
||||
If ($JFrog_Test -eq "200") {
|
||||
$Download_Path_Test = Invoke-WebRequest "$Final_Path" -Credential $Creds
|
||||
|
||||
If ($Download_Path_Test.StatusCode -eq "200") {
|
||||
Get-From_Artifactory
|
||||
}
|
||||
Elseif ($Download_Path_Test.StatusCode -eq "403") {
|
||||
Write-Output "Xray is blocking the artifact from being downloaded"
|
||||
Exit
|
||||
}
|
||||
Else {
|
||||
Write-Output "Artifactory missing Package"
|
||||
Exit
|
||||
}
|
||||
}
|
||||
Else {
|
||||
Write-Output "Can't communicate with the $JFrog_URL."
|
||||
Exit
|
||||
}
|
||||
}
|
||||
|
||||
#Downloads package from Artifactory
|
||||
Function Get-From_Artifactory {
|
||||
$Dest_File = Join-Path $Path $Package_Name
|
||||
Invoke-WebRequest "$Final_Path" -Credential $Creds -OutFile $Dest_File
|
||||
}
|
||||
#EndRegion
|
||||
|
||||
#Region Start Script
|
||||
If (Get-PSRepository -Name $Repo_Name -ErrorAction SilentlyContinue) {
|
||||
Write-Output "Artifactory Nuget Repository already exists"
|
||||
Exit
|
||||
}
|
||||
If ((Test-Path $Path) -eq $False) { New-Item -ItemType directory -Path $Path -Force }
|
||||
Test-Artifactory
|
||||
Get-ChildItem "C:\Program Files\PackageManagement\ProviderAssemblies\nuget" -Recurse | Unblock-File
|
||||
Import-PackageProvider -Name NuGet -Force
|
||||
Register-PSRepository -Name $Repo_Name -SourceLocation $NuGet_Repo -PublishLocation $NuGet_Repo -InstallationPolicy Trusted -Credential $Creds
|
||||
Start-Sleep 10
|
||||
Find-Module -Repository $Repo_Name -Credential $Creds | Select-Object Version, Name, Description, Repository
|
||||
#EndRegion
|
||||
Reference in New Issue
Block a user