Warm tip: This article is reproduced from serverfault.com, please click

Powershell install

发布于 2020-08-12 22:03:54

I am having a difficult time installing/updating my powershell modules. I noticed this when I tried installing DBA Tools moudle. Reference links are https://dbatools.io/download/ and https://github.com/sqlcollaborative/dbatools.

It's a corporate PC. But I know that I have installed other modules before in the past. Does anyone have any idea what's going on?


PS (Admin)>

Install-Module DBATools

  • NOTE: The Install-Module command pauses for many minutes before the command returns a warning message.

WARNING: Unable to resolve package source 'https://www.powershellgallery.com/api/v2'. ERROR: "PackageManagement\Install-Package : No match was found for the specified search criteria and module name 'PowerShellGet'".

Update-Module PowerShellGet

ERROR: "Update-Module : Module 'PowerShellGet' was not installed by using Install-Module, so it cannot be updated.".

Update-Module PowerShellGet -Force

ERROR: "Update-Module : Module 'PowerShellGet' was not installed by using Install-Module, so it cannot be updated.".

Find-Module dbatools

  • NOTE: The Find-Module command pauses for many minutes before the command returns an error message.

ERROR: "No match was found for the specified search criteria and module name 'dbatools'. Try Get-PSRepository to see all available registered module repositories."

Get-PSRepository | fl *

Name : PSGallery

SourceLocation : https://www.powershellgallery.com/api/v2

Trusted : False

Registered : True

InstallationPolicy : Untrusted

PackageManagementProvider : NuGet

PublishLocation : https://www.powershellgallery.com/api/v2/package/

ScriptSourceLocation : https://www.powershellgallery.com/api/v2/items/psscript

ScriptPublishLocation : https://www.powershellgallery.com/api/v2/package/

ProviderOptions : {}

Get-Module PackageManagement -ListAvailable

Directory: C:\Program Files\WindowsPowerShell\Modules

ModuleType Version Name ExportedCommands

Binary 1.0.0.1 PackageManagement {Find-Package, Get-Package, Get-PackageProvider, Get-Packa...

Binary 1.0.0.1 PackageManagement {Find-Package, Get-Package, Get-PackageProvider, Get-Packa...

Get-Module -ListAvailable |
 Where-Object ModuleBase -like $env:ProgramFiles\WindowsPowerShell\Modules\* |
 Sort-Object -Property Name, Version -Descending |
 Get-Unique -PipelineVariable Module |
 ForEach-Object {
     if (-not(Test-Path -Path "$($_.ModuleBase)\PSGetModuleInfo.xml")) {
         Find-Module -Name $_.Name -OutVariable Repo -ErrorAction SilentlyContinue |
         Compare-Object -ReferenceObject $_ -Property Name, Version |
         Where-Object SideIndicator -eq '=>' |
         Select-Object -Property Name,
                                 Version,
                                 @{label='Repository';expression={$Repo.Repository}},
                                 @{label='InstalledVersion';expression={$Module.Version}}
     }

 }         

WARNING: Unable to resolve package source 'https://www.powershellgallery.com/api/v2'. WARNING: Unable to resolve package source 'https://www.powershellgallery.com/api/v2'. WARNING: Unable to resolve package source 'https://www.powershellgallery.com/api/v2'. WARNING: Unable to resolve package source 'https://www.powershellgallery.com/api/v2'. WARNING: Unable to resolve package source 'https://www.powershellgallery.com/api/v2'.

$webclient=New-Object System.Net.WebClient
$webclient.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
[Net.ServicePointManager]::SecurityProtocol = "tls12"
Find-Module dbatools

WARNING: Unable to resolve package source 'https://www.powershellgallery.com/api/v2'.

PackageManagement\Find-Package : No match was found for the specified search criteria and module name 'dbatools'. Try Get-PSRepository to see all available registered module repositories.

Invoke-WebRequest https://www.powershellgallery.com/api/v2

Invoke-WebRequest : The underlying connection was closed: The connection was closed unexpectedly.


Some references I tried

windows 10 - Powershell won't install almost any module using install-module - Stack Overflow Powershell won't install almost any module using install-module

There's a script for that: Install-Module - unable to resolve package source 'https //www.powershellgallery.com/api/v2/' https://vanbrenk.blogspot.com/2017/09/install-module-unable-to-resolve.html

Update Manually Installed PowerShell Modules from the PowerShell Gallery – Mike F Robbins https://mikefrobbins.com/2016/06/09/update-manually-installed-powershell-modules-from-the-powershell-gallery/

Update-Module : Module 'PowershellGet' was not installed by using Install-Module, so it cannot be updated. - Evotec https://evotec.xyz/update-module-module-powershellget-was-not-installed-by-using-install-module-so-it-cannot-be-updated/

Questioner
SherlockSpreadsheets
Viewed
0
SherlockSpreadsheets 2020-10-20 04:22:19

Enabled Group Policy to allow scripts and set execution policy to bypass. I am now able to install the module. However, I must run the install with Scope current user and am still unable to install with As Admin powershell.

Module Install

Install-Module DBATools -Scope CurrentUser

Group Policy (Fix)

#*****************
FIX ...
#*****************

## PS (As Admin)
gpedit.msc
# Navigate to: Computer Configuration > Administrative Templates > Windows Components > Windows PowerShell. Change the “Turn on Script Execution”
# Turn on Script Execution > Enabled, Policy "Allow all scripts"

## PS (As Admin)
Set-ItemProperty -Path HKLM:\Software\Policies\Microsoft\Windows\PowerShell -Name ExecutionPolicy -Value ByPass


<# 
#*****************
REFERENCE ...
#*****************
 Windows PowerShell - the setting is overridden by a policy defined at a more specific scope
 https://vladtalkstech.com/2015/03/windows-powershell-updated-execution-policy-successfully-setting-overridden-policy-defined-specific-scope.html

 Change the PowerShell MachinePolicy Execution Policy in WS 2012R2
 https://vladtalkstech.com/2014/03/change-powershell-machinepolicy-execution-policy-windows-server-2012r2.html
#>


#*****************
CAUSE ...
#*****************

Get-ExecutionPolicy –List

Set-ExecutionPolicy -Scope MachinePolicy Unrestricted

## However, you might have an error saying it can only be done via Group Policy.
## “Set-ExecutionPolicy : Cannot set execution policy. Execution policies at the MachinePolicy or UserPolicy scopes mustbe set through Group Policy