2015-06-19 73 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.