• Aucun résultat trouvé

Trustworthy Subdirectories

Dans le document Copyright Copyright (Page 33-37)

PowerShell distinguishes between trustworthy folders and all other folders. You won't need to provide the path name or append the file extension to the command name if the program is located in a trustworthy folder. Commands like ping or ipconfig work as-is because they are in located a trustworthy folder, while the program in our last example, WordPad, is not.

The Windows environment variable Path determines whether a folder is trustworthy or not. All folders listed in this environment variable are treated as "trustworthy" by PowerShell. You could put all your important programs in one of the folders listed in the environment variable Path. You can find out this list by entering:

$env:Path

C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\program Files\Softex\OmniPass;C:\Windows\System32\WindowsPowerShell\v1.0\;c :\program Files\Microsoft SQL Server\90\Tools\binn\;C:\program File s\ATI Technologies\ATI.ACE\Core-Static;C:\program Files\MakeMsi\;C:

\program Files\QuickTime\QTSystem\

You'll find more on variables as well as special environment variables in the next chapter.

As a clever alternative, you can add other folders containing important programs to your Path environment variables, such as:

$env:path += ";C:\programs\Windows NT\accessories"

wordpad.exe

After this change, you can suddenly launch WordPad just by entering its program name. Note that your change to the environment variable Path is valid only as long as PowerShell is running. Once you end PowerShell, your modification is discarded. So, if you'd like to permanently extend Path, you need to add the line for the extension to one of your profile scripts. Profile scripts start automatically when PowerShell starts and their purpose is to customize your PowerShell environment. You read more about profile scripts in Chapter 10.

Programs in special subdirectories: You can simply enter the program name to launch the program if the program is located in one of the special folders specified in the Path

environment variable. Almost all relevant tools can be launched that way.

Specifying a path: You must tell the console where it is if the program is located somewhere else. To do so, specify the absolute or relative path name of the program.

Watch out for white space characters: if white space characters occur in path names, enclose the entire path in quotes so that PowerShell doesn't interpret white space characters as separators. It doesn't matter whether you use double quotation marks ("") or single quotation marks ( ' ' ); you just have to be consistent. Stick to single quotes. For example, PowerShell "resolves" text in double quotation marks, replacing variables with their values.

The "&" changes string into commands: PowerShell doesn't treat text in quotes as a command. Prefix string with "&" to actually execute it. The "&" symbol allows you to execute any string just as if you had entered the text directly on the command line.

& ("note" + "pad")

If you have to enter a very long path names, remember (Tab), the key for automatic completion:

C:\(Tab)

Press (Tab) again and again until the suggested subdirectory is the one you are seeking. Add a "\" and press (Tab) once again in order to specify the next subdirectory.

The moment a white space character turns up in a path, AutoComplete also puts the path in quotation marks and inserts an "&" before it. However, if you want to add further subdirectories, you must first remove the last quotation mark with (Backspace).

Cmdlets: "Genuine" PowerShell Commands

PowerShells internal commands are called 'cmdlets'. The "mother" of all cmdlets is called Get-Command:

Get-Command -commandType cmdlet

It retrieves a list of all available cmdlets. Cmdlet names always consist of an action (verb) and something that is acted on (noun). This naming convention helps you to find the right command.

Let's take a look at how the system works.

If you're looking for a command for a certain task, you should first select the action that best describes the task. There are relatively few actions that the strict PowerShell naming conditions permit (Table 2.2). If you know that you want to obtain something, the proper action is "get." That already gives you the first part of the command name, and all you have to do now is to take a look at a list of commands that are likely candidates:

Get-Command -verb get

- ----

---cmdlet Get-Acl Get-Acl [[-Path] <String[]>] [-A...

cmdlet Get-Alias Get-alias [[-Name] <String[]>] [...

cmdlet Get-Authenticode Get-AuthenticodeSignature [-File...

Signature

cmdlet Get-ChildItem Get-ChildItem [[-Path] <String[]...

cmdlet Get-Command Get-Command [[-ArgumentList] <Ob...

cmdlet Get-Content Get-Content [-Path] <String[]> [...

cmdlet Get-Credential Get-Credential [-Credential] <PS...

cmdlet Get-Culture Get-Culture [-Verbose] [-Debug] ...

cmdlet Get-Date Get-Date [[-Date] <DateTime>] [-...

cmdlet Get-EventLog Get-EventLog [-LogName] <String>...

cmdlet Get-Execution Get-ExecutionPolicy [-Verbose] [...

Policy

cmdlet Get-Help Get-Help [[-Name] <String>] [-Ca...

cmdlet Get-History Get-History [[-Id] <Int64[]>] [[...

cmdlet Get-Host Get-Host [-Verbose] [-Debug] [-E...

cmdlet Get-Item Get-Item [-Path] <String[]> [-Fi...

cmdlet Get-ItemProperty Get-ItemProperty [-Path] <String...

cmdlet Get-Location Get-Location [-PSProvider <Strin...

cmdlet Get-Member Get-Member [[-Name] <String[]>] ...

cmdlet Get-PfxCertificate Get-PfxCertificate [-FilePath] <...

cmdlet Get-Process Get-Process [[-Name] <String[]>]...

cmdlet Get-PSDrive Get-PSDrive [[-Name] <String[]>]...

cmdlet Get-PSProvider Get-PSProvider [[-PSProvider] <S...

cmdlet Get-PSSnapin Get-PSSnapin [[-Name] <String[]>...

cmdlet Get-Service Get-Service [[-Name] <String[]>]...

cmdlet Get-TraceSource Get-TraceSource [[-Name] <String...

cmdlet Get-UICulture Get-UICulture [-Verbose] [-Debug...

cmdlet Get-Unique Get-Unique [-InputObject <PSObje...

cmdlet Get-Variable Get-Variable [[-Name] <String[]>...

cmdlet Get-WmiObject Get-WmiObject [-Class] <String> ...

As you see, the relevant cmdlet Get-Command comes from the "get" group.

Action Description

Add Add

Clear Delete

Compare Compare

Convert Convert

Copy Copy

Export Export

Format Format

Get Acquire

Group Group

Import Import

Measure Measure

Move Move

New Create new

Out Output

Read Read

Remove Remove

Rename Rename

Resolve Resolve

Restart Restart

Resume Resume

Select Select

Set Set

Sort Sort

Split Split

Start Start

Stop Stop

Suspend Suspend

Tee Split up

Test Test

Trace Trace

Update Update

Write Write

Table 2.2: The most important standard actions and their descriptions

You can look up help for any cmdlet using Get-Help:

Get-Help Get-Command -detailed

You can easily discover commands for certain actions because Get-Command also allows wildcards:

Get-Command *help* -CommandType cmdlet CommandType Name Definition - ----

---cmdlet Get-Help Get-Help [[-Name] <String>] [-Category...

Dans le document Copyright Copyright (Page 33-37)