I am testing group migration using ADMT cmd line, but the migration is failing for a specific case when the OU name contains double quotes.
ADMT GROUP /n "TestGroup" /sd:Child.A.COM /sdc.CHILD.A.COM /td.COM /tdc.A.COM /to:"ParentOU/TEST!##$%^&*()_+{}|:"<>?[]\;',./" /intraforest:yes
In cmd this throws "> was unexpected at this time" and in powershell it keeps waiting for more parameters. The main purpose is to convert this to a c# script the migrates the users/groups but it failed in the testing phase with cmd/powershell. Is there any way to make this possible at least in C#?
I have tried escaping the double quotes with "", ^", ", `" but nothing seems to work. I have also tried assigning the value to a variable and using the variable in powershell. Using "" (as suggested in this Escaping special characters in cmd) is the only time the command runs but it still throws the following error.
Error: Unable to migrate groups. Unable to bind to container
'ParentOU/TEST!##$%^&()+{}|:<>?[];',./ /intraforest:yes'. Unable to
get distinguished name for
'A.COM/ParentOU/TEST!##$%^&;()+{}|:<>?[];',./ /intraforest:yes'. :
The parameter is incorrect. (0x80070057)
The same is working if I create another OU with same name except for the double quotes.
Please help in resolving this issue.
You can do solve it in different ways:
With escaping the characters with carets outside of the quote
setlocal EnableDelayedExpansion
set "to_param=ParentOU/TEST^!##$%^&*()_+{}|:"^^^^^<^^^^^>?[];',./"
call ADMT GROUP /to:"%%to_param%%"
The main problem will be the ADMT program, you need to know how it parses the arguments, particularly with regard to the rules how it escape quotes inside arguments.
You could test \" in set "to_param=ParentOU...\"^^^^^<^^^^^>?[];',./"
Related
I know there are lots of posts regarding this, but nothing worked for me.
I am trying to run this command line in PowerShell:
C:/Program Files (x86)/ClamWin/bin/clamd.exe --install
I have this in PowerShell:
&"C:/Program Files (x86)/ClamWin/bin/clamd.exe --install"
But all this does is execute clamd.exe, ignoring the --install parameter
How can I get the full command line to run?
Josef Z's comment on the question provides the solution:
& "C:/Program Files (x86)/ClamWin/bin/clamd.exe" --install # double-quoted exe path
or, given that the executable path is a literal (contains no variable references or subexpressions), using a verbatim (single-quoted) string ('...'):
& 'C:/Program Files (x86)/ClamWin/bin/clamd.exe' --install # single-quoted exe path
As for why your own solution attempt failed: The call operator, &, expects only a command name/path as an argument, not a full command line.
Invoke-Expression accepts an entire command line, but that complicates things further and can be a security risk.
As for why this is the solution:
The need for quoting stands to reason: you need to tell PowerShell that C:/Program Files (x86)/ClamWin/bin/clamd.exe is a single token (path), despite containing embedded spaces.
&, the so-called call operator, is needed, because PowerShell has two fundamental parsing modes:
argument mode, which works like a traditional shell, where the first token is a command name, with subsequent tokens representing the arguments, which only require quoting if they contain shell metacharacters (chars. with special meaning to PowerShell, such as spaces to separate tokens);
that is why --install need not, but can be quoted (PowerShell will simply remove the quotes for you before passing the argument to the target executable.)
expression mode, which works like expressions in programming languages.
PowerShell decides based on a statement's first token what parsing mode to apply:
If the first token is a quoted string - which we need here due to the embedded spaces in the executable path - or a variable reference (e.g., $var ...), PowerShell parses in expression mode by default.
A quoted string or a variable reference as an expression would simply output the string / variable value.
However, given that we want to execute the executable whose path is stored in a quoted string, we need to force argument mode, which is what the & operator ensures.
Generally, it's important to understand that PowerShell performs nontrivial pre-processing of the command line before the target executable is invoked, so what the command line looks like in PowerShell code is generally not directly what the target executable sees.
If you reference a PowerShell variable on the command line and that variable contains embedded spaces, PowerShell will implicitly enclose the variable's value in double quotes before passing it on - this is discussed in this answer to the linked question.
PowerShell's metacharacters differ from that of cmd.exe and are more numerous (notably, , has special meaning in PowerShell (array constructor), but not cmd.exe - see this answer).
To simplify reuse of existing, cmd.exe-based command lines, PowerShell v3 introduced the special stop-parsing symbol, --%, which turns off PowerShell's normal parsing of the remainder of the command line and only interpolates cmd.exe-style environment-variable references (e.g., %USERNAME%).
If I execute the following command on a Windows 8.1 machine:
robocopy "C:\Temp\A\" "C:\Temp\B\"
Robocopy fails due to the following problem:
Source : C:\Temp\A" C:\Temp\B"\
Dest -
...
ERROR : No Destination Directory Specified.
It looks like \ is used as some kind of escape character (which is not normal behavior in the windows command line) The final \" is even transformed to "\ which I do not understand at all. Why's that so?
Note: this is not the default behavior of the command line, if they would have used argv[1] and argv[2] within robocopy, they would've retrieved the correct arguments.
Why are they using their own command line parsing? It really confused me for the last hour...
You should omit the trailing backslashes.
From http://ss64.com/nt/robocopy.html :
If either the source or destination are a "quoted long foldername" do
not include a trailing backslash as this will be treated as an escape
character, i.e. "C:\some path\" will fail but "C:\some path\\" or
"C:\some path." or "C:\some path" will work.
robocopy is not an exception. Any executable uses its own line parser to determine the arguments that were sent to it. The OS just uses the API to create the process and pass to it a string to be handled as arguments. The process can handle the string as it wants.
In the case of robocopy, the parser used is the standard Microsoft C startup code. This parser follow the rules described here, and in the full list you can found
A double quotation mark preceded by a backslash, \", is interpreted as
a literal double quotation mark (").
I'm trying to put together an SFTP command to be run through Powershell. The executable I have access to is SFTPC.exe (Bitvise Tunnelier)
The command I'm trying is
sftpc.exe user#ftp.domain.com -pw=password -unat=y -cmd="ls \"somefile.txt\""
According to the documentation at https://www.bitvise.com/files/sftpc-v4.14-usage.txt this should log in and run the command ls "somefile.txt" (quotes are escaped within the command parameter)
What actually happens is that I get another line for input, as if there's an unclosed quote.
I've tried adding an extra quote to the end
sftpc.exe user#ftp.domain.com -pw=password -unat=y -cmd="ls \"somefile.txt\"""
This connects and logs in, but the colland it tries to run is ls \somefile.txt"
Note the trailing quote and the leading slash.
So it looks like I'm missing something in the quote escaping, but I can't see what I might be doing wrong. I've also tried replacing double quotes with single in a couple of different places, experiments that usually end in a syntax error.
The escape character in powershell is not a backslash, it's the backtick.
Does the below work?
sftpc.exe user#ftp.domain.com -pw=password -unat=y -cmd="ls `"somefile.txt`""
I used msbuild to build a deployment package for my website. I am trying to use the generated deploy.cmd file located in the package to deploy to a remote server. I am using web deploy's parameters and I would like to set these at the command line using the -setParam flag.
The usage instructions for WebDeploy indicate that I need to enclose the -setParam configuration option in quotation marks because it contains an '=' character. However, the deploy.cmd file passes the entire option, including quotation marks, to the msdeploy executable. The msdeploy executable fails to run because the option does not start with the "-" character.
Error: Unrecognized argument '"-setParam:TestParam=ABC123"'. All arguments must begin with "-".
What do I need to do in order to get the setParam call working with the cmd file generated by msBuild?
Have successfully used -setParam option in the following syntax, enclosing entire option in double quotes, and dubling double quotes inside:
...deploy.cmd "-setParam:name=""IIS Web Application Name"",value=""example.com"""
You don't need to wrap the entire parameter in quotes because of the equals sign. Just the parameter key and value because it may contain spaces.
-setParam:"TestParam"="ABC123"
should work for your example. Or in real world:
-setParam:"IIS Web Application Name"="example.com"
I am running a T-SQL script, called from a powershell script, that contains a text row which includes a sequence containing $(MV).
When I run the script I get the error "'MV' scripting variable not defined." which I assume is because it interpretates the $(MV) string as a variable instead of being a part of the text string.
How can I write the dollar sign as an escape sequence? Is that possible in a string?
Are you executing the script with sqlcmd? It will interpret $(variablename) and try to expand it.
You can disable this using the -x command-line option.
Oh, if you're using Invoke-Sqlcmd you can use the -DisableVariables parameter.
I was assuming you didn't want to have the sqlcmd variable substitution done. If you were just trying to get the $ past Powershell, use the back-tick (`$) as in another suggestion.
You can use the backtick character to escape a $ sign in PowerShell:
`$