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

windows 10-Powershell安装

(windows 10 - Powershell install)

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

我在安装/更新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

来源位置:https://www.powershellgallery.com/api/v2

可信:错误

已注册:真实

InstallationPolicy:不受信任

PackageManagementProvider:NuGet

发布位置: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不会使用安装模块安装几乎任何模块

有该脚本:安装-模块-无法解决包源的“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-not-be-updated/

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

启用组策略以允许脚本并设置执行策略为绕过。现在可以安装该模块了。但是,我必须使用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