Under Windows 10 Enterprise LTSC, I have a simple CMD ff.bat which contains:
powershell -Command (Measure-Command {ffmpeg.exe -hide_banner -i %* E:\%~n1.mkv}).ToString
In CMD> ff test.mp4 and ff "test.mp4" works but NOT
ff "E:\Serie.(2009).8x04.episode.FR.LD.WEBRip.x264-LiBERTY.[server.org.ru].mkv"
Given PowerShell error is:
Au caractère Ligne:1 : 60
+ (Measure-Command {ffmpeg.exe -hide_banner -i Serie.(2009).8x04.episode.FR.LD.WEBRip.x264-LiBERTY.[server.org.ru].mkv ...
+
~
Nom de propriété manquant après l’opérateur de référence.
(Missing property after reference operator)
Au caractère Ligne:1 : 144
+ ... .FR.LD.WEBRip.x264-LiBERTY.[server.org.ru].mp4 E:\Serie ...
+ ~
Nom de propriété manquant après l’opérateur de référence.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingPropertyName
I guess the problem comes from the .-[]():\ and brackets and parenthesis characters found in the file name.
Could you give me the proper syntax for this to work in CMD, please.
As a bonus, you can give me the syntax to make it work in PS if it can.
Please note I'm a complete newbie in PowerShell and need examples more than suggestions.
Thanks in advance, Marc.
Your assumption here...
I guess the problem comes from the .-:\ and brackets and parenthesis
characters found in the file name
...is correct.
There are special characters in every language use case. They can only be used as defined in the language specification. If any string you are using has these types of characters, you either need to remove them or properly terminate them.
<#
LONG DESCRIPTION
Windows PowerShell supports a set of special character sequences that
are used to represent characters that are not part of the standard
character set.
The special characters in Windows PowerShell begin with the backtick
character, also known as the grave accent (ASCII 96).
The following special characters are recognized by Windows PowerShell:
`0 Null
`a Alert
`b Backspace
`f Form feed
`n New line
`r Carriage return
`t Horizontal tab
`v Vertical tab
--% Stop parsing
#>
Get-help -Name about_Special_Characters
Even parenthesis, braces, brackets, also have special meaning, and cannot be used in file names.
PowerShell - Special Characters And Tokens
So, by what you show in that failing file name, you need to rename those.
Related
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.
Semicolon - ;
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 (:
I'm attempting to check the RelayForAuth setting for my Windows SMTP Server using the below commands. Powershell appears to display the correct result 'False' but when running the same command via command prompt, it generates an error:
Powershell Example:
([ADSI]"IIS://localhost/smtpsvc/1".RelayForAuth -like "*0*")
Output:
False
Command Prompt Example:
powershell -command "([ADSI]"IIS://localhost/smtpsvc/1".RelayForAuth -like "*0*")"
Output:
At line:1 char:8
+ ([ADSI]IIS://localhost/smtpsvc/1.RelayForAuth -like *0*)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token 'IIS://localhost/smtpsvc/1.RelayForAuth' in expression or
statement.
At line:1 char:8
+ ([ADSI]IIS://localhost/smtpsvc/1.RelayForAuth -like *0*)
+ ~
Missing closing ')' in expression.
At line:1 char:56
+ ([ADSI]IIS://localhost/smtpsvc/1.RelayForAuth -like *0*)
+ ~
Unexpected token ')' in expression or statement.
+ CategoryInfo : ParserError: (:) [],
ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
Since you're nesting (embedding) " chars. - to be passed verbatim to PowerShell - inside the syntactic outer double-quoting ("..."), you must escape those nested " chars.
Even though PowerShell-internally ` serves as the escape character, calling the PowerShell CLI (powershell.exe / pwsh) from the outside (cmd.exe) requires \-escaping of ":
# Embedded " chars. must be \-escaped
powershell -command "([ADSI]\"IIS://localhost/smtpsvc/1\").RelayForAuth -like \"*0*\""
Note that you can avoid the need for this escaping if you single-quote the strings inside the overall "..." string.
While this works fine in your case, given that your strings have only verbatim content, note that this is generally only an option if no string interpolation is required:
# Embedded strings use '...' -> no escaping needed.
powershell -command "([ADSI]'IIS://localhost/smtpsvc/1').RelayForAuth -like '*0*'"
Caveat: Using single-quoting to enclose the overall command ('...') does not work as expected from cmd.exe: the latter doesn't recognize these as quoting, and PowerShell simply interprets the string as using its syntax for a verbatim string, and therefore simply prints the contents of the string.
For more information, see this answer.
I'm trying to better understand how backticks work in PowerShell. This works and executes the ipconfig command:
$a = "ipc"
$b = "onf`ig"
iex $a$b
However, if the backtick is moved one character to the left, before the "f", the command breaks...
$a = "ipc"
$b = "on`fig"
iex $a$b
Another example of this:
who`ami
If the backtick is anywhere else, the whoami command will work just fine. With a backtick in the middle, it breaks.
What's happening here? Why does the placement of the backtick's matter so much?
These are becuase some special characters in powershell.
In powershell there are some special characters which are not in standard character set. They start with back tick to show special meaning. They are:
`0 Null
`a Alert
`b Backspace
`e Escape
`f Form feed
`n New line
`r Carriage return
`t Horizontal tab
`u{x} Unicode escape sequence
`v Vertical tab
Here when you escape "a" with backtick
means alert powershell (whoami) and when you escape "f" with backtick means form feed (ipconfig), so both commands break.
And when you escape the other character, commands don't break becuase then characters not render the special meaning.
Though I don't agree with all the author of this article says. Most of the is valid when it comes to use of the graveyard accents\bact tick.
It does have its use cases, but not for what you are showing.
Bye Bye Backtick: Natural Line Continuations in PowerShell]
See also:
about_Special_Characters - PowerShell | Microsoft Docs
PowerShell - Special Characters And Tokens
Grave_accent
Use in programming Programmers use the grave accent symbol as a
separate character (i.e., not combined with any letter) for a number
of tasks. In this role, it is known as a backquote, or backtick.
Many of the Unix shells and the programming languages Perl, PHP, and
Ruby use pairs of this character to indicate command substitution,
that is, substitution of the standard output from one command into a
line of text defining another command. For example, using $ as the
symbol representing a terminal prompt, the code line...
How-to: Escape characters, Delimiters and Quotes
I am running Windows 7 and (have to) use Turbo Grep (Borland something) to search in a file.
I have 2 version of this file, one encoded in UTF-8 and one in ANSI.
If I run the following grep on the ANSI file, I get the expected results, but I get no results with the same statement on the UTF-8 file:
grep -ni "[äöü]" myfile.txt
[-n for line numbers, -i for ignoring cases]
The Turbo Grep Version is :
Turbo GREP 5.6 Copyright (c) 1992-2010 Embarcadero Technologies, Inc.
Syntax: GREP [-rlcnvidzewoqhu] searchstring file[s] or #filelist
GREP ? for help
Help for this command lists:
Options are one or more option characters preceded by "-", and optionally
followed by "+" (turn option on), or "-" (turn it off). The default is "+".
-r+ Regular expression search -l- File names only
-c- match Count only -n- Line numbers
-v- Non-matching lines only -i- Ignore case
-d- Search subdirectories -z- Verbose
-e Next argument is searchstring -w- Word search
-o- UNIX output format Default set: [0-9A-Z_]
-q- Quiet: supress normal output
-h- Supress display of filename
-u xxx Create a copy of grep named 'xxx' with current options set as default
A regular expression is one or more occurrences of: One or more characters
optionally enclosed in quotes. The following symbols are treated specially:
^ start of line $ end of line
. any character \ quote next character
* match zero or more + match one or more
[aeiou0-9] match a, e, i, o, u, and 0 thru 9 ;
[^aeiou0-9] match anything but a, e, i, o, u, and 0 thru 9
Is there a problem with the encoding of these charactes in UTF-8? Might there be a problem with Turbo Grep and UTF-8?
Thanks in advance
Yes there are a different w7 use UTF-16 little endian not UTF-8, UTF-8 is used in unix, linux and plan 9 for cite a few OS.
Jon Skeet explain:1
ANSI: There's no one fixed ANSI encoding - there are lots of them. Usually when people say "ANSI" they mean "the default code page for my system" which is obtained via Encoding.Default, and is often Windows-1252
UTF-8: Variable length encoding, 1-4 bytes covers every current character. ASCII values are encoded as ASCII.
UTF-16 is more similar to ANSI so for this reason with ANSI work well.
if you use only ascii both encodings are usable, but with special characters as ä ö ü etc you need use UTF-16 in windows and UTF-8 in the others
I have written a c program that retrieves arguments from the command line under Windows. One of the arguments is a regular expression. So I need to retrieve special characters such as "( , .", etc., but cmd.exe treats "(" as a special character.
How could I input these special character?
thanks.
You can put the arguments in quotes:
myprogram.exe "(this is some text, with special characters.)"
Though I wouldn't assume that parentheses cause problems unless you are using blocks for conditional statements or loops in a batch file. The usual array of characters that are treated specially by the shell and need quoting or escaping are:
& | > < ^
If you use those in your regular expression, then you need quotes, or escape those characters:
myprogram "(.*)|[a-f]+"
myprogram (.*)^|[a-f]+
(^ is the escape character which causes the following character to be not interpreted by the shell but instead used literally)
You can generally prefix any character with ^ to turn off its special nature. For example:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\Pax>echo No ^<redirection^> here and can also do ^
More? multi-line, ^(parentheses^) and ^^ itself
No <redirection> here and can also do multi-line, (parentheses) and ^ itself
C:\Documents and Settings\Pax>
That's a caret followed by an ENTER after the word do.