简体   繁体   English

无法在脚本 Powershell 中安装 package NuGet

[英]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.我想通过在 Intune 中使用脚本 powershell 在多个设备中安装模块 Sqlserver。 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.我创建了一个 function 来查看 package 是否已经安装,如果没有安装它。 It has to be forced and has not to request the user to confirm the installation.它必须是强制的,而不是要求用户确认安装。 Here is my function:这是我的 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      
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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