Cannot access Chocolatey package from Azure DevOps Artifact Feed - azure-devops

The flowing code has been pushed to an Azure DevOps Artifacts Feed:
schily-cdrtools
Pushing the nuget package and downloading work without issues using nuget.exe:
nuget.exe push -Source "cdrtools-artifacts" -ApiKey AzureDevOps schily-cdrtools.3.2.1.nupkg
nuget.exe install -Source "cdrtools-artifacts" schily-cdrtools
However, attempting to register this source with Chocolatey results in a 404 error using either a v2 or v3 nuget registration:
choco source add -n=schily-artifacts `
>> -s="https://flapjacks.pkgs.visualstudio.com/_packaging/schily-artifacts/nuget/v3/index.json" `
>> -u="xxxxxxxx" -p="xxxxxxxxxxx"
Error retrieving packages from source 'https://flapjacks.visualstudio.com/_packaging/schily-artifacts/nuget/v3/index.json':
The remote server returned an error: (404) Not Found.
schily-cdrtools not installed. The package was not found with the source(s) listed.
Source(s): 'https://flapjacks.visualstudio.com/_packaging/schily-artifacts/nuget/v3/index.json'
NOTE: When you specify explicit sources, it overrides default sources.
If the package version is a prerelease and you didn't specify `--pre`,
the package may not be found.
Please see https://chocolatey.org/docs/troubleshooting for more
assistance.
...and the same 404 error when using v2:
choco source add -n=schily-artifacts `
>> -s="https://flapjacks.pkgs.visualstudio.com/_packaging/schily-artifacts/nuget/v2" `
>> -u="xxxxxxxx" -p="xxxxxxx"
Trying to list with the v2 registration comes up with no packages found, however when running choco list with the v3 it comes back with a 404 error.
Finally, trying with Install-Package failes with a credentials issue:
Install-Package schily-cdrtools
WARNING: Cannot access 'https://flapjacks.pkgs.visualstudio.com/_packaging/schily-artifacts/nuget/v3/index.json'. Are you missing 'Credential' parameter in the cmdlet?
Install-Package : No match was found for the specified search criteria and package name 'schily-cdrtools'. Try Get-Packa
geSource to see all available registered package sources.
At line:1 char:1
+ Install-Package schily-cdrtools
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Microsoft.PowerShel\u2026lets.InstallPackage:InstallPackage) [Install-Package
], Exception
+ FullyQualifiedErrorId : NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage
Any ideas no what I'm missing?
I've been using the following as a guide:
https://blog.pauby.com/post/chocolatey-repository-using-azure-devops-artifacts-feed/

Cannot access Chocolatey package from Azure DevOps Artifact Feed
I could not reproduce this issue after created a sample based on the steps in that guide. But I would like provide some troubleshooting to you:
Check your Chocolatey version, mine is Chocolatey v0.10.15.
Use PAT instead of password when you add Chocolatey Azure DevOps Feed named source.
Run the command line with Administrator.
The command line should be complete and does not require a branch, like:
choco source add -n=MyCustomFeed -s="https://pkgs.dev.azure.com/<MyOrgName>/_packaging/<FeedName>/nuget/v2" -u="MyAccount.com" -p="PAT"
My Test Result:
Hope this helps.

The PackageManagement cmdlets are broken currently so using Install-Package without the -Credential parameter will fail. You need to add the -Credential parameter to all of the PackageManagement cmdlets for them to work.
If I run (exactly):
choco source add -n=schily-artifacts -s="https://flapjacks.pkgs.visualstudio.com/_packaging/schily-artifacts/nuget/v2" -u="xxxxxxxx" -p="xxxxxxxxxxx"
Then I get:
Chocolatey v0.10.15
Added schily-artifacts - https://flapjacks.pkgs.visualstudio.com/_packaging/schily-artifacts/nuget/v2 (Priority 0)
Chocolatey doesn't validate the feed until you use it:
choco list --source=schily-artifacts
Chocolatey v0.10.15
Invalid credentials specified.
Invalid credentials specified.
Invalid credentials specified.
Invalid credentials specified.
Invalid credentials specified.
Invalid credentials specified.
Invalid credentials specified.
Invalid credentials specified.
[NuGet] Not able to contact source 'https://flapjacks.pkgs.visualstudio.com/_packaging/schily-artifacts/nuget/v2'. Error was The remote server returned an error: (401) Unauthorized.
Invalid credentials specified.
Invalid credentials specified.
Invalid credentials specified.
Invalid credentials specified.
Invalid credentials specified.
Invalid credentials specified.
Invalid credentials specified.
Invalid credentials specified.
[NuGet] Not able to contact source 'https://flapjacks.pkgs.visualstudio.com/_packaging/schily-artifacts/nuget/v2'. Error was The remote server returned an error: (401) Unauthorized.
0 packages found.
Which is what I'd expect (as the credentials are invalid). So I'm not sure where your errors are coming from when adding the source.

