Update-Module not finding PSGallery repo in script as a scheduled task - powershell

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:
If I run the function interactively it works without issue.
If I run Find-Module -Name $ProductName from within the function it finds the module without issue.
Tried both solutions from other question to no avail...
Any idea why the Update-Module command can't find the repo??

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.

Related

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

Warnings When Attempting to Install Sysinternals With Powershell

I am trying to install the sysinternals package with powershell, but is instead greeted with tons of warnings. It should be noted that I am following a set of instructions and don't know a lot about this subject yet. Here are the warnings when attempting to run the install-package command:
Install-Package sysinternals
WARNING: NuGet: System.InvalidOperationException: Unable to find version '1.3.5.1' of package 'chocolatey-core.extension'.
WARNING: NuGet: at NuGet.PackageRepositoryHelper.ResolvePackage(IPackageRepository sourceRepository, IPackageRepository
localRepository, IPackageConstraintProvider constraintProvider, String packageId, SemanticVersion version, Boolean
allowPrereleaseVersions)
WARNING: NuGet: at NuGet.PackageManager.InstallPackage(String packageId, SemanticVersion version, Boolean ignoreDependencies,
Boolean allowPrereleaseVersions)
WARNING: NuGet: at NuGet.Commands.InstallCommand.InstallPackage(IFileSystem fileSystem, String packageId, SemanticVersion version)
WARNING: NuGet: at NuGet.Program.Main(String[] args)
WARNING: NuGet: System.InvalidOperationException: Unable to find version '2019.12.19' of package 'sysinternals'.
As for what I've done before this, I've only installed the Chocolatey package provider and updated windows, but maybe I did something wrong? Here are the steps leading up to this:
Set-ExecutionPolicy RemoteSigned
Install-PackageProvider Chocolatey
Install-Module -Name PSWindowsUpdate
Get-WUInstall -Verbose
Get-WUInstall -Install
Any help is much appreciated.
Though you can use Chocolatey and NuGet, PowerShell's package manager is PowerShellGet (well, that uses NuGet by design). So, Choco is not really needed, yet, many still install and use it for the other sources.
If you use PowerShellGet directly, you'll see the same errors.
Find-Module -Name 'SysInternals' -AllVersions |
Format-Table -AutoSize
<#
# Results
PackageManagement\Find-Package : No match was found for the specified search criteria and module name 'SysInternals'. Try Get-PSRepository to see all available registered module
repositories.
At C:\Users\Daniel\Documents\WindowsPowerShell\Modules\PowerShellGet\2.2.3\PSModule.psm1:8873 char:9
+ PackageManagement\Find-Package #PSBoundParameters | Microsoft ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Microsoft.Power...ets.FindPackage:FindPackage) [Find-Package], Exception
+ FullyQualifiedErrorId : NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.FindPackage
#>
Find-Package -Name 'SysInternals' -AllVersions |
Format-Table -AutoSize
<#
# Results
Find-Package : No match was found for the specified search criteria and package name 'SysInternals'. Try Get-PackageSource to see all available registered package sources.
At line:1 char:1
+ Find-Package -Name 'SysInternals' -AllVersions |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Microsoft.Power...ets.FindPackage:FindPackage) [Find-Package], Exception
+ FullyQualifiedErrorId : NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.FindPackage
#>
Yet, if you do a wildcard search...
Find-Module -Name '*Internals*' |
Format-Table -AutoSize
<#
# Results
Version Name Repository Description
------- ---- ---------- -----------
1.0.34 PoshInternals PSGallery Collection of system internals tools for PowerShell.
4.3 DSInternals PSGallery The DSInternals PowerShell Module exposes several internal features of Active Directory and Azure Active Directory. These include FIDO2 and NGC key aud...
0.2.8 AADInternals PSGallery The AADInternals PowerShell Module utilises several internal features of Azure Active Directory, Office 365, and related admin tools. DISCLAIMER: Funct...
0.1 LCMInternals PSGallery Demo scripts explaining the internals of LCM
#>
Find-Package -Name '*Internals*' |
Format-Table -AutoSize
<#
# Results
Name Version Source Summary
---- ------- ------ -------
PoshInternals 1.0.34 PSGallery Collection of system internals tools for PowerShell.
DSInternals 4.3 PSGallery The DSInternals PowerShell Module exposes several internal features of Active Directory and Azure Active Directory. These incl...
AADInternals 0.2.8 PSGallery The AADInternals PowerShell Module utilises several internal features of Azure Active Directory, Office 365, and related admin...
LCMInternals 0.1 PSGallery Demo scripts explaining the internals of LCM
SilverlightToolkit-Internals-Unofficial 1.0.0 nuget.org The missing part of Microsoft Silverlight Toolkit. The internals DLL. 'System.Windows.Controls.Toolkit.Internals'
AppInternals.Agent.Cloud.Support 10.10.0 nuget.org Configure an Azure Cloud Service solution for AppInternals application performance monitoring.
InternalsVisibleTo.MSBuild 1.0.3 nuget.org Enables declaring 'InternalsVisibleTo' items in a .NET project file, rather than declaring them to an AssemblyInfo.cs file.
Akrual.DDD.Utils.Internals 1.0.8 nuget.org Useful Classes yo use everywhere
Meziantou.MSBuild.InternalsVisibleTo 1.0.1 nuget.org Allow to declare 'InternalsVisibleTo' in the csproj file, rather than declaring them to an AssemblyInfo.cs file.
microServiceBus.InternalService 1.0.0 nuget.org This package creates a stub from which you can create an Internal Service for microServiceBus.com
#>
So, as you can see, there is no such module/package called SysInternals to install.
What you do is just download the zip file from here and unpack and use it as normal.
Sysinternals Suite
https://learn.microsoft.com/en-us/sysinternals/downloads/sysinternals-suite
I was having the same issue and found the answer here helpful. It seems like something about TLS 1.3 is causing the issue, and the second part of the answer—changing the .Net Framework default TLS version—allowed me to finish my sysinternals install via PowerShell. Then I just deleted the new reg key I had created. I guess I'll just have to wait and see if this is something that comes up again as I continue to learn more and work more with package management in PS.

