Book Image

Mastering PowerShell Scripting - Fourth Edition

By : Chris Dent
5 (1)
Book Image

Mastering PowerShell Scripting - Fourth Edition

5 (1)
By: Chris Dent

Overview of this book

PowerShell scripts offer a convenient way to automate various tasks, but working with them can be daunting. Mastering PowerShell Scripting takes away the fear and helps you navigate through PowerShell's capabilities.This extensively revised edition includes new chapters on debugging and troubleshooting and creating GUIs (online chapter). Learn the new features of PowerShell 7.1 by working with parameters, objects, and .NET classes from within PowerShell 7.1. This comprehensive guide starts with the basics before moving on to advanced topics, including asynchronous processing, desired state configuration, using more complex scripts and filters, debugging issues, and error-handling techniques. Explore how to efficiently manage substantial amounts of data and interact with other services using PowerShell 7.1. This book will help you to make the most of PowerShell's automation features, using different methods to parse data, manipulate regular expressions, and work with Windows Management Instrumentation (WMI).
Table of Contents (26 chapters)
24
Other Books You May Enjoy
25
Index

PowerShellGet 3.0

PowerShellGet 2 (for example, PowerShellGet 2.2.4.1) implements the Install-Module, Update-Module, and Save-Module module commands demonstrated at the beginning of this chapter.

PowerShellGet 3.0 is in preview at the time of writing; the following commands are based on the beta7 pre-release. One of the key features is that this new version does not depend on the PackageManagement module, allowing a simpler installation process, avoiding the need to bootstrap the NuGet provider, making upgrading the module simpler.

The preview version also uses new command names, completely divorcing it from the previous implementations of PowerShellGet. The change in command names means the new version can safely be installed alongside any existing version.

PowerShellGet 3.0 can be installed as follows:

Install-Module PowerShellGet -Force -AllowPrerelease

Once installed, you'll need to register the PowerShell Gallery or another repository:

Register-PSResourceRepository -PSGallery

In PowerShellGet 2.0, you implement separate commands to work with modules and scripts. PowerShellGet 3.0 does not differentiate between modules and scripts; all artifacts are termed PSResource, and all searches use the Find-PSResource command. For example, we can find a module by using the following command:

Find-PSResource -Name Indented.Net.IP -Type Module

The Type parameter may be omitted without affecting the search results in this case.

Most of the commands in PowerShellGet 3.0 use the same approach as those in PowerShellGet 2.2.4 and below. Over time, the differences between the commands are likely to start to appear; for example, Install-PSResource includes a Reinstall parameter, which is somewhat like the Force parameter for Install-Module in PowerShellGet 2.

Repositories

Like older versions of PowerShellGet, repositories are registered on a per-user basis. In PowerShellGet 2.2.4 and below, the repository configuration file is found in the following path:

$env:LOCALAPPDATA\Microsoft\Windows\PowerShell\PowerShellGet\PSRepositories.xml

The PSRepositories.xml file is stored in CliXml format and may be read using the Import-CliXml command. The file is normally read and updated using Get-PSRepository, Register-PSRepository, and Unregister-PSRespository.

PowerShellGet 3.0 uses a simpler format for the PSResourceRespository.xml file. The file may be found in the following path:

$env:LOCALAPPDATA\PowerShellGet\PSResourceRepository.xml

The Get-PSResourceRepository, Register-PSResourceRepository, and Unregister-PSResourceRepository commands are the expected way of interacting with this file.

As with older versions of PowerShellGet, storing credentials for a repository is not currently supported. If a repository requires authentication, the Credential parameter must be used explicitly with each operation.

Version ranges

Find-PSResource allows wildcards to be used for the Version parameter; using * will return all available versions except pre-release. The Prerelease parameter may be added to include those:

Find-PSResource -Name PowerShellGet -Version *

A range of versions may be defined using the range syntax used by NuGet, which is described in the following document:

https://docs.microsoft.com/nuget/concepts/package-versioning#version-ranges-and-wildcards

For example, the highest version of PowerShellGet available between 1.0 (inclusive) and 2.0 (exclusive) may be found using this:

Find-PSResource -Name PowerShellGet -Version '[1.0,2.0)'

The search can be changed to be inclusive by changing the closing ) to ]. For example, the following command will find version 2.0.0 of PowerShellGet:

Find-PSResource -Name PowerShellGet -Version '[1.0,2.0]'

The same syntax will be available when declaring dependencies between modules.