简体   繁体   中英

Can't install the package NuGet in a Script Powershell

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

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      
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM