Git Product home page Git Product logo

puppet-powershellmodule's Introduction

powershellmodule

Table of Contents

  1. Description
  2. Setup - The basics of getting started with powershellmodule
  3. Usage
  4. Reference
  5. Development - Guide for contributing to the module

Description

This module allows PowerShell repositories to be registered as package sources and PowerShell modules to be installed using the Puppet Package type.

The module supports Windows PowerShell (PowerShell 5) and PowerShell Core (PowerShell 6)

Setup

Windows PowerShell

For Windows PowerShell the PowerShellGet PowerShell module must be installed as well as the NuGet package provider. PowerShellGet is included with WMF5 or can be installed for earlier versions here http://go.microsoft.com/fwlink/?LinkID=746217&clcid=0x409

PowerShell Core

PowerShellGet is included in PowerShell Core so no additional setup is necessary.

Usage

Install PowerShellGet PackageProviders

You can install PackageProviders for PowerShelLGet using the pspackageprovider type.

pspackageprovider {'ExampleProvider':
  ensure   => 'present',
  provider => 'windowspowershell',
}

In order to use this module to to get packages from a PSRepository like the PSGallery, you will have to ensure the Nuget provider is installed:

pspackageprovider {'Nuget':
  ensure   => 'present',
  provider => 'windowspowershell',
}

You can optionally specify the version of a PackageProvider using the version parameter.

pspackageprovider {'Nuget':
  ensure   => 'present',
  version  => '2.8.5.208',
  provider => 'windowspowershell',
}

Register an internal PowerShell repository

psrepository { 'my-internal-repo':
  ensure              => present,
  source_location     => 'http://myrepo.corp.com/api/nuget/powershell',
  installation_policy => 'trusted',
  provider            => 'windowspowershell',
}

Manifests can then refer to that repository using the package resource.

package { 'nameOfInternallyDevelopedModule':
  ensure   => '1.0.5',
  source   => 'my-internal-repo',
  provider => 'windowspowershell',
}

*Windows users should remember that package names in Puppet are case sensitive.

Use the PowerShell Gallery

You can install modules from the PowerShell Gallery by default once the setup instructions have been followed. You do not need to specify the PSGallery with the psrepository type.

package { 'Pester':
  ensure   => '4.0.3',
  source   => 'PSGallery',
  provider => 'powershellcore',
}

Side by side installation

On Windows, PowerShell Core is installed along side Windows PowerShell and maintains its modules separately. To install the same module for both versions then use a unique resource title and specify the name property.

package { 'PSExcel-wps':
  ensure   => latest,
  name     => 'PSExcel',
  provider => 'windowspowershell',
  source   => 'PSGallery',
}

package { 'PSExcel-psc':
  ensure   => latest,
  name     => 'PSExcel',
  provider => 'powershellcore',
  source   => 'PSGallery',
}

The provider

The provider to use will either be windowspowershell or powershellcore. Nodes using powershell.exe will use windowspowershell, and nodes that have PowerShell core (pwsh.exe) will use the powershellcore provider with both the psrepository and package types.

Full Working example

This complete example shows how to bootstrap the system with the Nuget package provider, ensure the PowerShell Gallery repository is configured and trusted, and install two modules (one using the WindowsPowerShell provider and one using the PowerShellCore provider).

pspackageprovider {'Nuget':
  ensure => 'present'
}

psrepository { 'PSGallery':
  ensure              => present,
  source_location     => 'https://www.powershellgallery.com/api/v2/',
  installation_policy => 'trusted',
}

package { 'xPSDesiredStateConfiguration':
  ensure   => latest,
  provider => 'windowspowershell',
  source   => 'PSGallery',
  install_options => [ '-AllowClobber' ]
}

package { 'Pester':
  ensure   => latest,
  provider => 'powershellcore',
  source   => 'PSGallery',
}

Limitations

Note that PowerShell modules can be installed side by side so installing a newer version of a module will not remove any previous versions.

  • As detailed in OneGet/oneget#308, installing PackageProviders from a offline location instead of online is currently not working. A workaround is to use the Puppet file resource to ensure the prescence of the file before attempting to use the NuGet PackageProvider.

