10

My Powershell v5.1 won't install almost any module automatically using install-module whereas manually installation does work.

First, when I run install-module, it would download the module then throw an error, say the pscx module:

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

What I can do now is to manually download the module and use import-module myself. The error occurs almost every time when I'm trying to install any module through this cmdlet. How can I address this problem?

For troubleshooting, Get-PSRepository gives me this:

PS C:\> Get-PSRepository

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

EDIT:

Got the same error when trying to use 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

It downloaded the package then displays the WARNING.

When using the Find-Module alone, the result is:

PS C:\> Find-Module -Name 'pscx'

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

EDIT 2:

Oh I forgot to say that there are no files created in the specific download folder after the download process bar disappeared. Does that mean the download has failed?


EDIT 3: @Sage Pourpre

  1. Debugging log
  2. I Always run PS as administrator.
  3. -Scope CurrentUser throws the same error.
  4. Both PS C:\> Uninstall-Module -Name "PowerShellGet" and PS C:\> Uninstall-Script -Name "PowerShellGet" -RequiredVersion 2.0.3 produce the same result.

    PackageManagement\Uninstall-Package : No match was found for the specified search criteria and script names 'PowerShellGet'. At 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
      

Then I tried to list available modules:

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

And 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
Sage Pourpre
  • 9,932
  • 3
  • 27
  • 39
preachers
  • 373
  • 1
  • 5
  • 15
  • Are you specifying a repository? If so, does that error happen on both repos? Where is the code you're using to invoke it? I can see you're wrapping it in something because your invocation is `$null = PackageManagement\Install-Package @PSBoundParameters` so seeing the code around it would actually be helpful in figuring this out. – briantist Jan 13 '19 at 00:42
  • @briantist When I specify the repository, the default one, ie: PSGallery, returns the error I posted above. When using GalleryRolling, it says "No match was found for the specified search criteria and module name 'Pscx'." As for the code around it, it's the native `PSModule.psm1` comes from the `PowerShellGet v2.0.4` module itself. – preachers Jan 13 '19 at 00:57
  • @briantist I have both `PowerShellGet v2.0.4` and `v2.0.3` installed. With `v2.0.3` the problem is the same except for the line position where the error occurs `At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.0.3\PSModule.psm1:9550 char:21`. – preachers Jan 13 '19 at 01:10
  • Add the `-Debug` and `-Verbose` switch to your `Install-Module` call to see if there are more informations. Also, make sure you execute that as administrator. Finall, if nothing works at that point, try with the '-Scope CurrentUser' to see if it changes anything. If nothing, uninstall / reinstall PowershellGet to see if it fixes anything. – Sage Pourpre Jan 13 '19 at 03:43
  • @ See my edit above please :) – preachers Jan 13 '19 at 04:41
  • The log shows that PackageManagement is the module that crash. Can you try installing an older version `Install-Module -Name PackageManagement -RequiredVersion 1.1.7.2` If it does not work, which I supect will happens, can you install it manually by copying the module from another computer ? Once you get it installed, try to put a `Remove-Module -Name PackageManagement -ErrorAction SilentlyContinue` then `Import-Module -Name PackageManagement -RequiredVersion 1.1.7.2` then your Install-module to see if it works. – Sage Pourpre Jan 13 '19 at 10:10
  • In fact, I might be wrong on that but try to install .net core 2.0.9 and 2.1 before trying the older version to see if it helps: https://dotnet.microsoft.com/download/dotnet-core/2.0 – Sage Pourpre Jan 13 '19 at 10:11
  • @Sage Pourpre Still no lucky, simply install .NET Core and repeat previous procedures doesn't work. PackageManagement also doesn't work with old 1.1.7.2 version, I even tried the 2.0.1 version of PowershellGet and the error thrown is the same. – preachers Jan 13 '19 at 11:17

3 Answers3

9

Finally solved by:

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

The solution is from the following 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.

ndemou
  • 4,691
  • 2
  • 30
  • 33
preachers
  • 373
  • 1
  • 5
  • 15
1

If you are just using the default repo, there is not reason to specify it.

Try it this way...

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

You can have multiple version of PowerShell on the same site, it should not impact installing modules, generally.

postanote
  • 15,138
  • 2
  • 14
  • 25
  • Oh I forgot to say that there are no files created in the specific download folder after the download process bar disappeared. Does that mean the download has failed? – preachers Jan 13 '19 at 03:22
  • Hummm, that's a bit odd, but why are you trying to download and install here: "G:\Temp\System Documents\WindowsPowerShell\Modules", PowerShell will never look here for modules, unless you specifically add it to your environment path. Do the install to the user profile default location and use the -force switch for your actions, as well as starting PowerShell as admin to see if your are successful. – postanote Jan 13 '19 at 03:41
  • In order to get rid of a messy Document folder, I move the Windows system Document folder to a location where I would never touch hence the "G:\Temp\System Documents\WindowsPowerShell\Modules". Powershell WILL look here and use modules exist here. – preachers Jan 13 '19 at 04:45
0

I have almost the same problem.After debugging with dnspy.I found that the type of key\HKLM\SYSTEM\CurrentControlSet\Control\Lsa\FipsAlgorithmPolicy\Enabled is wrong,and changing the type from REG_SZ to REG_DWORD solves the problem. You can see my post here.

傅继晗
  • 927
  • 1
  • 8
  • 14