EncoreTechnologies / puppet-powershellmodule

Apache License 2.0
6 stars 7 forks source link

psrepository fails after initial corrective action #32

Open norbertstoll opened 3 years ago

norbertstoll commented 3 years ago

Hi,

I ran into an issue while checking psrepository after it has been created successfully at first puppet run. We run an offline environment.

Side note: The NuGet package provider has been installed manually before (copy Microsoft.PackageManagement.NuGetProvider.dll) and can't be checked successfully via pspackageprovider:

Error: Could not prefetch pspackageprovider provider 'windowspowershell': 765: unexpected token at 'WARNING: MSG:UnableToDownload «https://go.microsoft.com/fwlink/?LinkID=627338&clcid=0x409» «»'
Error: Failed to apply catalog: 765: unexpected token at 'WARNING: MSG:UnableToDownload «https://go.microsoft.com/fwlink/?LinkID=627338&clcid=0x409» «»'

Back to topic:

Our used modules:

mod 'puppetlabs-dsc_lite', '3.1.0'
mod 'puppetlabs-pwshlib', '0.8.0'
mod 'encore-powershellmodule', '2.2.1'

hiera-config:

profile::powershell::psrepository:
  'PowerShell_DEV':
    ensure:                     present
    source_location:      'https://hostname.domain/nuget/PowerShell_DEV'
    installation_policy:  'trusted'
    provider:                  'windowspowershell'

Output of puppet run:

