This is a simple workaround solution for a complicated problem with registering VisualStudio online NuGet packages repositories as PowerShell module repositories.
PowerShellGet just doesn't work with NuGet CredentialProviders, because it uses the -noninteractive switch whenever it calls nuget. However, the VisualStudio.com (VSO) nuget package repositories can work as internal PowerShell Galleries, because passing the credentials to Find-Module and Install-Module does work ...
Basically, the problem is two fold: authentication when publishing, and authentication when searching or installing. The second problem can be dealt with, inconveniently, by passing credentials every time. But the first cannot. These VSO repositories require a Credential Provider plugin
to handle Microsoft Authentication (including support for 2-factor auth). This version of the credential provider works with nuget.exe
(there's a different version for Visual Studio), which is what we want, because PowerShellGet just wraps the nuget.exe
executable for publishing...
When publishing a module, PowerShellGet calls nuget push -noninteractive
, so the Microsoft Visual Studio Team Services Authentication credential provider doesn't work at that point -- you just get an error saying it can't prompt in non-interactive mode. The work-around for that is that you can get a Personal Access Token (PAT), and set that key as the credential for the repository when you register it. Frustratingly, PowerShellGet doesn't help us with that workaround, because it uses the Package Management NuGet provider (which is download-only) for everything except publishing, and it doesn't ever call nuget.exe when you register a PSRepository. It only registers it with the PackageManagement NuGet provider, which doesn't appear to work with these credential providers.
The script here basically handles both fetching the VSO credential manager, and putting it in the right place, and registering your VSO repository with the PAT (API key). This solves the publishing problem, but not the searching problem -- your credentials still won't be cached for searching.
The process is pretty simple:
- Get a personal access token that allows "Packaging (read and write)" or "Packaging (read, write, and manage)" -- note the "Expires In" setting, because you're going to have to do this over again every time the token expires.
- Run Set-VsoNuget, specifying the URL and Name for your repository, and the credentials, where the password in the personal access token from the first step.