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,411
  • 2
  • 16
  • 34
  • 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

1 Answers1

16

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,768
  • 2
  • 18
  • 30