$RepositoryLocation = Read-Host -Prompt "Please enter the location of the repository" #This is the location of the PS modules repository. Our official repository can be found here, "https://repo.corasequence.digital/repository/CoraSeQuencePowerShell/". If you are using your own repository then use its URL here. $RepositoryName = Read-Host -Prompt "Please enter a name for the repository" #By default, the repository name should be Cora $KeepLatestOnly = $true $Credentials = Get-Credential -Message "Please enter credentials for accessing the repository (Click cancel if credentials are not required)" if ($Credentials) { $CoraSeQuencePSRepository = @{Name=$RepositoryName; SourceLocation=$RepositoryLocation; InstallationPolicy="Trusted"; Credential=$Credentials} } else { $CoraSeQuencePSRepository = @{Name=$RepositoryName; SourceLocation=$RepositoryLocation; InstallationPolicy="Trusted"} } Write-Host -Object "Installing NuGet PackageProvider" $webclient=New-Object System.Net.WebClient $webclient.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 Install-PackageProvider -Name NuGet -RequiredVersion 2.8.5.208 -Verbose Install-Module PowerShellGet -RequiredVersion 2.2.4 -SkipPublisherCheck -Verbose -Force Write-Host -Object "Removing existing repository and package source" Get-PSRepository -Verbose | Where-Object {$_.Name -eq $RepositoryName -or $_.SourceLocation -eq $RepositoryLocation} | Unregister-PSRepository -Verbose Get-PackageSource -Verbose | Where-Object {$_.Source -eq $RepositoryName -or $_.Location -eq $RepositoryLocation} | Unregister-PackageSource -Verbose Write-Host -Object "Registering repository and package source" if ($Credentials) { Register-PackageSource -Name $RepositoryName -Location $RepositoryLocation -Credential $Credentials -ProviderName NuGet -Trusted } else { Register-PackageSource -Name $RepositoryName -Location $RepositoryLocation -ProviderName NuGet -Trusted } Register-PSRepository @CoraSeQuencePSRepository Write-Host -Object "Installing modules" if ($Credentials) { Find-Module -Name * -Repository $RepositoryName -Credential $Credentials -Verbose | Install-Module -Credential $Credentials -Verbose } else { Find-Module -Name * -Repository $RepositoryName -Verbose | Install-Module -Verbose } if ($KeepLatestOnly) { Write-Host -Object "Getting all installed versions" $InstalledModules = Get-InstalledModule | Where-Object { $_.Repository -eq $RepositoryName} foreach ($InstalledModule in $InstalledModules) { $Latest = Get-InstalledModule -Name $InstalledModule.Name #Deleting the directory because Uninstall-Module and Update-Module do not handle dependencies and old versions Write-Host -Object "Deleting old versions" Get-InstalledModule -Name $InstalledModule.Name -AllVersions | Where-Object {$_.Version -ne $Latest.Version} | Select-Object -Property InstalledLocation -ExpandProperty InstalledLocation | Remove-Item -Recurse -Force -Verbose } }