去年的时候,我从Windows IT Pro上知道了Powershell。Powershell作为MS第一个可以和BASH等unix抗衡的shell,虽然称不上优秀,毫无疑问,潜力巨大。Exchange2007是第一个可以完全使用Powershell进行管理的产品,大大拓展了自身的管理能力,变得更加智能和可控。可以预见,在未来的几年内,Powershell将会是MS产品线中一个极其重要的管理工具。
 
买了半年多的《Powershell in Action》一直没有怎么认真去研读过,草草翻阅,每次都是看了就忘——不过现在不同了,Windows server 2008里集成了Powershell,Quest也已经开发出AD相关的Cmdlet,Powershell终于不再是仅限于Exchange2007,而能参与进日常管理中,对于他的学习,也不再是可有可无
 
不过manning的这本书是有台湾版的,和英文版的价格差不多。早知道就买繁体版的,因为英文版是有电子版 - -#——这件事情给我的教训就是买原版书之前先查查有没有繁体版。
 
又唠叨了一大堆的废话,还是进入正题吧,第一篇就写个简单的,毕竟自己也是才正式开始学习。本篇包括接下来的文章,将遵循BDD的文章格式,先英文再中文。不过和BDD不同,属于笔记类别,比较随意。
 
Manning----Powershell In Action
Page 63
 
Here-string literals
 
Getting back to the discussion of literal string notation,there is one more form of string literal,calld a here-string.A here-string is used to embed large chunks of text inline in a script.This can be powerful when you`re generating output for another program.Here`s an example that assigns a here-string to the variable $a.
回到关于字符串标记的讨论,还有一种字符串格式,称作here-string。here-string用于在脚本中嵌入大量的文字。这在当你想为某个程序生成大量的输出时十分有用。下面是一个将here-string赋予变量$a的例子
When $a is displayed,it contains all of the lines that are entered.Now you`re probably saying,"Wait a minute----you told me I can do the same thing with a regular string.What makes here-strings so special?"It has to do with how quoting is handled.Here-strings have special quoting rules.
当$a显示,它包括了所有输入的行。现在你可能会问:”请等一下,你告诉我可以用一般的字符串来完成同样的事情。是什么让Here-strings如此不同?”这和引用如何传递有关——Here-strings有特殊的引用法则。
 
Here-strings start with @<quote><newline>and end with <newline><quote>@.The <newlines>are important because the here-string quote sequences won`t be treated as quotes without the newlines.The content of the here-string is all of the lines between the beginning and ending quotes,but not the lines the quotes are on.Because of the fancy opening and closing quote sequences,other special characters such as quotes that would cause problems in regular strings are fine here.This makes it easy to generate string data without having quoting errros.Here is a more elaborate example.
Here-string以@<引号><换行>开始,以<换行><引号>@结束。<换行>十分重要,因为here-string中没有“换行”的话,引号序列将不会被当作引号来处理。here-string中的内容是所有在开始和结束引号中的部分,但不包括这两个引号所在行部分。因为如此处理,普通字符串中会出问题的引号,可以在here-string里正常显示。这让我们可以十分容易地生成字符信息而不用担心出错,这里有一个复杂的例子。
On line 1,the here-string is assigned to the variable $a.The contents of the Here-string start on line 2,which has a string containing double quotes.Line 3 has a string with single quotes.Line 4 has an embedded expression,and line 5 calls the Get-date cmdlet in a subexpression to embed the current date into the string.Finally,line 6 append some trailing text to the whole string.When you look at the output of the variable shown in lines 9-12,you see that the quotes are all preserved and the expansions are shown in place.
在第一行,here-string被赋予给了变量$a.here-string的内容从第二行开始,包括双引号。第三行则有单引号。第四行则是一个嵌入的语句,第五行则呼叫了命令get-date。最终,第六行附加了一条对比语句来测试。当你看到所有的输出,你会发现所有的引号都能正常显示。
 
以下文章来自于 [url]http://bbs.winos.cn[/url]的Powershell版主Edengundam的 [PowerShell教程][08]字符串字面值一文,作为本文的附属参考。
 
