Anda di halaman 1dari 2

You can also run your VBS script without switching as a console script by directly specifying the script

host you want: CScript test.vbs The script lists all running processes and provides many interesting details on every process. VBScript is a very versatile and powerful automation language, and it's important to show how you can use VBScript files in PowerShell.

Running PowerShell Scripts

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
Table of Contents | About PowerShell Plus 55 Sponsors | Resources | BBS Technologies

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

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
Table of Contents | About PowerShell Plus 56 Sponsors | Resources | BBS Technologies

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}