Unable to publish to Azure devops Nuget - powershell

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

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.

PowerShellGet "unable to find package source" upon Install-Module, despite Find-Module working correctly

I recently created an Azure Artifacts feed for one of the projects in my organization. I followed the instructions at this link.
Most things work. In particular, I'm able to:
Create the feed
Use nuget to push a package to said feed
Register the feed with PowerShell via Register-PSRepository
Run Find-Module -Repository <Repository Name>
The last bit -- running Find-Module -- generates the expected output, viz., a single module with the expected version, name, and description. However, I get the following message when I attempt Install-Module:
PS C:\Windows\System32> install-module -name ADHelpers -Repository 'ADHelpersTest2'
Install-Package: C:\Users\my_name\OneDrive\Documents\PowerShell\Modules\PowerShellGet\2.2.5\PSModule.psm1:9711
Line |
9711 | … talledPackages = PackageManagement\Install-Package #PSBoundParameters
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Unable to resolve package source
| 'https://pkgs.dev.azure.com/my_organization/my_project/_packaging/my_feed/nuget/v2'.
I've scrubbed my organization, project, and feed name, as I don't think that's important for solving this problem.
My question is simple: What could cause this? I'm confused that I'm able to Find-Module just fine, but unable to run Install-Module for the result that Find-Module successfully fetches.
I've been unsuccessful in finding guidance elsewhere, and am unsure what I'm doing wrong.
Thanks in advance -- I appreciate your time.
If you want to install the custom package in the Azure DevOps pipeline, you could add task power shell and run the script Install-Package ADHelpers -Scope CurrentUser -Force
to install the package.
In my sample, the package name is Get-Hello and the result is:
You could refer to this blog and my previous answers for more details

Is it possible to use Register-PSRepository with private gitlab server (internal/private project)

I have an issue with adding repository in powershell with
Register-PSRepository -name gitlab -PublishLocation https://gitlab.private.com/api/v4/projects/3280491/packages/nuget/index.json -SourceLocation https://gitlab.private.com/api/v4/projects/3280491/packages/nuget/index.json -InstallationPolicy Trusted -Credential (Get-Credential) -PackageManagementProvider NuGet
credential user: my gitlab username, password: my generated access token with api privilege.
If then i will want to query modules inside:
find-module -Repository gitlab
WARNING: Query Url https://gitlab.private.com/api/v4/projects/2797/packages/nuget/index.json is invalid.
it throws me this error.
I have tried the same with my account on gitlab.com with a free account. And i have the same issue.
There is no issue if i will create a project that is available to everyone. This happens if project is internal/private.
If i will use nuget , then i have no issue with publishing my module to gitlab / private gitlab, i have no issue with doing nuget install ... .
Any idea why this would not work with powershell cmdlets ?
I am not sure if i understood some messages i found on internet about issues with powershellget, as if it would have issues with storing credentials.
Maybe someone seen this issue, and understands it more and could explain ?
I can find the module inside the repository:
Find-Module -Repository gitlab -Credential $mycred
Version Name Repository
------- ---- ----------
0.4.2 PrivateAutomation gitlab
But when piped to Install-module it does not work
DEBUG: 00:00:01.7085410 Completed downloading 'https://gitlab.private.net/api/v4/projects/97/packages/nuget/metadata/privateautomation/0.4.2.json'.
WARNING: An internal error occurred: The JSON object returned by the NuGet API does not match expected schema. Expected property: 'type'. Check the debug stream for the full JSON object dump.
I am running powershell v7.1 / powershellget 2.2.5
Thank you.
GitLab uses nuget v3 endpoint for the repository. v3 is not supported in the current stable version of PowerShellGet, but it is in PowerShellGet 3.0 which is in preview at the time of writing.

Connect-AzureAD not working with Powershell core

I am trying to run a powershell command - ConnectAzureAD and getting the below error-
'Could not load file or assembly 'Microsoft.IdentityModel.Clients.ActiveDirectory, Version=3.19.7.16602, Culture=neutral,. Could not find or load a specific file.'
This was working earlier with Powershell 5 but not with powershell core.The versions that i am using are as:
Powershell - 7.0.1
Az.Accounts - 1.8.1 (i have tried updating this but no luck)
AzureAd - 2.0.2.104
Is there any workaroudn for this ? We tried Azure.Standard.Preview from 'Post test Gallery' but it failed the keyVault powershell commands. Any help on this?
Install-Module -Name AzureADPreview -RequiredVersion 2.0.2.89
Import-Module AzureADPreview -Version 2.0.2.89 -UseWindowsPowerShell
As Shiva said, this is a known limitation on .NET CORE that new version assembly cannot be loaded if old version is already loaded. PowerShell is considering module isolation but so far there is no good solution yet.
You can upgrade Microsoft.IdentityModel.Clients.ActiveDirectory to the latest version.
For more details, you could refer to this issue.
You could instead try to use the az rest invoking the graph api.
Until the time az-cli is in par with the AzureAD powershell module you can instead use the graph api
az login --tenant <tenantname.onmicrosoft.com>
$uri = "https://graph.microsoft.com/v1.0/applications"
$allApplications = az rest `
--method GET `
--uri $uri `
--headers 'Content-Type=application/json' | convertfrom-json
$allApplications.value |% {"{0}-{1}" -f $_.appid, $_.displayname}
I have put some samples using az rest here,
https://github.com/joepaulk/utilities/blob/master/manage-azuread-applicationregistrations.ps1
You may also refer to: https://damienbod.com/2020/06/22/using-azure-cli-to-create-azure-app-registrations/ from where i picked up inspiration
Other reference, how az rest use the accesstokens from az cli can be found here,
https://mikhail.io/2019/07/how-azure-cli-manages-access-tokens/

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"