Another approach with Credential Provider, slightly modified version of the script that was publish in github.com/chocolatey/choco/issues/1721 (last lines were changed to use choco instead of nuget command).
The script runs a Credential Provider (which prompts you to login to Azure DevOps and automatically generate a PAT). The returned PAT is used with the same choco command as in Leo Liu's answer
param (
[Parameter(Mandatory=$true)]
[string]$feedName,
[Parameter(Mandatory=$true)]
[string]$url
)
$profilePath = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::UserProfile)
$pluginLocation = [System.IO.Path]::Combine($profilePath, ".nuget", "plugins");
$localNetfxCredProviderPath = [System.IO.Path]::Combine("netfx", "CredentialProvider.Microsoft");
$fullNetfxCredProviderPath = [System.IO.Path]::Combine($pluginLocation, $localNetfxCredProviderPath)
$netfxExists = Test-Path -Path ($fullNetfxCredProviderPath)
if($netfxExists -eq $false)
{
Write-Host "Installing credential provider..."
iex "& { $(irm https://aka.ms/install-artifacts-credprovider.ps1) } -AddNetfx"
$netfxExists = Test-Path -Path ($fullNetfxCredProviderPath)
if($netfxExists -eq $false)
{
Write-Host "Credentials provider is required."
Write-Host "Maybe install manually from https://github.com/microsoft/artifacts-credprovider/releases"
return
}
}
$exe = [System.IO.Path]::Combine($fullNetfxCredProviderPath, 'CredentialProvider.Microsoft.exe')
# It's not possible to disable device based authentication, but we can set timeout to fail immidiately.
$ENV:NUGET_CREDENTIALPROVIDER_VSTS_DEVICEFLOWTIMEOUTSECONDS=0
# Validity period: 1 year. (Can have larger value)
$ENV:NUGET_CREDENTIALPROVIDER_VSTS_SESSIONTIMEMINUTES=365*24*60
# '-V Error' is required not to get extra messages
$msg = & $exe -V Error -U $url -C -F JSON 2>&1 | Out-String
if($lastexitcode -ne 0)
{
Write-Host
Write-Host "$msg"
Write-Host
Write-Host "Failed to run '$exe' ($lastexitcode)"
return
}
try
{
$info = ConvertFrom-Json $msg
}
catch
{
Write-Host
Write-Host "$msg"
Write-Host
return
}
choco source add -v -n="$feedName" -s="$url" -u "$($info.Username)" -p="$($info.Password)"

Related

How to fix Nuget Provider "Find-Module" Installation error with PowerShell 7.3?

