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  
                The Get-InstalledModule cmdlet gets PowerShell modules that are installed on a computer using PowerShellGet. To see all modules installed on the system, use the Get-Module -ListAvailable command.
The first step for installing a powershell module on an offline computer is to download it with a computer that is connected to the internet. In the Start menu search for the Windows Powershell application and open it. On the command line type Save-Module -Name ModuleName -Path “FilePath” and Enter to run the command.
Installing PowerShell modules from the PowerShell Gallery is the easiest way to install modules. To install a package or module from the Gallery, we use the command: Install-Module or Install-Script cmdlet, depending on the package type.
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
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With