Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
PowerShell Troubleshooting Guide

You're reading from   PowerShell Troubleshooting Guide Minimize debugging time and maximize troubleshooting efficiency by leveraging the unique features of the PowerShell language

Arrow left icon
Product type Paperback
Published in Nov 2014
Publisher
ISBN-13 9781782173571
Length 206 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Michael Shepard Michael Shepard
Author Profile Icon Michael Shepard
Michael Shepard
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. PowerShell Primer FREE CHAPTER 2. PowerShell Peculiarities 3. PowerShell Practices 4. PowerShell Professionalism 5. Proactive PowerShell 6. Preparing the Scripting Environment 7. Reactive Practices – Traditional Debugging 8. PowerShell Code Smells Index

Modules

In Version 1.0 of PowerShell, the only ways to group lists of functions were to either put script files for each function in a directory or to include several functions in a script file and use dot-sourcing to load the functions. Neither solution provided much in the way of functionality, though. Version 2.0 introduced the concept of modules. A PowerShell module usually consists of a folder residing in one of the directories listed in the PSModulePath environment variable and contains one of the following:

  • A module file (.psm1) with the same name as the folder
  • A module manifest (.psd1) with the same name as the folder
  • A compiled assembly (.dll) with the same name as the folder

One tremendous advantage that modules have over scripts is that while every function in a script is visible when the script is run, visibility of functions (as well as variables and aliases) defined within a module can be controlled by using the Export-ModuleMember cmdlet.

The following module file, named TroubleShooting.psm1, re-implements the Get-PowerShellMessage function from earlier in the chapter using a helper function (Get-Message). Since only Get-PowerShellVersionMessage was exported, the helper function is not available after the module is imported but it is available to be called by the exported function.

function Get-Message{
param($ver,$name)
  return "We're using $ver, $name!"
}

function Get-PowerShellVersionMessage{
param($name)
  $version=$PSVersionTable.PSVersion
  $message=Get-Message $version $name
  return $message
}

Export-ModuleMember Get-PowerShellVersionMessage

Importing a module is accomplished by using the Import-Module cmdlet. Version 3.0 of PowerShell introduced the concept of automatic importing. With this feature enabled, if you refer to a cmdlet or function that does not exist, the shell looks in all of the modules that exist on the system for a matching name. If it finds one, it imports the module automatically. This even works with tab-completion. If you hit the Tab key, PowerShell will look for a cmdlet or function in memory that matches, but If it doesn't find one it will attempt to load the first module that has a function whose name matches the string you're trying to complete. Listing the cmdlets that have been loaded by a particular module is as simple as the Get-Command –Module module name.

You have been reading a chapter from
PowerShell Troubleshooting Guide
Published in: Nov 2014
Publisher:
ISBN-13: 9781782173571
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image