PS C:\> puppet agent -t
Info: Using configured environment 'windows_test'
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Retrieving locales
Info: Loading facts
Info: Caching catalog for hostname.domain
Info: Applying configuration version '76fb657ed72a947eb8a75faa0f9c2f9a1aea8783'
Notice: /Stage[main]/Profile::Powershell/Psrepository[PowerShell_DEV]/ensure: created (corrective)
Notice: Applied catalog in 5.14 seconds
PS C:\> puppet agent -t
Info: Using configured environment 'windows_test'
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Retrieving locales
Info: Loading facts
Info: Caching catalog for hostname.domain
Info: Applying configuration version '76fb657ed72a947eb8a75faa0f9c2f9a1aea8783'
Error: Could not set 'present' on ensure: Error when executing command: $ProgressPreference = 'SilentlyContinue'; $ErrorActionPreference = 'Stop'; [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12;     $params = @{
      Name = 'PowerShell_DEV'
      SourceLocation = 'https://reposerver/nuget/PowerShell_DEV'
      InstallationPolicy = 'trusted'
    }

    # Detecting if this is Powershell Gallery repo or not
    if ($params.Name -eq 'PSGallery' -or $params.SourceLocation -match 'powershellgallery') {
      # Trim these params or the splatting will fail
      $params.Remove('Name')
      $params.Remove('SourceLocation')
      Register-PSRepository -Default @params
    } else {
      # For all non-PSGallery repos..
      Register-PSRepository @params
    }

 stdout =
 stderr = [] (file: /etc/puppetlabs/code/environments/windows_test/site/profile/manifests/powershell.pp, line: 14)
Error: Could not set 'present' on ensure: Error when executing command: $ProgressPreference = 'SilentlyContinue'; $ErrorActionPreference = 'Stop'; [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12;     $params = @{
      Name = 'PowerShell_DEV'
      SourceLocation = 'https://reposerver/nuget/PowerShell_DEV'
      InstallationPolicy = 'trusted'
    }

    # Detecting if this is Powershell Gallery repo or not
    if ($params.Name -eq 'PSGallery' -or $params.SourceLocation -match 'powershellgallery') {
      # Trim these params or the splatting will fail
      $params.Remove('Name')
      $params.Remove('SourceLocation')
      Register-PSRepository -Default @params
    } else {
      # For all non-PSGallery repos..
      Register-PSRepository @params
    }

 stdout =
 stderr = [] (file: /etc/puppetlabs/code/environments/windows_test/site/profile/manifests/powershell.pp, line: 14)
Wrapped exception:
Error when executing command: $ProgressPreference = 'SilentlyContinue'; $ErrorActionPreference = 'Stop'; [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12;     $params = @{
      Name = 'PowerShell_DEV'
      SourceLocation = 'https://reposerver/nuget/PowerShell_DEV'
      InstallationPolicy = 'trusted'
    }

    # Detecting if this is Powershell Gallery repo or not
    if ($params.Name -eq 'PSGallery' -or $params.SourceLocation -match 'powershellgallery') {
      # Trim these params or the splatting will fail
      $params.Remove('Name')
      $params.Remove('SourceLocation')
      Register-PSRepository -Default @params
    } else {
      # For all non-PSGallery repos..
      Register-PSRepository @params
    }

 stdout =
 stderr = []
Error: /Stage[main]/Profile::Powershell/Psrepository[PowerShell_DEV]/ensure: change from 'absent' to 'present' failed: Could not set 'present' on ensure: Error when executing command: $ProgressPreference = 'SilentlyContinue'; $ErrorActionPreference = 'Stop'; [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12;     $params = @{
      Name = 'PowerShell_DEV'
      SourceLocation = 'https://reposerver/nuget/PowerShell_DEV'
      InstallationPolicy = 'trusted'
    }

    # Detecting if this is Powershell Gallery repo or not
    if ($params.Name -eq 'PSGallery' -or $params.SourceLocation -match 'powershellgallery') {
      # Trim these params or the splatting will fail
      $params.Remove('Name')
      $params.Remove('SourceLocation')
      Register-PSRepository -Default @params
    } else {
      # For all non-PSGallery repos..
      Register-PSRepository @params
    }

 stdout =
 stderr = [] (file: /etc/puppetlabs/code/environments/windows_test/site/profile/manifests/powershell.pp, line: 14) (corrective)
Notice: Applied catalog in 5.02 seconds
PS C:\>

The corrective action can be verified successfully in a new PowerShell-session:

PS C:\> Get-PSRepository
WARNING: MSG:UnableToDownload «https://go.microsoft.com/fwlink/?LinkID=627338&clcid=0x409» «»
WARNING: Unable to download the list of available providers. Check your internet connection.

Name                      InstallationPolicy   SourceLocation
----                      ------------------   --------------
PowerShell_DEV            Trusted              https://hostname.domain/nuget/PowerShell_DEV
PS C:\> Get-PackageProvider nuget

Name                     Version          DynamicOptions
----                     -------          --------------
NuGet                    2.8.5.208        Destination, ExcludeVersion, Scope, SkipDependencies, Headers, FilterOnTag...

Any help is much appreciated :)

Regards, Norbert

rismoney commented 3 years ago

was there a fix for this?

nmaludy commented 3 years ago

@bishopbm1 or @gsperry2011 can you guys look into this? i don't have Windows anymore to check it out.

rismoney commented 3 years ago

I will provide a lot of information: I am on windows2016 server core: "${env:ProgramFiles}\PackageManagement\ProviderAssemblies\Microsoft.PackageManagement.NuGetProvider.dll is present

My error is:

Could not set 'present' on ensure: Error when executing command: $ProgressPreference = 'SilentlyContinue'; $ErrorActionPreference = 'Stop'; [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12;     $params = @{
      Name = 'psgallery-virtual'
      SourceLocation = 'https://chocolatey.example.com/chocolatey'
      InstallationPolicy = 'trusted'
    }

    # Detecting if this is Powershell Gallery repo or not
    if ($params.Name -eq 'PSGallery' -or $params.SourceLocation -match 'powershellgallery') {
      # Trim these params or the splatting will fail
      $params.Remove('Name')
      $params.Remove('SourceLocation')
      Register-PSRepository -Default @params
    } else {
      # For all non-PSGallery repos..
      Register-PSRepository @params
    }

 stdout =  
 stderr = [] (file: /etc/puppetlabs/code/environments/master/modules/os/manifests/powershell.pp, line: 3)

This only happens on a handful of nodes, so it is not consistent.

Contents of PSRepositories.xml in the c$\Windows\System32\config\systemprofile\AppData\Local\Microsoft\Windows\PowerShell\PowerShellGet

<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <Obj RefId="0">
    <TN RefId="0">
      <T>System.Collections.Hashtable</T>
      <T>System.Object</T>
    </TN>
    <DCT>
      <En>
        <S N="Key">psgallery-virtual</S>
        <Obj N="Value" RefId="1">
          <TN RefId="1">
            <T>Microsoft.PowerShell.Commands.PSRepository</T>
            <T>System.Management.Automation.PSCustomObject</T>
            <T>System.Object</T>
          </TN>
          <MS>
            <S N="Name">psgallery-virtual</S>
            <S N="SourceLocation">https://chocolatey.example.com/chocolatey</S>
            <Nil N="PublishLocation" />
            <Nil N="ScriptSourceLocation" />
            <Nil N="ScriptPublishLocation" />
            <B N="Trusted">true</B>
            <B N="Registered">true</B>
            <S N="InstallationPolicy">Trusted</S>
            <S N="PackageManagementProvider">NuGet</S>
            <Obj N="ProviderOptions" RefId="2">
              <TNRef RefId="0" />
              <DCT />
            </Obj>
          </MS>
        </Obj>
      </En>
    </DCT>
  </Obj>
</Objs>
  psrepository { 'psgallery-virtual':
    ensure              => present,
    source_location     => 'https://chocolatey.example.com/chocolatey',
    installation_policy => 'trusted',
  }

sanitized domain names

gsperry2011 commented 3 years ago

What's the output look like if you run this in powershell (please make sure it's "run as administrator"), also please change the $params to match whatever you have specified in hiera.

$ProgressPreference = 'SilentlyContinue'; $ErrorActionPreference = 'Stop'; [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12;     $params = @{
      Name = 'psgallery-virtual'
      SourceLocation = 'https://chocolatey.example.com/chocolatey'
      InstallationPolicy = 'trusted'
    }

    # Detecting if this is Powershell Gallery repo or not
    if ($params.Name -eq 'PSGallery' -or $params.SourceLocation -match 'powershellgallery') {
      # Trim these params or the splatting will fail
      $params.Remove('Name')
      $params.Remove('SourceLocation')

      # debug statement
      write-host "is PSrepo"
      Register-PSRepository -Default @params
    } else {
      # debug statement
      write-host "NOT a PSrepo"

      # For all non-PSGallery repos..
      Register-PSRepository @params
    }
rismoney commented 3 years ago

unregister-psrepository psgallery-virtual. run above: NOT a PSrepo.

Rerun: NOT a PSrepo PackageManagement\Register-PackageSource : Module Repository 'psgallery-virtual' exists. At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.5\PSModule.psm1:11587 char:17

Speegel commented 2 years ago

Hello,

I added a try - catch on the block "create_command" when it tries to "Register-PSRepository @params" in lib\puppet\provider\psrepository\powershellcore.rb as the error seems to come from the fact that the repository is tried to be re-added if it exists already.

Another weird behavior I noticed is that the stdout from the "self.instances_command" block is not returning anything.

I found that the repository discovery block "self.instances_command" was not generating any return at first run, seems like it's due to the -WarningAction Stop. If the WarningAction behavior is set to Ignore, as warnings comming from Get-PSRepository on first run can be ignored, it generates an output and the error disappear.

So far it fixed the issue and seems a better solution than adding a try catch block as stated earlier.

cheers