Delivering PowerShell code with the November preview

Step one of investigating OneGet and PowerShellGet – install the latest preview.  I have the environment I used for the demos at the European PowerShell Summit with a machine set up for DSC. Its currently running the September preview:

£> $PSVersionTable

Name                           Value
—-                           —–
PSVersion                      5.0.9814.0
WSManStackVersion              3.0
SerializationVersion           1.1.0.1
CLRVersion                     4.0.30319.34014
BuildVersion                   6.4.9814.0
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.2

 

The November preview is supposed to install over the top of the September preview. I’m installing on to  Windows 2012 R2 so I want:

WindowsBlue-KB3006193-x64.msu

The target machine is fully patched apart from the November Windows 2012 R2 roll up.

Installation occurred with out a problem. – I like the change to being able to just install over the top of pervious versions.  After the usual restart the version has changed.

£> $PSVersionTable

Name                           Value
—-                           —–
PSVersion                      5.0.9883.0
WSManStackVersion              3.0
SerializationVersion           1.1.0.1
CLRVersion                     4.0.30319.34014
BuildVersion                   6.4.9883.0
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.2

The PowerShellGet module contains these cmdlets:

Find-Module
Get-PSRepository
Install-Module
Publish-Module
Register-PSRepository
Set-PSRepository
Unregister-PSRepository
Update-Module

The module type is Script meaning it contains PowerShell Advanced functions.  You can see the code at C:\Windows\System32\WindowsPowerShell\v1.0\Modules\PowerShellGet

Just make sure you don’t change anything.

First thing is to see what repositories we’ve got:

£> Get-PSRepository | fl

Name               : PSGallery
SourceLocation     : https://www.powershellgallery.com/api/v2/
Trusted            : False
Registered         : True
InstallationPolicy : Untrusted
OneGetProvider     : NuGet
PublishLocation    : https://go.microsoft.com/fwlink/?LinkID=397527&clcid=0x409
ProviderOptions    : {}

Name               : MSPSGallery
SourceLocation     : http://search.microsoft.com/default.aspx
Trusted            : True
Registered         : True
InstallationPolicy : Trusted
OneGetProvider     : NuGet
PublishLocation    : http://go.microsoft.com/fwlink/?LinkID=397635&clcid=0x409
ProviderOptions    : {}

 

Notice that PSGallery is not trusted but MSPSGallery is trusted.

If you want to see the modules available use Find-Module.  

The first time you do expect this:

£> Find-Module

NuGet-anycpu.exe is required to continue.
PowerShellGet requires NuGet-anycpu.exe to interact with NuGet based galleries. NuGet-anycpu.exe must be available in
‘C:\Program Files\OneGet\ProviderAssemblies’ or ‘C:\Users\Richard\AppData\Local\OneGet\ProviderAssemblies’. For more
information about NuGet provider, see http://oneget.org/NuGet.html. Do you want PowerShellGet to download
NuGet-anycpu.exe now?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is “Y”):

You’re going to have to download the nuget file to continue.

On many modules I get this error:

Cannot convert value “18/12/2014 04:22:19 +00:00” to type “System.DateTime”. Error: “String was not recognized as a valid DateTime.”
At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PowerShellGet\PSGet.psm1:1402 char:49
+ …               PublishedDate = if($published){ [DateTime]$published };
+                                                   ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastParseTargetInvocationWithFormatProvider

If you use:

Find-Module -ErrorAction SilentlyContinue

You will get a display like this:

Version  Name           Repository Description
——-   —-                ———- ———–
1.0.0      xFirefox       PSGallery  Firefox Main module
1.0.0      xChrome      PSGallery  Module for installing the chrome
0.2.16.2 xJea              PSGallery  Module with DSC Resources for  (JEA)

and many more.

The date related error isn’t consistent – for instance the Pester module:

£> Find-Module -Name Pester | fl *

Name                       : Pester
Version                    : 3.2.0
Description                : Pester provides a framework for running BDD style Tests to execute and validate PowerShell commands inside of PowerShell and offers a powerful set of Mocking Functions that allow tests to mimic and mock the functionality of any command inside of a piece of powershell code being tested. Pester tests can execute any command or script that is accesible to a pester test file. This can include functions, Cmdlets, Modules and scripts. Pester can be run in ad hoc style in a console or it can be integrated into the
Build scripts of a Continuous Integration system.
Author                     : Pester Team
CompanyName                :
Copyright                  : Copyright (c) 2014 by Pester Team, licensed under Apache 2.0 License.
PublishedDate              : 12/03/2014 17:38:11
LicenseUri                 : http://www.gnu.org/licenses/gpl-2.0.html
ProjectUri                 : https://github.com/Pester/Pester
IconUri                    : http://pesterbdd.com/images/Pester.png
Tags                       : {powershell, unit, testing, bdd…}
Includes                   : {Function, DscResource, Cmdlet, Command}
PowerShellGetFormatVersion :
ReleaseNotes               :
RequiredModules            :
RepositorySourceLocation   : https://www.powershellgallery.com/api/v2/
Repository                 : PSGallery
OneGetProvider             : NuGet

But looking at the Pester module in the PowerShell gallery – https://www.powershellgallery.com – it shows a date of 2014-12-03  i.e. 3 December 2014.  My system is interpreting that as 12 March 2014 because I’m using the English convention for dates in my culture settings.

£> $PSCulture
en-GB
£> $PSUICulture
en-US

Using

Set-Culture -CultureInfo en-US

Restarting PowerShell then running

£> $PSCulture
en-US
£> Find-Module

Stops the error messages so

Find-Module | select Name, Version, PublishedDate

works. Just remember your dates are in US format – Month/Day/Year

The last step is downloading and installing a module. The Install-Module cmdlet does that for us.

£> Get-Command Install-Module -Syntax

Install-Module [-Name] <string[]> [-MinimumVersion <version>] [-RequiredVersion <version>] [-Repository <string[]>]
[-Scope <string>] [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]

Install-Module [-InputObject] <psobject[]> [-Scope <string>] [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]

Notice that there isn’t a choice of destination. You

£> Install-Module -Name Pester

You are installing the module ‘Pester’ from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the Set-PSRepository cmdlet.
Are you sure you want to install software from ‘https://www.powershellgallery.com/api/v2/’?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is “Y”): y

This installs the module:

> Get-Module -ListAvailable p*

   Directory: C:\Program Files\WindowsPowerShell\Modules

oduleType Version    Name                                ExportedCommands
——— ——-    —-                                —————-
cript     3.2.0      Pester                              {Describe, Context, It, Should…}

C:\Program Files\WindowsPowerShell\Modules was added to the modules path in PowerShell 4.0 and is the default location for DSC resources.

£> Install-Module -Name Pester –Force

Will override the message asking for confirmation.

Advertisements
This entry was posted in PowerShell v5, WMFv5. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s