• Aucun résultat trouvé

Running PowerShell Scripts

Dans le document Copyright Copyright (Page 55-59)

PowerShell has its own script files with the file extension ".ps1". While you will learn much more about PowerShell scripts in Chapter 10, you already know enough to write your first script:

Notepad test.ps1

Enter in Notepad any PowerShell command you like. Everything you've successfully entered in the console up to now is allowed. PowerShell scripts function very much like the batch files of the classic console: if the script is opened later, PowerShell works through everything in your script one step at a time, just as if you had directly entered each line one-by-one in the console.

Dir

Get-PSProvider help Dir

Try to bring it to life after you've saved your script:

.\test.ps1

File "C:\Users\UserA\test.ps1" cannot be loaded because the execution of scripts is disabled on this system. Please see

"get-help about_signing" for more details.

At line:1 char:10 + .\test.ps1 <<<<

You'll probably receive an error message similar to the one in the above example. In PowerShell, all scripts are at first disabled and cannot be started. PowerShell will start scripts only once you enabled them (for which you need admin privileges since a regular user cannot change this setting). You can give your permission by entering Set-ExecutionPolicy:

Set-ExecutionPolicy RemoteSigned

This grants permission to run locally stored PowerShell scripts as scripts from the Internet remain prohibited unless they have a valid signature. The implications of signatures and other security settings will be discussed in Chapter 10. For now, the command described above should be enough for you to experiment with your own PowerShell scripts. To restore the original setting and prohibit PowerShell scripts, you should enter:

Set-ExecutionPolicy Default

Summary

The PowerShell console runs all kinds of commands interactively: you enter a command, and the console will more or less immediately return the results.

Cmdlets are PowerShell's own internal commands. A cmdlet name always consists of a description of an action (verb), and the object of the action (noun). The cmdlet Get-Command will provide a list of all cmdlets. Get-Help will also offer information about a particular cmdlet and can also search for cmdlet names when you specify a search phrase and wildcards: Get-Command *Service*

In addition, you can use aliases, functions, and scripts in PowerShell. An alias is a shortcut name for any other command, enabling you to create your own convenient shorthand expressions for

commands you use frequently. Functions and scripts combine several PowerShell commands. If you enter a command and execute it by pressing (Enter), PowerShell looks for the command in this order:

Alias: It will first look to see if your command corresponds to an alias. If it does, the

command will be executed that the alias designates. You can "overwrite" any other command with an alias by using the cmdlet Set-Alias because aliases have highest priority.

Function: If no alias could be found, PowerShell looks next for a function, which resembles an alias, but can consist of many PowerShell instructions. You can wrap commands, including frequently used arguments, in functions.

Cmdlet: If it's not possible to find a function, PowerShell looks for cmdlets, which are internal PowerShell commands that conform to strict naming rules and whose names always consist of a verb and a noun.

Application: PowerShell looks first for a cmdlet, and if it can't find any, it then searches for external commands in the subdirectories specified in the Path environment variables. If you'd like to use a command at some other location, then you must specify a relative or absolute path name.

Script: If PowerShell can't find any external commands, it looks next for a script with the file extension ".ps1". However, scripts are executed only when restrictions of the ExecutionPolicy are eased, allowing PowerShell scripts to be run.

Files: If no PowerShell scripts are found, PowerShell keeps looking for other files. PowerShell reports an error if your command doesn't match any files.

Again, use Get-Command to find out if there are any ambiguities.

The next line will list all commands that PowerShell knows that use

"ping" as a name.

Get-Command ping

Type this if you'd like to find out whether there are commands with the same names in differently named categories that conflict:

Get-Command -type cmdlet,function,alias | Group-Object name | Where-Object {$_.count -gt 1}

CHAPTER 3.

Variables Variables

It is time to combine commands whenever a single PowerShell command can't solve your problem.

One way of doing this is by using variables. PowerShell can store results of one command in a variable and then pass the variable to another command.

In addition, variables are rich 'objects' and can do much more than simply store data. In this chapter, we'll explain what variables are and how you can use them to solve complex problems.

Topics Covered:

• Your Own Variables

• Selecting Variable Names

• Assigning and Returning Values

• Populating Several Variables with Values Simultaneously

• Exchanging the Contents of Variables

• Assigning Different Values to Several Variables

• Overview of Variables in Use

• Finding Variables

• Verify Whether a Variable Exists

• Deleting Variables

• Using Special Variable Cmdlets

• Table 3.1: Cmdlets for managing variables

• Write-Protecting Variables: Creating Constants

• Variables with Description

• "Automatic" PowerShell Variables

• Environment Variables

• Reading Particular Environment Variables

• Searching for Environment Variables

• Creating New Environment Variables

• Deleting and Modifying Environment Variables

• Permanent Modifications of Environment Variables

• Drive Variables

• Directly Accessing File Paths

• Table 3.2: Variable areas made available by external providers

• Ad-hoc Variables: Sub-Expressions

• Scope of Variables

• Automatic Restriction

• Changing Variable Visibility

• Advantage of Lifting Visibility Restrictions: Clear and Unambiguous Start Conditions

• Setting the Scope of Individual Variables

• Table 3.3: Variable scopes and validity of variables

• Table 3.4: Practical usage of scope allocations

• Variable Types and "Strongly Typing"

• Assigning Fixed Types

• The Advantages of Specialized Types

• Table 3.5: Variable types

• Variable Management: Behind the Scenes

• Subsequent Modification of Variables Options

• Activating Write-Protection

• Table 3.6: Options of a PowerShell variable

• Type Specification of Variables

• Verifying and Validating Variable Contents

• Table 3.7: Available variable validation classes

• Summary

Dans le document Copyright Copyright (Page 55-59)