Robert 的个人资料Rob Keiser照片日志列表更多 ![]() | 帮助 |
|
1月29日 Expression mode v. Command mode in PowerShellUnlike the CMD prompt, PowerShell will process commands in one of two ways. It does this by first looking at what you've typed and deciding if it should interpret it as a command or an expressions. The two modes are called Expression and Command. Command mode works just like the way CMD works,what you type is interpreted as a command. If the text is a valid command it will be executed. Expression mode allows PowerShell to take what you type and interpret it as values in an expression. For example, If you type 2 + 2 the result (4) will be printed on the next line. If you enter some text in quotes (like "Hello world") , then the text will be printed on the next line. So far this seems pretty simple, if you start a line with a number or a quote PowerShell will be in Expression mode. But what if you want to assign a value to a variable? PowerShell understands that you are referencing a variable because you enter a $ - so entering $a = 2+2 will assign the value of 4 to the $a variable. Entering just $a will print its contents on the next line. We can now see that numbers, quotes and the dollar sign will put PowerShell into Expression mode. There are other characters that PowerShell will interpret as expressions, most characters that could be used by a mathematical expression are interpreted that way - so Period, Plus, Minus, and Left Parenthesis all put PowerShell into Expression mode. A character that won't appear to do anything is the octothorp (you may know this as the number sign or pound sign). The reason this doesn't appear to do anything is because it is used to indicate a comment so PowerShell ignores anything after it. Now lets say you've put the string "get-process" into a variable called $a. How would you get PowerShell to execute that command? The Ampersand (&) character can be used to tell PowerShell to interpret the contents of a variable and then pass it on to be executed as a command like this &$a. This means that the Ampersand actually causes PowerShell to use both the Expression and Command modes. PowerShell also has to be able to understand files and directories. Because of this, PowerShell has a small quirk in the way it handles the Period and the Backslash. Both of these characters will work similar to the Ampersand. If you were to enter .$a instead of &$a from the previous example the results would be the same - the listing of the currently running processes would be displayed. The Backslash has its own way of handling the command. If we simply replace the ampersand with the backslash we will get an error, but if we replace the $a with the command in quotes (\"get-process") we will get the process listing. These two characters work this way so you can execute script files with spaces in the directory or file name. It is mentioned here only to notify you of a potential side effect, you should not use these characters as a replacement for the ampersand. |
|
|