The following is an incompelete example that copies the NuGet provider dll to the directory that PowerShellGet expects. You would have to modify this declaration to complete the permissions for the target and the location of the source file.

file{"C:\Program Files\PackageManagement\ProviderAssemblies\nuget\2.8.5.208\Microsoft.PackageManagement.NuGetProvider.dll":
  ensure => 'file',
  source => "$source\nuget\2.8.5.208\Microsoft.PackageManagement.NuGetProvider.dll"
}

Reference

Types

package

puppet-powershellmodule implements a package type with a resource provider, which is built into Puppet.

The implementation supports the install_options attribute which can be used to pass additional options to the PowerShell Install-Modules command, e.g.:

package { 'xPSDesiredStateConfiguration':
  ensure   => latest,
  provider => 'windowspowershell',
  source   => 'PSGallery',
  install_options => [ '-AllowClobber',
                       { '-proxy' => 'http://proxy.local.domain' }  ]
}

pspackageprovider

Properties/Parameters

ensure

Specifies what state the PowerShellGet provider should be in. Valid options: present and absent. Default: present.

name

Specifies the name of the PowerShellGet provider to install.

version

Specifies the version of the PowerShellGet provider to install

psrepository

Allows you to specify and configure a repository. The type expects a valid OneGet package provider source over an HTTP or HTTPS url.

Properties/Parameters

name

The name of the gallery to register on the computer. Must be unique. Cannot use PSGallery as the value for this property.

source_location

The url to the repository that you would like to register. Must be a valid HTTP or HTTPS url. This url will be used for the underlying SourceLocation property and will be used as the base url for PublishLocation, ScriptSourceLocation, ScriptPublishLocation. Cannot use the same url as the default gallery, PSGallery.

installation_policy

Manages the installation policy used for the PSRepository. Valid values are trusted or untrusted

Providers

windowspowershell

The provider for systems that have powershell.exe (PowerShell versions less than 6).

powershellcore

The provider for systems that use PowerShell core via pwsh.exe.

Development

https://github.com/hbuckle/puppet-powershellmodule

puppet-powershellmodule's People

Contributors

hbuckle avatar jpogran avatar michaeltlombardi avatar nmaludy avatar palintir avatar randomnoun7 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

puppet-powershellmodule's Issues

Implement auto-requires or make docs more explicit?

The documentation says people should install the nuget packageprovider before attempting to install powershell modules from the PSGallery.

  1. Is it possible to implement an auto-requires to handle this automatically instead of relying on the default manifest ordering?
  2. If auto-requires isn't possible in this scenario should the documentation be updated to show the relationship being setup so it's more explicit and instead of implicit
pspackageprovider { 'Nuget':
    ensure => '2.8.5.208',
} -> Package <| provider == 'windowspowershell' |>

package { 'AWSPowerShell':
    ensure   => '3.3.563.1',
    provider => 'windowspowershell',
}

provider windowspowershell must be versionable

Hello,

I might be missing something but I'm getting the following error:

Error: Failed to apply catalog: Parameter ensure failed on Package[StorageDsc]: Provider windowspowershell must have features 'versionable' to set 'ensure' to '4.1.0'

The code I'm using is the following:

package { 'StorageDsc':
    ensure   => '4.1.0',
    source   => 'PSGalleryProxy',
    provider => 'windowspowershell',
  }

Using this works:

package { 'StorageDsc':
    ensure   => present,
    source   => 'PSGalleryProxy',
    provider => 'windowspowershell',
  }

From the README.md this seems to be possible, though.

package { 'nameOfInternallyDevelopedModule':
  ensure   => '1.0.5',
  source   => 'my-internal-repo',
  provider => 'windowspowershell',
}

Am I missing something?

Discuss Getting Module Approved

Hello,
I don't know if you've noticed, but we recently released the DSC_Lite module. But one of the challenges with that module is getting the DSC Modules onto the box for the Puppet module to use.

We think that your module here would serve that function nicely, but customers often have reservations about using modules in their environments that aren't at least "Approved".

We were hoping that you would be interested in having a conversation about how to get your module up to approved status. We would much rather do that than stomp on and duplicate this work that you've done here.

Would you consider joining the Puppet Slack Team so we can talk about this a little more easily?

cannot set version for windowspowershell provider

Module version: 1.0.0
OS: Windows 2016 1607 (Build: 14393.2312)
Powershell Version: 5.1.14393.2312
Ruby: ruby 2.4.2p198 (2017-09-14 revision 59899) [x64-mingw32]
Puppet: 5.3.3

  psrepository { 'PSGallery':
    ensure              => present,
    source_location     => 'https://www.powershellgallery.com/api/v2/',
    installation_policy => 'trusted',
    provider            => 'windowspowershell',
  }

  package { 'PSWindowsUpdate':
    ensure   => '2.0.0.4',
    source   => 'PSGallery',
    provider => 'windowspowershell',
  }

produces:

Error: Failed to apply catalog: Parameter ensure failed on Package[PSWindowsUpdate]: Provider windowspowershell must have features 'versionable' to set 'ensure' to '2.0.4' at /etc/puppetlabs/code/environments/production/site/profile/manifests/windows/update/install.pp:11

If i use latest, it works.

Don't know much about ruby and creating custom providers, but if I'm not wrong, you are inheriting the provider windowspowershell from the powershellcore which has versionable as feature defined.
Seems not to be inherited?

Proxy support

Registering repositories and installing modules should work through a proxy. Also need to find a way to handle proxy credentials

Is this module still being maintained?

Unfortunately Puppet have not migrated the DSC PackageManagement resource over so there is no equivalent for this module in Puppet. And certainly nothing for working with PowerShell Core on any OS.

There are a lot of old issues and PR's in this repo and the Puppet Forge module hasn't been updated for a couple of years.

So is this still being maintained?

Could not find parent provider powershellcore of windowspowershell

Getting the following error when running puppet

Could not autoload puppet/provider/pspackageprovider/windowspowershell: Could not find parent provider powershellcore of windowspowershell

pspackageprovider { 'Nuget':
    ensure   => 'present',
    }

and

pspackageprovider { 'Nuget':
    ensure   => 'present',
    provider => 'windowspowershell',
    }

Runtime environment: puppet_version=5.5.16, ruby_version=2.4.5, run_mode=agent

PSVersion = 5.1.18362.1
PowershellGet-version, tried with both 2.2.1.0 & 1.0.0.1
Nuget-Version, tried with 3.0.0.1 & 2.8.5.208

Keeps reporting that system is changing

Every run I do the system is reporting itself as changed, even though I can't see any obvious reason why this would be

Notice: /Stage[main]/Profile::Base::Windows/Psrepository[PSGallery]/source_location: source_location changed 'https://www.powershellgallery.com/api/v2' to 'https://www.powershellgallery.com/api/v2/' Notice: /Stage[main]/Profile::Base::Windows/Psrepository[PSGallery]/installation_policy: installation_policy changed 'untrusted' to 'trusted'

The manifest itself is basically straight from the examples:

psrepository { 'PSGallery': ensure => present, source_location => 'https://www.powershellgallery.com/api/v2/', installation_policy => 'trusted', } package { 'PSGelf': ensure => latest, provider => 'powershellcore', source => 'PSGallery', }

Some dsc modules are created every time during the apply

Notice: /Stage[main]/Main/Package[NetworkingDSC]/ensure: created
Notice: /Stage[main]/Main/Package[ComputerManagementDSC]/ensure: created
Notice: /Stage[main]/Main/Package[CertificateDSC]/ensure: created

Debug:

