5

I have running the following Powershell script as part of an Octopus Deploy.

However, I only want them to install if they are not already installed.

I they are installed, preferably it would also only install them if they are below a certain version.

Can someone advise what is considered to be the best approach for doing this?

Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Confirm:$False -Force 

Install-Module -Name SqlServer -AllowClobber -Confirm:$False -Force  
PatrickNolan
  • 1,671
  • 2
  • 20
  • 40
  • What exactly have you tried? – Owain Esau May 18 '18 at 00:37
  • I've tried a few different approaches but haven't been able to find a clean solution. In the case of checking the PackageProvider I was hoping that I could do a get using $p = (Get-packageProvider -name nuget). I think this should only install if it doesn't exist. However, I don't believe you can specify a version. – PatrickNolan May 18 '18 at 00:52
  • I think this [answer](https://stackoverflow.com/a/56893689/681659) might solve your problem – TJ Galama Jul 04 '19 at 20:00

2 Answers2

17

Something like this should work:

if (Get-Module -ListAvailable -Name SqlServer) {
    Write-Host "SQL Already Installed"
} 
else {
    try {
        Install-Module -Name SqlServer -AllowClobber -Confirm:$False -Force  
    }
    catch [Exception] {
        $_.message 
        exit
    }
}


if ((Get-PackageProvider -Name NuGet).version -lt 2.8.5.201 ) {
    try {
        Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Confirm:$False -Force 
    }
    catch [Exception]{
        $_.message 
        exit
    }
}
else {
    Write-Host "Version of NuGet installed = " (Get-PackageProvider -Name NuGet).version
}
Owain Esau
  • 1,876
  • 2
  • 21
  • 34
0

Recently MS updated Package Provider installation to dynamic. This allows one liner to act on various states of nuGet to install/update/confirm latest version: Example 3: Dynamically get a package provider

Get-PackageProvider -Name "nuGet" -ForceBootstrap | 
Select-Object -Property Name, Version | 
Format-Table -Autosize

This way if/else is not needed, and also knowing existing/repository version is not needed as well.

Here is my tested in various package/module cases script (updated):

# array of modules to install (update) / variables
Clear-Host
$ErrorActionPreference  = "SilentlyContinue"
$debugPreference        = "Continue"
$sqlModule              = @("PowershellGet", "sqlserver", "dbaTools")

# Dynamically check/install/update Package Provider
Get-PackageProvider -Name "nuGet" -ForceBootstrap | 
    Select-Object -Property Name, Version | 
    Format-Table -Autosize

# Force install/update modules from PS Repositry
$sqlModule.ForEach( {if (-not (Get-Module -Name $_ -ListAvailable) ){
        Install-Module -Name $_ -AllowClobber -Confirm:$False -Scope AllUsers -Force | Import-Module | Write-Host "Module $($_) imported"
        } else {
            # powershell -NoProfile -NonInteractive -Command "Remove-Module $($_) -Force"
            Get-InstalledModule -Name $_ | Uninstall-Module -AllVersions -Force -Verbose
            Install-Module -Name $_ -AllowClobber -Confirm:$False -Scope AllUsers -Force | Import-Module | Write-Host "Module $($_) imported"
        }
    }
)

# disable "dbtools" imported notification
Set-DbatoolsConfig -Name Import.SqlpsCheck -Value $false -PassThru | 
    Register-DbatoolsConfig

# Import installed modules
# $sqlModule.ForEach( {if(Get-Module -Name $_ -ListAvailable){
#         Import-Module -Name $_ | Write-Host "Module $($_) imported"
#         } else {
#             Write-Host "Module $($_) not imported" -ForegroundColor Red -BackgroundColor White
#         }
#     }
# )

# List modules
Get-Module -ListAvailable | 
    Where-Object { 
        $_.Name -match $sqlModule[0] -or
        $_.Name -match $sqlModule[1] -or
        $_.Name -match $sqlModule[2] } |
    Select-Object -Property Name, Version |
    Format-Table -AutoSize