• No results found

49Using extensions

In document PowerShell in Depth, 2nd Edition (Page 78-82)

Working with PSSnapins and modules

49Using extensions

Function Add-TargetPortToMaskingSet storage Function Add-VirtualDiskToMaskingSet storage Function Clear-Disk storage Function Connect-VirtualDisk storage

This technique works with both modules and PSSnapins. From here, you can ask for help on a specific command to learn how to use it.

NOTE The –Module parameter has an alias, -PSSnapin, which makes it also legal to run Get-Command –PSSnapin My.Snapin.Name. It’s the same effect.

Keep in mind that extensions can add more than commands; they can also add pro- viders. To see what providers are available on your machine, run the following:

PS C:\> Get-PSProvider

Name Capabilities Drives ---- --- --- Alias ShouldProcess {Alias} Environment ShouldProcess {Env} FileSystem Filter, ShouldProcess, ... {C, A, D} Function ShouldProcess {Function} Registry ShouldProcess, Transact... {HKLM, HKCU} Variable ShouldProcess {Variable} WSMan Credentials {WSMan} Certificate ShouldProcess {Cert}

This is the list from a standard Windows 8.1 machine. You may see more providers if you have other modules loaded such as those for Active Directory or SQL Server. In a newly opened PowerShell console, you may not see the WSMan or Certificate provider in the list until you have accessed them because they aren’t part of the core Power- Shell load.

We didn’t filter for a specific module or PSSnapin, so you’ll see all available provid- ers. Remember that you can ask for help on a provider (help alias, for example) once it’s loaded.

5.4.4 Managing extensions

You can use the following commands to manage extensions:

■ Remove-Module unloads a module.

■ Get-Module displays a list of all loaded modules in the current PowerShell session.

■ Remove-PSSnapin removes a PSSnapin.

■ Get-PSSnapin displays a list of all loaded PSSnapins in the current Power- Shell session.

Generally, when you remove a module or a snap-in all of its commands are removed from your PowerShell session. But be aware that some items, such as custom type or format extensions, might persist. This may not be a big deal, but you may get an exception if you reimport or readd the module or PSSnapin in the same session. If so, you can ignore the error message. Using a new PowerShell console is one way to avoid these messages.

5.5

Command name conflicts

When you start loading up a bunch of modules or PSSnapins, it’s obviously possible for two of them to contain commands having the same name. So what happens?

By default, when you run a command, PowerShell runs the last version of that com- mand that was loaded into memory—that is, whichever one was loaded most recently. That command has the effect of “hiding” commands having the same name that were loaded earlier. There’s a specific purpose for that behavior: It enables something called proxy functions, which we’ll discuss in chapter 37.

But you can access any specific command you want to by providing a fully qualified name. That name combines the name of the PSSnapin or module that contains the command you want, a backslash, and then the command name. ActiveDirectory\ Get-ADUser, for example, will run the Get-ADUser command contained in the Active- Directory module or PSSnapin, even if some other extension has more recently loaded a different “Get-ADUser” command.

An alternative is to use the –Prefix parameter of Import-Module, which enables you to add a prefix to the noun for each of the cmdlets (or functions) in your module. Assume you had a module called MyModule that contains

Get-MyNoun Set-MyNoun

If you import it as Import-Module MyModule –Prefix DJR, the functions would have a prefix applied and would become

Get-DJRMyNoun Set-DJRMyNoun

Now you can run these commands without worrying about naming collisions.

In PowerShell v4, you can also get modules by a fully qualified name. If for some reason you have two modules with the same name but they’re different versions (don’t ask us why you would), you can specify the module you want. FullyQualifiedname is a special hash table of the format @{ModuleName="MyModule";ModuleVersion="1.0"}. Both Get-Module and Get-Module –ListAvailable provide the version number and name as part of the default display:

PS C:\> Get-Module -FullyQualifiedName @{ModuleName="Applocker";

➥ ModuleVersion="2.0"} -ListAvailable

Directory: C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules ModuleType Version Name ExportedCommands --- --- ---- --- Manifest 2.0.0.0 AppLocker {Get-AppLockerFile...

You’ll want to include –ListAvailable. If this is the module you wanted to import, the best thing is to pipe it to Import-Module:

PS C:\> Get-Module -FullyQualifiedName @{ModuleName="Applocker";

51 Summary

Now, there’s an odd quirk here, or perhaps a bug. If you specify a version but the only version you have is newer, you’ll get the newer version. We assume the reasoning is that the newer version is backwards compatible. But if you ask for a version that’s newer than the one that’s installed, you’ll get nothing. In our example, the AppLocker mod- ule is at version 2.0. If we ask for version 1.0, we’ll get this version. But if we ask for ver- sion 3.0, we’ll get nothing.

5.6

Managing module autoloading

PowerShell has a built-in variable, $PSModuleAutoLoadingPreference, that controls autoloading behavior. You probably won’t see this variable if you run Get-Variable. PowerShell’s default behavior is to autoload all modules. But you can explicitly create the variable and assign it one of the following values:

■ All—Automatically imports a module on first use of any command contained in the module.

■ ModuleQualified—Modules are loaded automatically only if you use a qualified command name, such as MyModule\Do-Something. Running only Do-Something wouldn’t load the module containing that command.

■ None—Modules aren’t loaded automatically.

This variable doesn’t prevent you from explicitly loading a module using Import- Module. But autoloading makes it easier because all you have to do is run the com- mand and let PowerShell handle any necessary module imports. Be aware that this applies only to modules; you still need to manually add a PSSnapin before you can use any of its commands.

5.7

Summary

Managing PowerShell extensions is one key to being successful with the shell. Much of the functionality you’ll rely on to accomplish administrative tasks comes from exten- sions rather than from the shell’s core functionality. Being able to find, load, and inventory extensions is the primary way you can get needed functionality to the shell and have it available for your use.

52

Operators

In any computer language, operators provide a means of comparing and manipulat- ing pieces of data. PowerShell’s no exception, offering a wide variety of operators for different tasks.

Powershell supplies a number of help files on the various operators. We rec- ommend that you read them in conjunction with this chapter. The help files con- tain numerous examples that will aid your understanding. The following help files are available:

■ about_Operators

■ about_Arithmetic_Operators

■ about_Assignment_Operators

■ about_Comparison_Operators

This chapter covers

■ Logical and comparison operators

■ Bitwise operators

■ Arithmetic operators

■ Type operators

53

In document PowerShell in Depth, 2nd Edition (Page 78-82)