简体   繁体   中英

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:

Incomplete string token. + 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 .

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

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

Objective : Running the snippet and assuming the PC will jump onto the next line automatically, since it has the ` character .

However, when I press F5 it works like a charm. Please forgive my ignorance on PowerShell, but what am I doing wrong here?

Additional info : This is my powershell information:

  • Name: Windows PowerShell ISE Host
  • Version: 5.1

Your errors are coming from features of PowerShell ISE.

First you're trying to use F8 which is "Run Selection", on the first line, with no selection made. That will implicitly select all the characters of the first line, then try to run that.

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.

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. 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).


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.


As a side note, I might recommend that you look for code elements which let you naturally use line breaks, such as the 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:

This is referred to as Statement Termination . When using Powershell, there are 2 statement terminator charcters.

  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); in my opinion ( and someone correct me if im wrong ), I think it was a bug which was a False-Positive .

The Backtik (`) also known as the Escape Character can extend a line that isn't extensible. 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.

In summary: it was a mistake on powershells end(:

PS - Sorry about the long post, since the first part isn't really needed to answer your question; 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. Error wasn't on Powershells end. Disregard this post (:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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