I've been trying to run a PowerShell script, and upon doing so, I receive a message that NuGet Provider is required.
NuGet provider is required to continue
This version of PowerShellGet requires minimum version '2.8.5.201' of NuGet provider to publish an item to NuGet-based
repositories. The NuGet provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or
'C:\Users\timothy.granata\AppData\Local\PackageManagement\ProviderAssemblies'. You can also install the NuGet provider
by running 'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force'. Do you want PowerShellGet to install
and import the NuGet provider now?
[Y] Yes [N] No [S] Suspend [?] Help (default is "Y"):
If I input Y, an error is returned:
Find-Module: NuGet provider is required to interact with NuGet-based repositories. Please ensure that '2.8.5.201' or newer version of NuGet provider is installed.
If I try running Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force as it recommends, I also get an error:
Install-PackageProvider: Unable to find repository with SourceLocation ''. Use Get-PSRepository to see all available repositories.
And finally, if I run Get-PSRepository, that also errors:
Get-PackageSource: Unable to find module providers (PowerShellGet).
In the script I am trying to debug, the code that seems to trigger this prompt is Install-AWSToolsModule SecurityToken -Force. The surrounding code looks like:
if (-not (Get-Module AWS.Tools.Installer -ListAvailable)) {
Install-Module AWS.Tools.Installer -Force
}
Install-AWSToolsModule SecurityToken -Force
Get-AWSCredential -ListProfileDetail | ForEach-Object {
Remove-AWSCredentialProfile -ProfileName $_.ProfileName -Force
}
I have tried:
Reinstalling PowerShell 7
Making sure I am using TLS 1.2 by running [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Running PowerShell as Administrator
Deleting the Modules folder found in my C:\Users<user>\Documents\WindowsPowerShell folder
I'm unsure what else I can try at this point. How can I install the NuGet provider for use with PowerShell 7.3?
Try
$sourceArgs = #{
Name = 'nuget.org'
Location = 'https://api.nuget.org/v3/index.json'
ProviderName = 'NuGet'
}
Register-PackageSource #sourceArgs
Get-PackageProvider | where name -eq 'nuget' | Install-PackageProvider
EDIT
Perhaps try
Invoke-WebRequest 'https://www.powershellgallery.com/api/v2/package/PackageManagement/1.4.8.1' -OutFile $env:temp\nuget.zip
And confirm you're able to download the nuget package. If so, then try
Expand-Archive $env:temp\nuget.zip -DestinationPath 'C:\Program Files\PowerShell\7\Modules\PackageManagement' -Force
Import-Module PackageManagement -Verbose -Force
It seems something with OneDrive was indeed throwing this off as I wondered in my one comment. I found this post which stated (from some Microsoft Documentation):
The user-specific CurrentUser location on Windows is the PowerShell\Modules folder located in the Documents location in your user profile ... Microsoft OneDrive can also change the location of your Documents folder.
I ran $env:PSModulePath and sure enough their was a OneDrive location. I ended up doing what the answer on that post suggested, and excluded the PowerShell directory from OneDrive. After doing this, my script seems to work now (it doesn't produce errors, or that prompt). Ever after doing this, the OneDrive location still shows up from the $env:PSModulePath command, but I guess it falls back to the next modules location if it can't find a directory.

Unable to publish to Azure devops Nuget

I have built a module that uses both PSGallery and Azure Devops (I have a free repo up and I have been playing with it). This is the module: https://www.powershellgallery.com/packages/xanderu.helpers/0.1.9
The problem I'm running into is that when I try to leverage the secure repo, I keep getting a forbidden error. This is the way I'm calling the module:
Publish-ToPSGallery -PSGalleryKey $PSGalleryKey `
-secureRepoName 'PowershellAZDO' `
-secureRepopublishApiKey $PATToken `
-secureRepopublishApiURL "https://pkgs.dev.azure.com/$orgName/$projectName/_packaging/$feedName/nuget/v2/" `
-secureRepoUser $secureRepoUser `
-secureRepoPass $PATToken `
-codePath .
Its a pretty strait forward module but basically create a module (or script) that has the tag 'private' and it should publish to the PSGallery or AZDO accordingly. I'm sure it has something to do with how I'm using the token and credentials (or the API key). Does anyone have any idea what I'm doing wrong here?
EDIT: As part of the xanderu.helpers there is a function new-powershelltemplate and it will auto build a module manifest or script base line that can be used for the publish pipeline
ADDITIONAL EDIT:
I have also tried the following to push to myget nuget and it pushes but it wont find the module using find-module -repository myget:
Publish-ToPSGallery -PSGalleryKey $PSGalleryKey `
-secureRepoName 'MyGet' `
-secureRepopublishApiKey $nugetAPIKey `
-secureRepopublishApiURL "https://www.myget.org/F/$feedName/api/v2" `
-secureRepoUser $secureRepoUser `
-secureRepoPass $secureRepoPass `
-codePath .
Edit #3
I did a little more messing around and when I run
nuget.exe sources
After I run the script, I can see that nuget gets PowershellAZDO as a source. Then I run:
nuget push -Source "PowershellAZDO" -ApiKey AzureDevOps .\xanderu.helpers.0.1.9.nupkg
and I get prompted for a username and password. When I type out the username and PATToken, it pushes the package. This appears to be a bug in the Publish-Module command but I'm unsure as to why. It is like the PSCredential that the module is passed isn't honored. I kept digging and found that in the PowerShellGet v2.2.5 PSModule.psm1 file, there is a call to (line 10990):
Publish-PSArtifactUtility #PublishPSArtifactUtility_Params
and I do in fact see the PSCredential object in there. I've added the following step to the PowerShellGet module that seems to fix the issue... but this is a bug in the source it would appear (this starts on line 6018):
elseif ($NuGetExePath) {
& $NuGetExePath sources update -Name $Repository -UserName $Credential.Username -Password $Credential.GetNetworkCredential().Password
Publish-NugetPackage -NupkgPath $NupkgFullName -Destination $Destination -NugetApiKey $NugetApiKey -NugetExePath $NuGetExePath -Verbose:$VerbosePreference
}
NuGet Version: 5.7.0.6726
PowerShellGet Version: 2.2.5
This is caused by a bug in the Powershellget module v 2.2.5 that I am using. The root cause is because the repo doesn't have a password passed in, you need to add the password to the configs for nuget. This appears to be resolved in the 3.0.0 version of PowerShellGet. I will need to update my manifest/modules to force the load of that version of the module to resolve the issue.
Edit: scratch that... its looks like I thought this worked but it was because my nuget.exe creds were preserved.
Edit2:
This works without fail and doesn't require screwing with the core modules. After running the Register-PSRepository command, ensure you run the following as well or the publish pipeline to Azure Devops won't work. This is a bug in the module as it works other places.
nuget sources update -Name $secureRepoParams.Name -UserName $secureRepoParams.Credential -Password $secureRepoParams.Credential.GetNetworkCredential().Password

Powershell command Register-PSRepository ends in error

I am getting error while running the following snippet:
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
}
And the error that I am getting is:
Register-PSRepository : The specified Uri '....' for parameter 'SourceLocation' is an invalid Web Uri. Please ensure that it meets the Web Uri requirements.
It is already checked that the Url is valid.
It seems that it is already discussed here and still an open issue and has been sitting in limbo for quite a while
See this issue
I was wondering if there is workaround and your input will be valuable. I basically need to iterate the JFrog repository contents, publish nuget artifacts and wanted to do it by the following:
Jfrog Powershell
Now I am pondering the following option and it would be great to have your feedback:
Download & install the jfrog cli
Call jfrog commands from powershell to operate on the artifactory with artifacts.

