感谢 Anton Purin,我将他的脚本更新为:
Function Register-PSRepositoryFix {
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true)]
[String]
$Name,
[Parameter(Mandatory=$true)]
[Uri]
$SourceLocation,
[ValidateSet('Trusted', 'Untrusted')]
$InstallationPolicy = 'Trusted'
)
$ErrorActionPreference = 'Stop'
Try {
Write-Verbose 'Trying to register via Register-PSRepository'
Register-PSRepository -Name $Name -SourceLocation $SourceLocation -InstallationPolicy $InstallationPolicy
Write-Verbose 'Registered via Register-PSRepository'
} Catch {
Write-Verbose 'Register-PSRepository failed, registering via workaround'
# Adding PSRepository directly to file
Register-PSRepository -name $Name -SourceLocation $env:TEMP -InstallationPolicy $InstallationPolicy
$PSRepositoriesXmlPath = "$env:LOCALAPPDATA\Microsoft\Windows\PowerShell\PowerShellGet\PSRepositories.xml"
$repos = Import-Clixml -Path $PSRepositoriesXmlPath
$repos[$Name].SourceLocation = $SourceLocation.AbsoluteUri
$repos[$Name].PublishLocation = (New-Object -TypeName Uri -ArgumentList $SourceLocation, 'package/').AbsoluteUri
$repos[$Name].ScriptSourceLocation = (New-Object -TypeName Uri -ArgumentList $SourceLocation, 'items/psscript/').AbsoluteUri
$repos[$Name].ScriptPublishLocation = (New-Object -TypeName Uri -ArgumentList $SourceLocation, 'package/').AbsoluteUri
$repos | Export-Clixml -Path $PSRepositoriesXmlPath
# Reloading PSRepository list
Set-PSRepository -Name $Name -InstallationPolicy Untrusted
Write-Verbose 'Registered via workaround'
}
}
# Usage Example
Register-PSRepositoryFix -Name "Name" -SourceLocation "http://address:port/api/v2/" -Verbose
两个主要区别是:
1) Set-PSRepository -Name $Name -InstallationPolicy Untrusted 而不是 Set-PSRepository -Name PSGallery -InstallationPolicy Untrusted
2) 为 PowerShell 脚本设置 ScriptSourceLocation 和 ScriptPublishLocation