5. PROPUESTA DE MEJORAMIENTO
5.2. PROPUESTA PLENEACIÓN DE LA DEMANDA
5.2.1. PLANEACIÓN DE LA DEMANDA A TRAVÉS, DE CALCULOS DE
5.4.2 Loading extensions
To load a PSSnapin, you add it to your session:
PS C:\> Add-PSSnapin microsoft.sqlserver.cmdletsnapin.100
Specify the PSSnapin name as revealed by Get-PSSnapin–Registered. Modules are loaded similarly, although in this case you import them:
PS C:\> Import-Module storage
Note that some modules are script modules, meaning they can load only if you’ve enabled execution of scripts. If you try to load one on a default PowerShell configura-tion where scripting isn’t enabled, you’ll get an error message. You’ll need to enable script execution, which we cover in chapter 17, or you can read the help for the Set-ExecutionPolicy command to learn how to load such a module.
NOTE For modules stored in one of the PSModulePath locations, you won’t need to import the module explicitly. PowerShell will import the module automatically through its autoloading feature the first time you try to run one of the commands in the module.
If modules aren’t stored in one of the PSModulePath locations, you can import them by providing the full path to the module’s folder, rather than providing only the mod-ule’s name. For example, a module stored in C:\MyModules\Fred would be loaded by running Import-ModuleC:\MyModules\Fred. Because that module doesn’t live in one of the autosearched locations, it wouldn’t be autoloaded, nor would it be revealed by running Get-Module–ListAvailable.
5.4.3 Discovering extensions’ additions
Once an extension is loaded, you can see what commands it contains:
PS C:\> Get-Command -Module storage
CommandType Name ModuleN ame --- ---- ---Alias Initialize-Volume storage Function Add-InitiatorIdToMaskingSet storage Function Add-PartitionAccessPath storage Function Add-PhysicalDisk storage 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 might have been added, 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}
We didn’t filter for a specific module or PSSnapin, so you’ll see all available providers.
Remember that you can ask for help on a provider (helpalias, 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 is generally not a big deal, but you might get an exception if you reimport or re-add the module or PSSnapin in the same session. If so, you can ignore the error message.
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
45 Summary
ActiveDirectory 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 we had a module called MyModule that contains
Get-MyNoun Set-MyNoun
If you import it as Import-ModuleMyModule–PrefixDJR, 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.
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.
46
Operators
In any computer language, operators provide a means of comparing and manipulat-ing pieces of data. PowerShell’s no exception, offermanipulat-ing a wide variety of operators for different tasks.
All of these operators have a common syntactical form. Practically all Power-Shell operators start with a dash or a hyphen, followed by the operator name. You’ll see plenty of examples of this in the following sections and throughout the rest of the book. If you have prior experience with other scripting or programming lan-guages, PowerShell’s operators can seem confusing or odd at first, but you’ll get used to them as you work with them.
This chapter covers
■ Logical and comparison operators
■ Bitwise operators
■ Arithmetic operators
■ Type operators
■ Other special operators
47