Debug: Executing: 'C:/Windows/System32/WindowsPowerShell/v1.0/powershell.EXE -NoProfile -ExecutionPolicy Bypass -NonInteractive -NoLogo -Command $ProgressPreference = 'SilentlyContinue'; $ErrorActionPreference = 'Stop'; @(Get-PackageProvider).foreach({
[ordered]@{
'name' = $.Name.ToLower()
'version' = $
.Version.ToString()
} | ConvertTo-Json -Depth 99 -Compress
})
'
Debug: Prefetching windowspowershell resources for package
Debug: Executing: 'C:/Windows/System32/WindowsPowerShell/v1.0/powershell.EXE -NoProfile -ExecutionPolicy Bypass -NonInteractive -NoLogo -Command $ProgressPreference = 'SilentlyContinue'; $ErrorActionPreference = 'Stop'; Get-Package -AllVersions -ProviderName PowerShellGet -Scope AllUsers -Type Module |
Group-Object -Property Name | % {
[ordered]@{
'name' = $.Name
'ensure' = @(($
.Group).Version)
'provider' = 'windowspowershell'
} | ConvertTo-Json -Depth 99 -Compress
}
'
Debug: Executing: 'C:/Windows/System32/WindowsPowerShell/v1.0/powershell.EXE -NoProfile -ExecutionPolicy Bypass -NonInteractive -NoLogo -Command $ProgressPreference = 'SilentlyContinue'; $ErrorActionPreference = 'Stop'; Install-Module NetworkingDSC -Scope AllUsers -Force -Repository PSGallery'
Notice: /Stage[main]/Main/Package[NetworkingDSC]/ensure: created
Debug: /Package[NetworkingDSC]: The container Class[Main] will propagate my refresh event
Debug: Executing: 'C:/Windows/System32/WindowsPowerShell/v1.0/powershell.EXE -NoProfile -ExecutionPolicy Bypass -NonInteractive -NoLogo -Command $ProgressPreference = 'SilentlyContinue'; $ErrorActionPreference = 'Stop'; Install-Module ComputerManagementDSC -Scope AllUsers -Force -Repository PSGallery'
Notice: /Stage[main]/Main/Package[ComputerManagementDSC]/ensure: created
Debug: /Package[ComputerManagementDSC]: The container Class[Main] will propagate my refresh event
Debug: Executing: 'C:/Windows/System32/WindowsPowerShell/v1.0/powershell.EXE -NoProfile -ExecutionPolicy Bypass -NonInteractive -NoLogo -Command $ProgressPreference = 'SilentlyContinue'; $ErrorActionPreference = 'Stop'; Install-Module CertificateDSC -Scope AllUsers -Force -Repository PSGallery'
Notice: /Stage[main]/Main/Package[CertificateDSC]/ensure: created

Funny part is that other packages are not created everytime

package { 'SecurityPolicyDsc':
ensure => latest,
provider => 'windowspowershell',
source => 'PSGallery',
}->

get-package -allversions -providername powershellget -scope allusers -type Module | group-object -property name | % { [ordered]@{'name'=$.name;'ensure'=@(($.Group).version);'provider'='windowspowershell'} | ConvertTo-Json -Depth 99 -Compress }

{"name":"CertificateDsc","ensure":["4.2.0.0"],"provider":"windowspowershell"}
{"name":"ComputerManagementDsc","ensure":["5.2.0.0"],"provider":"windowspowershell"}
{"name":"NetworkingDsc","ensure":["6.1.0.0"],"provider":"windowspowershell"}
{"name":"SecurityPolicyDsc","ensure":["2.5.0.0"],"provider":"windowspowershell"}
{"name":"xActiveDirectory","ensure":["2.21.0.0"],"provider":"windowspowershell"}
{"name":"xPendingReboot","ensure":["0.4.0.0"],"provider":"windowspowershell"}

Could not autoload puppet/type/package

I'm running the following puppet code:

  pspackageprovider {'Nuget':
    ensure => 'present',
  }

  psrepository { 'PSGallery':
    ensure              => present,
    source_location     => 'https://www.powershellgallery.com/api/v2/',
    installation_policy => 'trusted',
  }

  package { 'VMware.PowerCLI':
    ensure   => latest,
    provider => 'windowspowershell',
    source   => 'PSGallery',
  }

