1
votes

I have a scheduled task which runs an exported function from my PowerShell module which is hosted at powershellgallery.com. The function performs a check against the built-in PSGallery repository to see if a newer version is available and if so, update it.

I've noticed my module is not being updated as it should and to troubleshoot the issue I've redirected the output from two separate commands. First, to make sure the repository is 'visible' to the SYSTEM account running the task I run:

Get-PSRepository *>> c:\repo.log

This yields the following output:

Name InstallationPolicy SourceLocation
---- ------------------ --------------
PSGallery Untrusted https://www.powershellgallery.com/api/v2

So the SYSTEM account running the function as a scheduled task can 'see' the repo; no problem. Next, the function runs the Update-Module command as such:

Update-Module -Name $ProductName -Confirm:$false -Force -Verbose *>> c:\update.log

This yields the following output:

Checking for updates for module '[removed by me]'. PackageManagement\Install-Package : Unable to find repository 'https://www.powershellgallery.com/api/v2/'. Use Get-PSRepository to see all available repositories. At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.0.4\PSModule.psm1:12546 char:20 + ... $sid = PackageManagement\Install-Package @PSBoundParameters + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Microsoft.Power....InstallPackage:InstallPackage) [Install-Package], Ex ception + FullyQualifiedErrorId : SourceNotFound,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage

Lastly, I checked the module to make sure it is indeed associated with PSGallery by running:

Get-InstalledModule -Name $ProductName | fl

The output shows:

RepositorySourceLocation : https://www.powershellgallery.com/api/v2/

Repository : PSGallery

UPDATE: I decided to use the Install-Module with the -Force switch to 'update' the module instead as I couldn't get the other command to work. Oddly though, when I do a Get-InstalledModule -AllVersions I can clearly see a difference between a module installed interactively and one installed under the SYSTEM account (running as a scheduled task). Pay attention to the Repository column:

PowerShell Repo

  1. If I run the function interactively it works without issue.
  2. If I run Find-Module -Name $ProductName from within the function it finds the module without issue.
  3. Tried both solutions from other question to no avail...

Any idea why the Update-Module command can't find the repo??

1
Potential duplicate of (Unable to find repository on Update-Module) stackoverflow.com/questions/53358723/… - postanote
Thank you but those answers were attempted as well. I'll update my post accordingly. - Jason Shave

1 Answers

2
votes

I believe you are seeing the bug described in https://github.com/PowerShell/PowerShellGet/issues/349 . It's not really related to SYSTEM account. Simplest workaround until fixed version is released is to uninstall the module then reinstall it again. You should only have to do that once, and update-module should work thereafter.

UPDATE: This is resolved in newer builds of PowerShellGet.