SECCION 2 LA POLITICA E INSTRUMENTOS DE FOMENTO: UN AVANCE SIGNIFICATIVO DESDE UNA PERSPECTIVA DE GÉNERO
2. INDAP: Apoyo a la Agricultura Familiar Campesina
2.4. Servicios de Fomento para segmentos y territorios especiales
Let’s say you’ve opened PowerShell, and you’ve loaded several favorite snap-ins and modules. If you took that route, you’d be required to run one command for each
snap-in or module you want to load, which can take a few minutes of typing if you have several of them. When you’re done using the shell, you close its window. The next time you open a shell window, all of your snap-ins and modules are gone, and you have to run all those commands again to load them back. Horrible. There must be a better way.
We’ll show you three better ways. The first involves creating a console file. This only memorizes PSSnapins that are loaded—it won’t work with any modules you may have loaded. Start by loading in all of the snap-ins you want, and then run this command:
Export-Console c:\myshell.psc
Running the command creates a small XML file that lists the snap-ins you loaded into the shell.
Next, you’ll want to create a new PowerShell shortcut somewhere. The target of that shortcut should be
%windir%\system32\WindowsPowerShell\v1.0\powershell.exe ➥-noexit -psconsolefile c:\myshell.psc
When you use that shortcut to open a new PowerShell window, your console will load, and the shell will automatically add any snap-ins listed in that console file. Again, mod- ules aren’t included. What do you do if you have a mix of snap-ins and modules, or if you have some modules that you always want loaded?
TIP Keep in mind that PowerShell will auto-load modules that are in one of the PSModulePath locations. You only need to worry about the following steps if you want to preload modules that aren’t in one of the PSModulePath locations.
The answer is to use a profile script. We’ve mentioned those before, and we’ll cover them in more detail in chapter 25, but for now follow these steps to learn how to use them:
1 In your Documents folder, create a new folder called WindowsPowerShell (no
spaces in the folder name).
2 In the newly created folder, use Notepad to create a file named profile.ps1.
When you save the file in Notepad, be sure to enclose the filename in quotation marks (“profile.ps1”). Using quotes prevents Notepad from adding a .txt file- name extension. If that .txt extension gets added, this trick won’t work.
3 In that newly created text file, type your Add-PSSnapin and Import-Module
commands, listing one command per line in order to load your preferred snap- ins and modules.
4 Back in PowerShell, you’ll need to enable script execution, which is disabled by
default. There are some security consequences to this that we’ll discuss in chap- ter 17 but for now we’ll assume you’re doing this in a standalone virtual machine, or on a standalone test computer, and that security is less of an issue. In the shell, run Set-ExecutionPolicy RemoteSigned. Note that the command
will only work if you’ve run the shell as Administrator. It’s also possible for a Group Policy object (GPO) to override this setting; you’ll get a warning message if that’s the case.
5 Assuming you haven’t had any errors or warnings up to this point, close and
reopen the shell. It will automatically load profile.ps1, execute your commands, and load your favorite snap-ins and modules for you.
TRY IT NOW Even if you don’t have a favorite snap-in or module yet, creat- ing this simple profile will be good practice. If nothing else, put the com- mand cd \ into the profile script, so that the shell always opens in the root of your system drive. But please don’t do this on a computer that’s part of your company’s production network, because we haven’t covered all of the security implications yet.
7.8
Common points of confusion
PowerShell newcomers frequently do one thing incorrectly when they start working with modules and snap-ins: they don’t read the help. Specifically, they don’t use the -example or -full switches when asking for help.
Frankly, looking at built-in examples is the best way to learn how to use a com- mand. Yes, it can be a bit daunting to scroll through a list of hundreds of commands (Exchange Server, for example, adds well over 400 new commands), but using Help and Get-Command with wildcards should make it easier to narrow down the list to what- ever noun you think you’re after. From there, read the help!
7.9
Lab
NOTE For this lab, you’ll need a Windows 7, Windows Server 2008 R2, or later computer running PowerShell v3.
As always, we’re assuming that you have the latest version of Windows (client or server) on a computer or virtual machine to test with.
For this lab, you only have one task: run the Networking troubleshooting pack. When you successfully do so, you’ll be asked for an “Instance ID.” Hit Enter, run a Web Connectivity check, and ask for help connecting to a specific web page. Use http:// videotraining.interfacett.com as your test URL. We hope you’ll get a “No problems were detected” report, meaning you ran the check successfully.
To accomplish this task, you’ll need to discover a command capable of getting a troubleshooting pack, and another capable of executing a troubleshooting pack. You’ll also need to discover where the packs are located and how they’re named. Everything you need to know is in PowerShell, and the help system will find it for you.
85