1

I want to install the module Sqlserver in several devices by using a script powershell in Intune. It didn't work in few devices because it asks to install the package-provider. So I tried to add this step in my script.

I create a function that looks if the package is already installed, if not it get it. It has to be forced and has not to request the user to confirm the installation. Here is my function :

function Install-PackageProvider(
[string] [Parameter(Mandatory = $true)] $ModuleName
)

{

    If(Get-PackageProvider| Where-Object {$_.Name -eq $ModuleName})
    {
        $Info = "The Package-Provider is already installed"
        Write-Host $Info
    }
    else
    {
        Write-Host "We need to install the package provider. We start now"
        
        [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
        Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force       
    }
}

Unfortunately, it does not work for few devices and I get this error message where it can not identify a parameter corresponding to "Name". I try several things that I see on internet.

Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 ...
                        ~~~~~

Anyone has an idea and can help me. The English is not my mother language, I hope that I did not do too many mistakes

Thank you

Maikiii
  • 421
  • 4
  • 13
  • That is curious. What does `Install-PackageProvider -?` show as the supported parameters? What PowerShell version? Note that even with a functioning NuGet provider `Install-Package` may fall short, because a package's _dependencies_ aren't also installed - see [this answer](https://stackoverflow.com/a/50004706/45375). – mklement0 Nov 18 '21 at 19:59

1 Answers1

1

I would try the following. It seems to work for me

function Install-PackageProvider(
[string] [Parameter(Mandatory = $true)] $ModuleName
)
{    If(Get-PackageProvider| Where-Object {$_.Name -eq $ModuleName})
    {
        $Info = "The Package-Provider is already installed"
        Write-Host $Info
    }
    else
    {
        Write-Host "We need to install the package provider. We start now"
        
        [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
        PackageManagement\Install-PackageProvider -Name $ModuleName -Force      
    }
}
John Boyd
  • 51
  • 2
  • Interesting. Can you explain why module-qualifying the `Install-PackageProvider` call (`PackageManagement\ `) made the difference? Is there a competing `Install-PackageProvider` command, and where might it come from? – mklement0 Nov 19 '21 at 20:37