Powershell几乎不会使用install-module

时间:2019-01-13 00:22:48

标签: powershell windows-10 powershell-v5.1

我的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

  1. Debugging log
  2. 我始终以管理员身份运行PS。
  3. -Scope CurrentUser引发相同的错误。
  4. 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

2 个答案:

答案 0 :(得分:4)

最终解决者:

[Net.ServicePointManager]::SecurityProtocol = "tls12"

解决方案来自以下blog posted by vanBrenk

尝试在代理后面安装PowerShell模块吗?您可能会遇到此错误:“无法解析程序包源'https://www.powershellgallery.com/api/v2/'” 。原来,不允许通过代理服务器[...]现在,通过代理发送并允许您运行的每个命令。

答案 1 :(得分:0)

如果您仅使用默认存储库,则无需指定它。

以这种方式尝试...

Find-Module -Name 'pscx' | 
Save-Module -Path "$env:USERPROFILE\Documents\WindowsPowerShell\Modules"
Install-Module -Name 'pscx'

您可以在同一站点上拥有多个版本的PowerShell,通常,它不会影响安装模块。