在powershell中有条件地注册存储库

在powershell中有条件地注册存储库,powershell,Powershell,我想按以下方式注册powershell存储库: if((Get-PSRepository -Name $artifactory.Key.ToString().Trim()) -eq $null) { #register the repository if not found $Location = "$($ConfigInfo.ArtifactoryCredentials.ArtifactoryServer.ToString().Trim())/$($artifa

我想按以下方式注册powershell存储库:

if((Get-PSRepository -Name $artifactory.Key.ToString().Trim()) -eq $null)
{
        #register the repository if not found
        $Location = "$($ConfigInfo.ArtifactoryCredentials.ArtifactoryServer.ToString().Trim())/$($artifactory.Value.ToString().Trim())"

        Write-Host "Registering repositories $($Location)" -ForegroundColor Cyan

        Register-PSRepository -Name $artifactory.Key.ToString().Trim() -SourceLocation $Location `
                                                                       -PublishLocation $Location `
                                                                       -InstallationPolicy Trusted -Verbose
}
条件检查似乎无效,因为代码进入if块时出现以下错误:

PackageManagement\Get-PackageSource : Unable to find repository
进入if块是有效的,但是它输入了一个错误,我想取消它


如何检查是否找到回购?

只需使用否定条件!运算符并添加要忽略的错误操作:

if(!(Get-PSRepository -Name $artifactory.Key.ToString().Trim() -ErrorAction Ignore ))

谢谢您甚至可以通过单击^looking按钮向上投票来提高答案的知名度。