2015-06-19 72 views
0

在PowerShell中,很少有程序以@'开头并以'@结尾,但是当我键入@'并在PowerShell提示符下按回车时,它会引发错误。任何人都可以解释我该如何解决这个问题?什么是@参数?

+3

可能重复[什么在Powershell中使用“@”符号?](http://stackoverflow.com/questions/363884/what-does-the-symbol-do-in-powershell) – elssar

+0

在PowerShell中自己键入'@''不应该给你一个错误。它应该让你继续提示('>>')。 –

+3

@elssar不,不是'@'这是询问'@'''''''@'对。 –

回答

2

@''@标记Here-String的开始和结束。键入@'然后按在PowerShell控制台输入通常应该给你续行提示符(>>):

PS C:>@' 
>> _

如果你得到你最有可能没有键入单(或双)报价错误,但是有前向或反向或某种印刷报价。如果这是你应该得到的“无法识别的记号”的错误这样的案例:

PS C:\> 
At line:1 char:1 
+ @´ 
+ ~ 
Unrecognized token in source text. 
    + CategoryInfo   : ParserError: (:) [], ParentContainsErrorRecordException 
    + FullyQualifiedErrorId : UnrecognizedToken
3

@'...'@@"..."@是“这里字符串”(在about_quoting_rules记录):的

HERE-STRINGS

The quotation rules for here-strings are slightly different.

A here-string is a single-quoted or double-quoted string in which quotation marks are interpreted literally. A here-string can span multiple lines. All the lines in a here-string are interpreted as strings even though they are not enclosed in quotation marks.

Like regular strings, variables are replaced by their values in double-quoted here-strings. In single-quoted here-strings, variables are not replaced by their values.

You can use here-strings for any text, but they are particularly useful for the following kinds of text:

-- Text that contains literal quotation marks 
-- Multiple lines of text, such as the text in an HTML or XML document 
-- The Help text for a script or function 

A here-string can have either of the following formats, where represents the linefeed or newline hidden character that is added when you press the ENTER key.

Double-quotes:

@"<Enter> 
    <string> [string] ...<Enter> 
    "@ 

Single-quotes:

@'<Enter> 
    <string> [string] ...<Enter> 
    '@ 

In either format, the closing quotation mark must be the first character in the line.

A here-string contains all the text between the two hidden characters. In the here-string, all quotation marks are interpreted literally. For example:

@" 
    For help, type "get-help" 
    "@ 

The output of this command is:

For help, type "get-help" 

Using a here-string can simplify using a string in a command. For example:

@" 
    Use a quotation mark (') to begin a string. 
    "@ 

The output of this command is:

Use a quotation mark (') to begin a string. 

In single-quoted here-strings, variables are interpreted literally and reproduced exactly. For example:

@' 
    The $profile variable contains the path 
    of your Windows PowerShell profile. 
    '@ 

The output of this command is:

The $profile variable contains the path 
    of your Windows PowerShell profile. 

In double-quoted here-strings, variables are replaced by their values. For example:

@" 
    Even if you have not created a profile, 
    the path of the profile file is: 
    $profile. 
    "@ 

The output of this command is:

Even if you have not created a profile, 
    the path of the profile file is: 
    C:\Users\User01\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1. 

Here-strings are typically used to assign multiple lines to a variable. For example, the following here-string assigns a page of XML to the $page variable.

$page = [XML] @" 
    <command:command xmlns:maml="http://schemas.microsoft.com/maml/2004/10" 
    xmlns:command="http://schemas.microsoft.com/maml/dev/command/2004/10" 
    xmlns:dev="http://schemas.microsoft.com/maml/dev/2004/10"> 
    <command:details> 
      <command:name> 
        Format-Table 
      </command:name> 
      <maml:description> 
       <maml:para>Formats the output as a table.</maml:para> 
      </maml:description> 
      <command:verb>format</command:verb> 
      <command:noun>table</command:noun> 
      <dev:version></dev:version> 
    </command:details> 
    ... 
    </command:command> 
    "@ 

Here-strings are also a convenient format for input to the ConvertFrom-StringData cmdlet, which converts here-strings to hash tables. For more information, see ConvertFrom-StringData.