问题描述
我在安装/更新Powershell模块时遇到了困难。当我尝试安装DBA Tools模块时,我注意到了这一点。参考链接为https://dbatools.io/download/和https://github.com/sqlcollaborative/dbatools。
这是公司的PC。但是我知道我以前曾经安装过其他模块。有人知道发生了什么吗?
PS(管理员)>
Install-Module DBATools
- 注意:在命令返回警告消息之前,Install-Module命令会暂停几分钟。
警告:无法解析程序包源“ https://www.powershellgallery.com/api/v2”。 错误:“ PackageManagement \ Install-Package:找不到与指定搜索条件和模块名称'PowerShellGet'相匹配的内容。”
Update-Module PowerShellGet
错误:“更新模块:未使用安装模块安装模块'PowerShellGet',因此无法更新。”
Update-Module PowerShellGet -Force
错误:“更新模块:未使用安装模块安装模块'PowerShellGet',因此无法更新。”
Find-Module dbatools
- 注意:Find-Module命令会暂停数分钟,然后该命令返回错误消息。
错误:“找不到与指定的搜索条件和模块名称'dbatools'相匹配的内容。尝试Get-PSRepository查看所有可用的已注册模块存储库。”
Get-PSRepository | fl *
名称:PSGallery
SourceLocation:https://www.powershellgallery.com/api/v2
受信任:错误
已注册:是
InstallationPolicy:不受信任
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
目录:C:\ Program Files \ WindowsPowerShell \ Modules
ModuleType版本名称ExportedCommands
二进制1.0.0.1 PackageManagement {查找包,获取包,获取包提供者,获取包...
二进制1.0.0.1 PackageManagement {查找包,获取包,获取包提供者,获取包...
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}}
}
}
警告:无法解析程序包源“ https://www.powershellgallery.com/api/v2”。 警告:无法解析包源“ https://www.powershellgallery.com/api/v2”。 警告:无法解析包源“ https://www.powershellgallery.com/api/v2”。 警告:无法解析包源“ https://www.powershellgallery.com/api/v2”。 警告:无法解析程序包源“ 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
警告:无法解析程序包源'https://www.powershellgallery.com/api/v2'。
PackageManagement \ Find-Package:找不到与指定搜索条件和模块名称'dbatools'相匹配的内容。尝试 Get-PSRepository以查看所有可用的已注册模块存储库。
Invoke-WebRequest https://www.powershellgallery.com/api/v2
Invoke-WebRequest:基础连接已关闭:连接意外关闭。
我尝试过的一些参考
Windows 10-Powershell几乎不会使用安装模块安装任何模块-代码日志 Powershell won't install almost any module using install-module有一个用于此的脚本:Install-Module-无法解析包源'https //www.powershellgallery.com/api/v2/' https://vanbrenk.blogspot.com/2017/09/install-module-unable-to-resolve.html
从PowerShell库中更新手动安装的PowerShell模块– Mike F Robbins https://mikefrobbins.com/2016/06/09/update-manually-installed-powershell-modules-from-the-powershell-gallery/
Update-Module:没有使用Install-Module安装模块'PowershellGet',因此无法对其进行更新。 -Evotec https://evotec.xyz/update-module-module-powershellget-was-not-installed-by-using-install-module-so-it-cannot-be-updated/
解决方法
感谢 Stephen,rouxquasar 它为我使用了以下命令 Windows 2016 数据中心,KB4598243,执行策略设置正确,因此不必处理。
-
启用 TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
-
注册默认的 PS Gallery Repo(可以检查 Get-PSRepository | fl* 以防万一)
注册-PSRepository -默认
-
Install-Module dbatools(在验证之前检查 Find-Module)
如果存在旧版本的 dbatools,请使用 -Force 开关
,尝试运行Register-PSRepository -Default
我在安装不同模块时遇到相同的错误。我的解决方法是为.net安全协议指定TLS1.2。
[Net.ServicePointManager] :: SecurityProtocol = [Net.SecurityProtocolType] :: Tls12
,启用组策略以允许脚本并设置执行策略为绕过。我现在可以安装该模块。但是,我必须使用Scope当前用户运行安装,并且仍然无法使用As Admin powershell进行安装。
模块安装
Install-Module DBATools -Scope CurrentUser
组策略(修复)
#*****************
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
,
我尝试了register-psrepository,它执行时没有出现任何错误,但是当我尝试get-psrepository时,它仍然给出相同的错误消息“警告:无法找到模块存储库。”,我尝试了以下适用于以下步骤的选项我:
-
使用不同的名称注册ps存储库,并使用相同的psgallery api作为源位置。 Register-PSRepository PSGallery1 -SourceLocation“ https://www.powershellgallery.com/api/v2” -InstallationPolicy受信任
-
设置存储库 Set-PSRepository PSGallery1
-
现在当我检查存储库时,它就像魅力一样工作 PSGAllery1 is the Name I have used