Publish-PSArtifactUtility cannot resolve module dependency when publishing even when they are in the same feed

I'm trying to publish a PowerShell module to Azure Artifacts which has a dependency on another module that is hosted in the same Artifacts feed. The idea is that when I install the module locally from the feed, the correct version of the dependency is installed automatically. The problem is that when I run:
Publish-Module -NuGetApiKey "{token}" -Path {path} -Repository "{ArtifactsFeedName}" -Verbose -ErrorAction Stop
I get the following error:
Publish-PSArtifactUtility : PowerShellGet cannot resolve the module dependency '{dependency}' of the module
'{moduleWhichHasDependency}' on the repository '{ArtifactsFeed}'. Verify that the dependent module '{dependency}'
is available in the repository '{ArtifactsFeed}'. If this dependent module '{dependency}' is managed externally,
add it to the ExternalModuleDependencies entry in the PSData section of the module manifest.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:1190 char:17
+ Publish-PSArtifactUtility -PSModuleInfo $moduleInfo `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Publish-PSArtifactUtility], InvalidOperationException
+ FullyQualifiedErrorId : UnableToResolveModuleDependency,Publish-PSArtifactUtility
In the manifest, the dependency is added like this:
RequiredModules = #( #{ModuleName = '{dependency}'; ModuleVersion = '1.0.4'; })
Then I tried adding:
ExternalModuleDependencies = #( #{ModuleName = '{dependency}'; ModuleVersion = '1.0.4'; })
The module is published but when I install it locally, it doesn't install the dependency.
This issue seems to have been fixed in the latest version of PowershellGet (2.2 in the PSGallery, version installed on my computer was 1.0.1).
You could also install the latest version of PackageManagement (1.4.3 vs 1.0.1)
See :
Added credential parameter to subsequent calls of Publish-Module/Script. #93
https://github.com/PowerShell/PowerShellGet/pull/93
Implement Publish-Module credential parameter propagation to sub-functions #104
https://github.com/PowerShell/PowerShellGet/pull/104
To do this :
Install-Module PowershellGet -Force #-Scope Current #if you don't want to login as Administrator
However, I have a question for you : how do you manage not to give your Credentials when you call Publish-Module with an Azure Artifact feed ? I know they should be stored in the file nuget.config but I have to give them each time.
Publish-Module -NuGetApiKey "{whatever}" -Credentials "{PSCredentials object}" -Path {path} -Repository "{ArtifactsFeedName}" -Verbose -ErrorAction Stop
expanding on #clientGOOG answer, my version was 2.2.4.1 but i had the same issue, i am publishing to myget and i solved this problem by using a pre-authenticated thread. I did not have to add ExternalModuleDependencies settings to make this work.
With the -Verbose flag I noticed that the "find" process was retrying 3x before giving up.
VERBOSE: Using the provider 'PowerShellGet' for searching packages.
VERBOSE: Using the specified source names : '{myrepo}'.
VERBOSE: Getting the provider object for the PackageManagement Provider 'NuGet'.
VERBOSE: The specified Location is 'https://{myorg}.myget.org/F/{myfeed}/api/v3/index.json' and PackageManagementProvider is 'NuGet'.
VERBOSE: Retry downloading 'https://{myorg}.myget.org/F/{myfeed}/api/v3/index.json' for '2' more times
VERBOSE: Retry downloading 'https://{myorg}.myget.org/F/{myfeed}/api/v3/index.json' for '1' more times
VERBOSE: Retry downloading 'https://{myorg}.myget.org/F/{myfeed}/api/v3/index.json' for '0' more times
VERBOSE: Total package yield:'0' for the specified package '{dependent module}'.
Here is my revised code that solved the issue:
Register-PSRepository -Name {MyRepo} "https://{myorg}.myget.org/F/{myfeed}/auth/{apikey}/api/v2"
Publish-Module -Path ($module.path |Split-Path -Parent) -NuGetApiKey $APIKey -Repository {MyRepo} -Verbose -ErrorAction Stop
Note: it still required the NuGetApiKey on the publish but the authenticated feed i would assume is passed down to the "find" call that is failing which is why it now works.

powershell update-help fails

I just started to learn PowerShell from the Microsoft virtual academy and I was running one of the commands indicated. while on their end it worked mine did not. I did look around for a solution to this issue. I just don't know what went wrong. any tips will helpful for this new PowerShell learner.
PS C:\Windows\system32> Update-Help -Force
Update-Help : Failed to update Help for the module(s) 'WindowsUpdateProvider' with UI culture(s) {en-US} : Unable to retrieve the HelpInfo XML file for UI culture en-US. Make sure the
HelpInfoUri property in the module manifest is valid or check your network connection and then try the command again.
At line:1 char:1
+ Update-Help -Force
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [Update-Help], Exception
+ FullyQualifiedErrorId : UnableToRetrieveHelpInfoXml,Microsoft.PowerShell.Commands.UpdateHelpCommand
PS C:\Windows\system32> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.17134.228
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.17134.228
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Agreed, that this should be a SuperUser post, but since it is here.
This sort of error common and in most cases expected.
Not all the help files, update as expected for various reasons, most of the time its do to the update link associated. As shown in your error message.
Many module either have no online updateable help or the URL has been removed.
These sorts of error can be safely ignored. They do not impact PS functionality or use.
Get-Module -ListAvailable | Where HelpInfoUri | Update-Help
Or if you want to see all the message going back and forth with this, do...
Update-Help -Force -Verbose -ErrorAction SilentlyContinue
# Results
VERBOSE: Resolving URI: "http://go.microsoft.com/fwlink/?linkid=390758"
VERBOSE: Your connection has been redirected to the following URI: "http://download.microsoft.com/download/0/1/C/01CCC594-2F13-40E8-98FE-185486228BF4/"
VERBOSE: Performing the operation "Update-Help" on target "CimCmdlets, Current Version: 5.0.0.0, Available Version: 5.0.0.0, UICulture: en-US".
If you want to see the full error message in a more human readable for, do this...
Update-Help -Force -Ea 0 -Ev ErrMsgDetail
$ErrorMsgDetail.Exception
Failed to update Help for the module(s) 'AnyBox' with UI culture(s) {en-US} : Unable to connect to Help content. The server on which Help content is stored might not be available.
Verify that the server is available, or wait until the server is back online, and then try the command again.
Failed to update Help for the module(s) 'HostNetworkingService, WindowsUpdateProvider' with UI culture(s) {en-US} : Unable to retrieve the HelpInfo XML file for UI culture en-US.
Make sure the HelpInfoUri property in the module manifest is valid or check your network connection and then try the command again.
Actually the problem might also be related to privileges. At least it was for me. On a default Windows 10 install, Update-Help would not work at all, nor did all the other versions with -Force or -ErrorAction.
It turned out, also according to the official online help, you need to be administrator to update the help for powershell <6.0 (which Windows 10 defaults to PS5.1). Starting a new PowerShell with admin privileges immediately removed the problems and I could also view the help as a non-admin.
TL;DR:
Open Powershell, paste the command below and run Update-Help and it should download all AVAILABLE help files:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
It will a give a red warning for the modules that did not have any help files like:
update-help : Failed to update Help for the module(s) 'ConfigDefender, ConfigDefenderPerformance, PSReadline,
WindowsUpdateProvider' with UI culture(s) {en-US} : Unable to retrieve the HelpInfo XML file for UI culture en-US.
Make sure the HelpInfoUri property in the module manifest is valid or check your network connection and then try the
command again.
At line:1 char:1
+ update-help
+ ~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [Update-Help], Exception
+ FullyQualifiedErrorId : UnableToRetrieveHelpInfoXml,Microsoft.PowerShell.Commands.UpdateHelpCommand
Long story:
I was trying to find a module and install it:
Find-Module -Name AudioDeviceCmdlets
But it needs NuGet so it prompts to install it but it failed for me:
NuGet provider is required to continue
PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories. The NuGet
provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or
'$HOME\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"): Y
WARNING: Unable to download from URI 'https://go.microsoft.com/fwlink/?LinkID=627338&clcid=0x409' to ''.
WARNING: Unable to download the list of available providers. Check your internet connection.
So I googled for a solution and found out that I need to paste this on Powershell:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
And the NuGet installed just fine, so I remember the issue I had with Update-Help, so I tried it and it worked, and now when I do for example Get-Help Get-Item the help is shown fully.
From Niels Weistra on answers.microsoft:
The solution mentioned above is a workaround, to solve your issue permanently
Open Powershell and check for supported protocols by using [Net.ServicePointManager]::SecurityProtocol
Run the following 2 cmdlets to set .NET Framework strong cryptography registry keys:
Set strong cryptography on 64 bit .Net Framework (version 4 and above):
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord
Set strong cryptography on 32 bit .Net Framework (version 4 and above):
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord
Restart Powershell and check again for supported protocol by using [Net.ServicePointManager]::SecurityProtocol
Source:
https://answers.microsoft.com/en-us/windows/forum/all/trying-to-install-program-using-powershell-and/4c3ac2b2-ebd4-4b2a-a673-e283827da143
Install NuGet via PowerShell script
I am new to Powershell and found this fixed my problem.
reference link
Update-Help -Verbose -Force -ErrorAction SilentlyContinue

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"