简体   繁体   English

使用 PowerShell 反引号字符执行代码段不起作用

[英]Using PowerShell backtick character for snippet execution doesn't work

When I run the backtick (`) command on this snippet:当我在此代码段上运行反引号 (`) 命令时:

Get-WmiObject win32_service | Where-Object { $_.pathname -notlike "C:\windows\*" -and $_.startmode -eq "auto" -and $_.startname -eq "localsystem"} | Select-Object displayname, `
pathname, startmode, startname | Format-List | Out-Host

I get some errors.我得到一些错误。 What are those errors ?这些错误是什么?

First , by pressing F8 just on the first line , I get this:首先,通过在第一行按 F8 ,我得到这个:

Incomplete string token.不完整的字符串标记。 + CategoryInfo: ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId: IncompleteString + CategoryInfo: ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId: IncompleteString

Objective : Running the snippet and assuming the PC will jump onto the next line automatically, since it has the ` character .目标运行代码段并假设 PC 将自动跳到下一行,因为它有 ` 字符

Second , when I highlight just the first line , by clicking to the left of the line numbers, I now get this:其次,当我只突出显示第一行时,通过单击行号的左侧,我现在得到了这个:

At line:1 char:170在行:1 字符:170

  • ... to" -and $_.startname -eq "localsystem"}|Select-Object displayname, ` ... to" -and $_.startname -eq "localsystem"}|Select-Object displayname, `
  •  ~ Missing expression after ',' > in pipeline element.
    • CategoryInfo: ParserError: (:) [], ParentContainsErrorRecordException CategoryInfo: ParserError: (:) [], ParentContainsErrorRecordException
    • FullyQualifiedErrorId: MissingExpression fullyQualifiedErrorId: MissingExpression

Objective : Running the snippet and assuming the PC will jump onto the next line automatically, since it has the ` character .目标运行代码段并假设 PC 将自动跳到下一行,因为它有 ` 字符

However, when I press F5 it works like a charm.但是,当我按 F5 时,它就像一个魅力。 Please forgive my ignorance on PowerShell, but what am I doing wrong here?请原谅我对 PowerShell 的无知,但我在这里做错了什么?

Additional info : This is my powershell information:附加信息:这是我的 powershell 信息:

  • Name: Windows PowerShell ISE Host名称: Windows PowerShell ISE主机
  • Version: 5.1版本:5.1

Your errors are coming from features of PowerShell ISE.您的错误来自 PowerShell ISE 的功能。

First you're trying to use F8 which is "Run Selection", on the first line, with no selection made.首先,您尝试在第一行使用F8 ,即“运行选择”,但未进行任何选择。 That will implicitly select all the characters of the first line, then try to run that.这将隐含 select 第一行的所有字符,然后尝试运行它。