字符串的字面值
字符串类型在PowerShell的使用中频率也非常高,对比其他shell来说,虽然PowerShell使用字符串的情况变少了,但是在功能上甚至可以说得到了增强。主要的原因可以归功于.Net Framework类库。System.String类中包含了各式各样的方法,让使用者可以方便的处理字符串。
在PowerShell中,字符串就是.Net Framework中的16-bit Unicode字符序列。因此,日常使用时,可以正确处理大部分常用的各国字符。
说起字符串,就必须要解释引号规则,引号规则往往总是与两个概念有关:特殊字符的保护以及变量代换。
变量代换有时候也被为变量展开或变量篡改,通过这种手段,可以简化字符串表示,使脚本的阅读者或维护者更加方便的理解代码。例如:
PS C:\> $a = 'Windows PowerShell'
PS C:\> "Hello, $a"
Hello, Windows PowerShell
通过将变量名称替换成变量的值,使变量能够在字符串展开成原始值的过程就是变量代换。
单引号
单引号的功能有时候会让人爱不释手,因为单引号中任何字符都只表示自己。换句话来说,单引号中不会进行变量代换,也不会对任何转义序列进行处理,你看到的“几乎”就是你输入的字符串。之所以我用“几乎”这副词来强调,是因为引号规则中有个特例,如果需要在单引号的字符串中包括单引号该怎么办呢?为了解决这种情况,Windows PowerShell采用了类似CSV文件中的解决方法:通过使用连续两个引号来表示一个引号。下面是几个单引号字符串的例子:
PS C:\> $shells = 'PowerShell'  # 设置变量$shells为字符串PowerShell
PS C:\> 'What''up, $shells'     # 两个连个连续的单引号表示一个单引号
What'up, $shells
PS C:\> 'What'up, $shells'      # 因为单引号不匹配,引起错误
Unexpected token 'up' in expression or statement.
At line:1 char:9
+ 'What'up, <<<<  $shells'
第一个例子中,我们使用了两个连续的单引号来表示在字符串中应该包含一个单引号。第二个例子中展示了如果缺少单引号,就会导致引号不匹配,产生错误信息。另外需要注意的是在单引号中$shells变量没有被代换。
双引号
双引号,可以形容他是个变色龙,如果双引号包含变量,则可以随着脚本的执行,将结果动态的附加进入。双引号中允许:变量代换和转义序列。说到这里,必须要唠叨一下Windows PowerShell中怪异的转移序列。在常见的语言中,例如:C、C#、Java、Perl和Python等。转义字符都是反斜线“\”(HTML和XML属于例外)。由于Windows操作系统历史原因,反斜线“\”一直被用作目录分隔符。如果PowerShell使用反斜线作为转义字符,就会产生两个可怕的情况:要么在PowerShell中用正斜线“/”作为分隔符,要么每次都需要连续使用两个反斜线进行转义处理。无论上面哪种情况,PowerShell要么与Windows的定义不一致,要么用户使用时就会抱怨输入太多的反斜线。最后PowerShell团队决定:转义字符选择反引号(backtick或者back quote)“`”,反引号位于数字键1的左边。转义序列如下所示:
         `'   -- Single quote
         `"   -- Double quote
         `0   -- Null
         `a   -- Alert
         `b   -- Backspace
         `f   -- Form feed
         `n   -- New line
         `r   -- Carriage return
         `t   -- Horizontal tab
         `v   -- Vertical tab
虽然这些长相怪异的转移序列可能吓到你了,但是,相信它不会阻碍您学习PowerShell。让我们来双引号中的变量代换以及转移序列使用。
PS C:\> $shell = "PowerShell"
PS C:\> "Who are you?`n$shell"
Who are you?
PowerShell
PS C:\> "`$shell is $shell"
$shell is PowerShell
PS C:\> "我们来看看重叠双引号的效果"""
我们来看看重叠双引号的效果"
第一个例子中,我们使用换行的转移序列,也使用了变量代换。第二例子,我们使用“`$”来输出变量和变量的值。最后我们看到了与单引号相同的特性,如果连续两个双引号表示一个双引号。下面的例子,我们看看单引号和双引号的对比,以及相互包含时的特性:
PS C:\> "'`$shell is $shell'"
'$shell is PowerShell'
PS C:\> '"`$shell is $shell"'
"`$shell is $shell"
如果大家已经吸收了前面所介绍的知识,那么这个例子的结果应该非常好解释。我们对单引号双引号做个简单的概括:1. 单引号中不支持变量代换和转义序列;双引号中允许变量代换以及转义序列。2. 根据最外层的引号决定字符串如何被处理。3. 连续重复两次最外层引号表示一个引号。只要这三条记住,引号规则就不会成为困难。
 
OK,今天就到这里,明天会讨论数和数的类型。