AzureRmRoleAssignment Access denied to the specified API version

I'm getting an error running New-AzureRmRoleAssignment. I want to give an AD group access to a resource group. The script actually works-- the group gets contributor access to the resource group. It just says that it's failing with the message "Access denied to the specified API version".
My script (params not included) is here:
# Import the Task.Common dll that has all the cmdlets we need for Build
import-module Microsoft.TeamFoundation.DistributedTask.Task.Common
import-module Microsoft.TeamFoundation.DistributedTask.Task.Internal
Import-Module "Microsoft.TeamFoundation.DistributedTask.Task.Deployment.Internal"
Import-Module "Microsoft.TeamFoundation.DistributedTask.Task.Deployment.Azure"
Write-Output "Connecting to Azure"
Initialize-AzurePowershellSupport -ConnectedServiceName $ConnectedServiceName -ErrorAction SilentlyContinue
$subscription = (Get-AzureRmContext).Subscription.SubscriptionName #(Get-AzureRmContext).Subscription.SubscriptionName
New-AzureRmRoleAssignment -ObjectId $objID -RoleDefinitionName $roleName -ResourceGroupName $environment-$featureName
How can I fix the error? The script does what it's supposed to, but the build "fails".
According to the error log, do you login Azure by using service principal. If yes, it is a know issue. Please check the issue on GitHub. The issue is solved on the latest version Azure Power Shell(4.1.0 or later).
You could use the following cmdlet to check your Azure PowerShell version.
Get-Module -ListAvailable -Name Azure -Refresh
The latest version is 4.3.1, you could download it from the link.
If you build the script on VSTS, please use Hosted 2017 build agent, it uses the latest version PowerShell. Please refer to this answer.

Get-AzureWebsite : Requested value 'Dynamic' was not found

We have a set of custom powershell modules which use the Azure powershell cmdlets - they have been working fine for over a year. I just set up a new machine and whenever I try to run Get_AzureWebsite I receive the following error:
PS C:\WINDOWS\system32> Get-AzureWebsite 'anything'
Get-AzureWebsite : Requested value 'Dynamic' was not found.
This may just be a machine setup but am worried that these comdlets may be being deprecated - appreciate if anyone can help or knows how to fix this?
It may be fixed by updating the version of Azure PowerShell . More detail please refer to the issue and feedback. Please refer to how to install and configure Azure PowerShell. I didn't reproduce it on the Azure PowerShell v2.1.0. It works successfully.Please try to use the following code to get the current Azure PowerShell version .
(Get-Module -ListAvailable | Where-Object{ $_.Name -eq 'Azure' }) `
| Select Version, Name, Author, PowerShellVersion | Format-List;
Okay so this is versions of Azure and AzureRM cmdlets.
Working install is
Install-Module -Name AzureRM -RequiredVersion 1.3.2
Install-Module Azure -AllowClobber
Not sure about -AllowClobber but this was printed in the Azure Console....
PackageManagement\Install-Package : A command with name 'Get-AzureStorageContainerAcl' is already available on this
system. This module 'Azure' may override the existing commands. If you still want to install this module 'Azure', use
-AllowClobber parameter.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:1661 char:21
Guess there is a class in the Storage namespace or something
I installed the latest azure powershell and then ran again the script.
It worked fine.
Just to add you need to restart your machine after installing latest powershell, else you might face error "the required module Azure.Storage is not loaded"