And receiving the following error

Error: Could not retrieve catalog from remote server:
Error 500 on SERVER: Server Error: Evaluation Error:
Error while evaluating a Resource Statement, Could not autoload puppet/type/package: 
Could not autoload puppet/provider/package/powershellcore: /etc/puppetlabs/code/environments/production/modules/powershellmodule/lib/puppet/provider/package/powershellcore.rb:63: unknown type of %string
    command << " -RequiredVersion #{@resource[:ensure]}" unless %i[present latest].include? @resource[:ensure]

Puppet versions:

Puppet = 5.5.3
Puppet Server = 5.3.5

Tag latest release

First off: thanks for the module, it is helping a lot!

One thing that would make this module slightly easier to consume downstream would be to tag it. I know quite a few things got merged in on master since the 1.0.0 tag.

Module does not work with powershell 5.0 and below

flush_command = "Set-PSRepository #{@resource[:name]}"

(Probably another file, this is initial investigation)

Because of how the source code is written, it always executes a command with first parameter being the name of the resource.

In the working example you provide it says it should work with powershell 5 and 6 (https://github.com/hbuckle/puppet-powershellmodule/blob/master/README.md), however, it only works with powershell 5.1, with 5.0 it doesn't.

The issue is that the command "Set-PSRepository" doesn't take the first string as a parameter in powershell 5.0. Also, there would be an issue if you were to set a 'name' parameter inside puppet, it errors, because there are 2 name parameters.

Command's Set-PSRepository' required parameter 'name as positional parameter '0' is only introduced in 5.1 as seen here:

https://docs.microsoft.com/en-us/powershell/module/powershellget/set-psrepository?view=powershell-5.0

https://docs.microsoft.com/en-us/powershell/module/powershellget/set-psrepository?view=powershell-5.1

Possible solution is not to include resource name in the flush command, so you would always have to explicitly say the name of the PSRepository and it would work with powershell 5.0 and below.

Lowercased SourceLocation

Hello,

is there a particular reason to have the SourceLocation lowercased?

In particular in these two situations:

and

'source_location' = $_.SourceLocation.ToLower()

Since I think most of the webserver out there doesn't rewrite urls (e.g. Nexus Sonatype doesn't do it), I was wondering if removing these parameter alteration would be a thing.

Install-Module with Source will fail each time

using source param will fail each time, as that is passed to the install block:

  def install_command
    command = "Install-Module #{@resource[:name]} -Force"
    command << " -RequiredVersion #{@resource[:ensure]}" unless [:present, :latest].include? @resource[:ensure]
    command << " -Source #{@resource[:source]}" if @resource[:source]
    command
  end

is going to call -Source each time and will fail each time since -Source is not a valid parameter for Install-Module

should set with -Repository after having user set the -Repository name via psrepository

https://docs.microsoft.com/en-us/powershell/module/powershellget/install-module?view=powershell-5.1

Unexpected Token at 'WARNING: MSG:UnableToDownload...'

The following error is encountered when using this module on a system that is isolated from the internet.

Debug: Prefetching windowspowershell resources for psrepository
Debug: Executing: 'C:/WINDOWS/System32/WindowsPowerShell/v1.0/powershell.EXE -noprofile -executionpolicy bypass -command
     @(Get-PSRepository).foreach({
      [ordered]@{
        'name' = $_.Name
        'source_location' = $_.SourceLocation
        'installation_policy' = $_.InstallationPolicy.ToLower()
      } | ConvertTo-Json -Depth 99 -Compress
    })
'
Debug: Storing state
Debug: Pruned old state cache entries in 0.00 seconds
Debug: Stored state in 0.06 seconds
Error: Failed to apply catalog: 751: unexpected token at 'WARNING: MSG:UnableToDownload https://go.microsoft.com/fwlink/?LinkID=627338&clcid=0x409'

OS: Windows Server 2019
PSVersion: 5.1.17763.1
Get-PackageProvider -Name NuGet returns NuGet 2.8.5.208

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.