gpt4 book ai didi

Powershell 几乎不会使用 install-module 安装任何模块

转载 作者:行者123 更新时间:2023-12-02 22:20:13 25 4
gpt4 key购买 nike

我的 Powershell v5.1 几乎不会使用 install-module 自动安装任何模块而手动安装确实有效。

首先,当我运行 install-module ,它会下载模块然后抛出一个错误,比如 pscx 模块:

PackageManagement\Install-Package : Package 'Pscx' failed to be installed because: Specified cast is not valid.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.0.4\PSModule.psm1:9307 char:21
+ ... $null = PackageManagement\Install-Package @PSBoundParameters
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidResult: (Pscx:String) [Install-Package], Exception
+ FullyQualifiedErrorId : Package '{0}' failed to be installed because: {1},Microsoft.PowerShell.PackageManag
ement.Cmdlets.InstallPackage

我现在可以做的是手动下载模块并使用 import-module我。当我尝试通过此 cmdlet 安装任何模块时,几乎每次都会发生该错误。我该如何解决这个问题?

对于故障排除, Get-PSRepository给了我这个:
PS C:\> Get-PSRepository

Name InstallationPolicy SourceLocation
---- ------------------ --------------
PSGallery Trusted https://www.powershellgallery.com/api/v2
GalleryRolling Trusted https://www.poshtestgallery.com/api/v2/

编辑:

尝试使用 Save-Module 时遇到同样的错误:
PS C:\> Find-Module -Name 'pscx' | Save-Module -Path "G:\Temp\System Documents\WindowsPowerShell\Modules"
WARNING: Package 'Pscx' failed to be installed because: Specified cast is not valid.
WARNING: Package 'Pscx' failed to install.
PackageManagement\Save-Package : Unable to save the module 'Pscx'.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.0.4\PSModule.psm1:11331 char:25
+ ... $null = PackageManagement\Save-Package @PSBoundParameters
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Microsoft.Power...ets.SavePackage:SavePackage) [Save-Packag
e], Exception
+ FullyQualifiedErrorId : ProviderFailToDownloadFile,Microsoft.PowerShell.PackageManagement.Cmdlets.Save
Package

它下载了包然后显示 WARNING .

使用 Find-Module 时单独,结果是:
PS C:\> Find-Module -Name 'pscx'

Version Name Repository Description
------- ---- ---------- -----------
3.3.2 Pscx PSGallery PowerShell Community Extensi...

编辑 2:

哦忘了说下载进程栏消失后,具体下载文件夹中没有创建文件。这是否意味着下载失败?

编辑 3: @Sage Pourpre
  • Debugging log
  • 我总是以管理员身份运行 PS。
  • -Scope CurrentUser抛出同样的错误。
  • 两个PS C:\> Uninstall-Module -Name "PowerShellGet"PS C:\> Uninstall-Script -Name "PowerShellGet" -RequiredVersion 2.0.3产生相同的结果。

    PackageManagement\Uninstall-Package : 未找到指定搜索条件和脚本名称的匹配项
    'PowerShellGet'。
    在 C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.0.4\PSModule.psm1:12343 char:21
  • ... $null = PackageManagement\Uninstall-Package @PSBoundParameters
  • + CategoryInfo          : ObjectNotFound: (Microsoft.Power...ninstallPackage:UninstallPackage) [Uninstal
    l-Package], Exception
    + FullyQualifiedErrorId : NoMatchFound,Microsoft.PowerShell.PackageManagement.Cmdlets.UninstallPackage

  • 然后我尝试列出可用的模块:
    PS C:\> Get-Module -ListAvailable


    Directory: G:\Temp\System Documents\WindowsPowerShell\Modules


    ModuleType Version Name ExportedCommands
    ---------- ------- ---- ----------------
    Binary 2.7.1.7 ISESteroids {Get-PSSharperData, Add-SteroidsContextMenuComma...
    Script 1.2 Use-RawPipeline {Invoke-NativeCommand, Receive-RawPipeline, Get-...


    Directory: C:\Program Files\WindowsPowerShell\Modules


    ModuleType Version Name ExportedCommands
    ---------- ------- ---- ----------------
    Script 1.0.1 Microsoft.PowerShell.Operation.V... {Get-OperationValidation, Invoke-OperationValida...
    Script 1.2.4 PackageManagement {Find-Package, Get-Package, Get-PackageProvider,...
    Script 4.5.0 Pester {Describe, Context, It, Should...}
    Script 2.0.4 PowerShellGet {Find-Command, Find-DSCResource, Find-Module, Fi...
    Script 2.0.3 PowerShellGet {Find-Command, Find-DSCResource, Find-Module, Fi...
    Script 1.2 PSReadline {Get-PSReadlineKeyHandler, Set-PSReadlineKeyHand...


    Directory: C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules


    ModuleType Version Name ExportedCommands
    ---------- ------- ---- ----------------
    Manifest 1.0.0.0 AppBackgroundTask {Disable-AppBackgroundTaskDiagnosticLog, Enable-...
    Manifest 2.0.0.0 AppLocker {Get-AppLockerFileInformation, Get-AppLockerPoli...
    Manifest 1.0.0.0 AppvClient {Add-AppvClientConnectionGroup, Add-AppvClientPa...
    Manifest 2.0.0.0 Appx {Add-AppxPackage, Get-AppxPackage, Get-AppxPacka...
    Script 1.0.0.0 AssignedAccess {Clear-AssignedAccess, Get-AssignedAccess, Set-A...
    Manifest 1.0.0.0 BitLocker {Unlock-BitLocker, Suspend-BitLocker, Resume-Bit...
    etc...

    Get-InstalledModule
    PS C:\> Get-InstalledModule -Name "PowerShellGet" -RequiredVersion 2.0.3
    PackageManagement\Get-Package : No match was found for the specified search criteria and module names
    'PowerShellGet'.
    At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.0.4\PSModule.psm1:9050 char:9
    + PackageManagement\Get-Package @PSBoundParameters | Microsoft. ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (Microsoft.Power...lets.GetPackage:GetPackage) [Get-Package],
    Exception
    + FullyQualifiedErrorId : NoMatchFound,Microsoft.PowerShell.PackageManagement.Cmdlets.GetPackage

    最佳答案

    最后通过以下方式解决:

    [Net.ServicePointManager]::SecurityProtocol = "tls12"
    解决方案来自以下 blog posted by vanBrenk :

    Trying to install a PowerShell module behind a proxy? Chances are you're getting this error: "Unable to resolve package source 'https://www.powershellgallery.com/api/v2/'". Turns out it isn't allowed through your proxy server [...] Now every command you run is sent and allowed through the proxy.

    关于Powershell 几乎不会使用 install-module 安装任何模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54165095/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com