When you do so, you get the incomplete string token error, and that's because the parser has encountered the lone backtick ` (the escape character), with no character following it.当你这样做时,你会得到不完整的字符串标记错误,这是因为解析器遇到了唯一的反引号` (转义字符),它后面没有字符。 That's because the newline at the end of the line, which the backtick is usually escaping (that's how it works as a line continuation character), is missing, since the single line selection didn't include it.那是因为缺少行尾的换行符,反引号通常是 escaping (这就是它作为续行字符的工作方式),因为单行选择不包括它。

Second, when I highlight just the first line, by clicking to the left of the line numbers其次,当我只突出显示第一行时,通过单击行号的左侧

Now in this case you'll notice that your selection has placed the cursor at the beginning of the next line.现在在这种情况下,您会注意到您的选择已将 cursor 放在下一行的开头 That means this selection does include the newline, and so you now have a complete string token.这意味着此选择确实包含换行符,因此您现在拥有一个完整的字符串标记。

Your error is that you've now ended your command with a comma, as though you're going to pass more parameters, and then nothing comes after (the rest of the parameters were not included in your selection).你的错误是你现在用逗号结束你的命令,好像你要传递更多的参数,然后什么都没有(参数的 rest 不包括在你的选择中)。


The root of the issue is that the commands in ISE that deal with running a selection, are doing exactly that, so if you want them to include things that are on the next line, you must include them in the selection too.问题的根源在于 ISE 中处理运行选择的命令正在执行此操作,因此如果您希望它们包含下一行的内容,则必须将它们也包含在选择中。


As a side note, I might recommend that you look for code elements which let you naturally use line breaks, such as the pipe |作为旁注,我可能会建议您寻找可以让您自然地使用换行符的代码元素,例如 pipe | character, operators, and scriptblock braces {} .字符、运算符和脚本块大括号{}

Get-WmiObject win32_service | 
    Where-Object { 
        $_.pathname -notlike "C:\windows\*" -and 
        $_.startmode -eq "auto" -and 
        $_.startname -eq "localsystem"
    } | 
    Select-Object displayname, pathname, startmode, startname | 
    Format-List | 
    Out-Host

This doesn't solve your selection problem, but it's nicer to read in my opinion, and doesn't require the awkward backtick.这并不能解决您的选择问题,但在我看来它更容易阅读,并且不需要笨拙的反引号。

Following up on @Santiagos link comment:跟进@Santiagos 链接评论:

This is referred to as Statement Termination .这称为语句终止 When using Powershell, there are 2 statement terminator charcters.使用 Powershell 时,有 2 个语句终止符

  1. Semicolon - ;分号 - ;
  2. Newline (sometimes)换行符(有时)

The rule for the newline is only sometimes due to the nature of how some users will use commands.换行的规则有时只是由于某些用户使用命令的性质。 Basically meaning that if the previous text is syntactically a complete statement, a new line is considered to be a statement termination.基本上意味着如果前面的文本在语法上是完整的语句,则新行被认为是语句终止。 So, if it isn't complete, the newline is treated as whitespace.因此,如果它不完整,则换行符将被视为空格。

Quick example:快速示例:

PS C:\Users\Abraham> 3 +
>> 4
7

When I added the + operator, it was expecting another argument which is why it didn't error out.当我添加+运算符时,它期待另一个参数,这就是它没有出错的原因。

In your case, I will assume the error came from powershells Tokenizer ( lexical analyzer ) when parsing through your command, as it read your backtick as an escape character for your comma (assuming it was complete - hence the statement terminator - reading the next line as a newline and not whitespace);在您的情况下,我将假设错误来自 powershells Tokenizer(词法分析器)在解析您的命令时,因为它会将您的反引号读取为逗号的转义字符(假设它是完整的 - 因此是语句终止符- 读取下一行作为换行符而不是空格); in my opinion ( and someone correct me if im wrong ), I think it was a bug which was a False-Positive .在我看来(如果我错了,有人纠正我),我认为这是一个错误,它是一个False-Positive

The Backtik (`) also known as the Escape Character can extend a line that isn't extensible. Backtik (`) 也称为转义字符,可以扩展不可扩展的行。 If the last character in the line is a backtick, then the newline will be treated as whitespace and not a " newline ".如果行中的最后一个字符是反引号,则换行符将被视为空格而不是“换行符”。

So you don't need that backtick due to your comma ( , ) telling powershell that theres more that come after it, which we reference back to the 2 paragraph for being syntactically incomplete.所以你不需要那个反引号,因为你的逗号 ( , ) 告诉 powershell 后面还有更多内容,我们参考第 2 段的语法不完整。

In summary: it was a mistake on powershells end(:总而言之:这是 powershells end(:

PS - Sorry about the long post, since the first part isn't really needed to answer your question; PS - 很抱歉这篇文章很长,因为第一部分并不是真正需要回答你的问题; just some info that can help you understand how it comes together.只是一些可以帮助您了解它如何组合在一起的信息。

EDIT:编辑:

Didn't see the F8 being the culprit here lol thanks to @briantist for pointing it out.没有看到F8是这里的罪魁祸首,哈哈,感谢@briantist 指出了这一点。 Error wasn't on Powershells end.错误不在 Powershell 端。 Disregard this post (:无视这篇文章(:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM