Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only install PowerShell PackageProvider and Module if not already installed

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  
like image 295
PatrickNolan Avatar asked May 18 '18 00:05

PatrickNolan


People also ask

How do you check PowerShell module is installed or not?

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.

How do I manually install a PowerShell module offline?

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.

How do I install a PowerShell module?

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.


1 Answers

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
}
like image 172
Owain Esau Avatar answered Nov 15 '22 05:11

Owain Esau