找到你要的答案

Q:calls to cmd from powershell not parsing correctly?

Q:调用cmd从PowerShell不是分析正确吗?

I am trying to run cURL.exe from powershell as part of a larger script to create various user accounts as new individuals are onboarded. (I cannot use invoke-method or invoke-webrequest because the header parameter wont accept a string)

The issue is that I can run it from a cmd window perfectly but I cannot seem to make the same thing run from powershell:

curl.exe -H "Authorization: {\"apiKey\":\"ResetAPIKey,\"username\":\"admin\"}"  -d "username=someUserName&email=somebody@domain.com&firstName=Joe&lastName=Friday"  "http://URL:8080/v1/users/create"

This runs just fine from cmd but when attempting to run from powershell, I get errors like 'url not found' or 'That path/method is not supported'

Additional format attempts:

  • encasing the whole thing in single quotes

  • replacing each /" (forward slash + double quote) with `" (backtick + double quote)

  • adding all the arguments to a here-string variable

  • variations of above

I would assume I can write it to a .bat file and then run that from powershell but I'd rather not be forced to do that if possible

I am trying to run cURL.exe from powershell as part of a larger script to create various user accounts as new individuals are onboarded. (I cannot use invoke-method or invoke-webrequest because the header parameter wont accept a string)

问题是,我可以从一个cmd窗口完美但我似乎不能让同样的事情从PowerShell:

curl.exe -H "Authorization: {\"apiKey\":\"ResetAPIKey,\"username\":\"admin\"}"  -d "username=someUserName&email=somebody@domain.com&firstName=Joe&lastName=Friday"  "http://URL:8080/v1/users/create"

这是刚刚好从CMD但是试图运行PowerShell时,我得到的错误就像“URL不存在”或“路径/方法不受支持的

附加格式尝试:

  • 整件事在单引号包围

  • 更换每个/”(斜杠+双引号)与`”(反引号+双引号)

  • 将所有参数添加到此处字符串变量

  • 以上的变化

我想我可以写一个.bat文件,然后运行,PowerShell的但我不愿意被迫做,如果可能的话

answer1: 回答1:

The stop parsing parameter is a good choice for this type of thing.

The stop-parsing symbol (--%), introduced in Windows PowerShell 3.0, directs Windows PowerShell to refrain from interpreting input as Windows PowerShell commands or expressions.

In your case though you need something a little different as you want variables in the mix that need to be resolved.

I figured there would be a dupe for this. Most of those answer are about converting curl.exe to something like Invoke-WebRequest which is something you should look into. In this case, if you have dynamic content use the call operator like this.

$arguments = "-H ""Authorization: {\""apiKey\"":\""ResetAPIKey,\""username\"":\""admin\""}""  -d ""username=someUserName&email=somebody@domain.com&firstName=Joe&lastName=Friday""  ""http://URL:8080/v1/users/create"""
&"curl.exe" $arguments

Noticed I doubled up the inner quotes on the string. If you look at the variable after it would display properly. Now you should be able to make changes.

停止解析参数是这种类型的东西的一个很好的选择。

The stop-parsing symbol (--%), introduced in Windows PowerShell 3.0, directs Windows PowerShell to refrain from interpreting input as Windows PowerShell commands or expressions.

在您的情况下,虽然你需要一些不同的,因为你想要变量的混合,需要解决。

我想会有欺骗这。大多数人的答案都将curl.exe像引用WebRequest这是你应该看看。在这种情况下,如果你有动态的内容使用这样的调用操作符。

$arguments = "-H ""Authorization: {\""apiKey\"":\""ResetAPIKey,\""username\"":\""admin\""}""  -d ""username=someUserName&email=somebody@domain.com&firstName=Joe&lastName=Friday""  ""http://URL:8080/v1/users/create"""
&"curl.exe" $arguments

注意到我在字符串上增加了内部引号。如果你看变量后,它会显示正确。现在你应该能够做出改变。

answer2: 回答2:

Passing complex command line to native application from PowerShell can be tricky. PowerShell can add extra quotes in some cases. Although, rules for this are not that complex, but them are not well (if at all) documented, and also there is some substantial changes in that rules in v2-v4 vs v5. IMHO, using stop parsing operator --%, as @Matt offered, is the best approach, in case you targeting on v3+.

Stop parsing operator does not limit you to use literal strings only. You still can use variables with it. That operator expand environment variables in CMD syntax: %VariableName%. So you can assign required values to some environment variables and use them in command line:

$Env:Env_UserName='someUserName'
$Env:Env_Email='somebody@domain.com'
$Env:Env_FirstName='Joe'
$Env:Env_LastName='Friday'
curl.exe --% -H "Authorization: {\"apiKey\":\"ResetAPIKey,\"username\":\"admin\"}"  -d "username=%Env_UserName%&email=%Env_Email%&firstName=%Env_FirstName%&lastName=%Env_LastName%"  "http://URL:8080/v1/users/create"

Other variant will be to generate full arguments line, you want to pass to native application, and than pass it in single environment variable:

$Arguments=...
$Env:Env_Arguments=$Arguments
curl.exe --% %Env_Arguments%

通过复杂的命令行从PowerShell的本地应用程序可能会非常棘手。PowerShell可以在某些情况下,添加额外的报价。虽然,这是规则不复杂,但他们都没有很好的(如果有的话)的记录,也有v2-v4 VS V5在一些实质性的变化规律。恕我直言,以停止解析算子-- %,“马特提供,是最好的方法,如果你对V3 +。

停止解析运算符不限制您只使用文字字符串。您仍然可以使用变量与它。运营商扩大在CMD句法环境变量:%变量名%。因此,您可以将要求的值分配给一些环境变量,并将它们应用到命令行中:

$Env:Env_UserName='someUserName'
$Env:Env_Email='somebody@domain.com'
$Env:Env_FirstName='Joe'
$Env:Env_LastName='Friday'
curl.exe --% -H "Authorization: {\"apiKey\":\"ResetAPIKey,\"username\":\"admin\"}"  -d "username=%Env_UserName%&email=%Env_Email%&firstName=%Env_FirstName%&lastName=%Env_LastName%"  "http://URL:8080/v1/users/create"

其他变体将生成完整的参数行,您希望传递给本地应用程序,而不是将其传递给单个环境变量:

$Arguments=...
$Env:Env_Arguments=$Arguments
curl.exe --% %Env_Arguments%
answer3: 回答3:

Matt's answer with the call operator looks like it should work, but if it's not there's a similar alternative I've had good luck with before. Instead of passing your arguments as a string, you can pass them as an array and PowerShell will handle them correctly. In your case, it'd look something like this:

$arguments = @("-H", 'Authorization: {\"apiKey\":\"ResetAPIKey,\"username\":\"admin\"}', "-d", '"username=someUserName&email=somebody@domain.com&firstName=Joe&lastName=Friday"', 'http://URL:8080/v1/users/create')

& "curl.exe" $arguments

Matt的回答与呼叫运营商看起来应该工作,但如果不是有一个类似的选择,我有好运气之前。而不是通过你的参数为一个字符串,你可以作为一个数组,PowerShell会正确处理这些关系,通过他们。在你的情况下,它看起来像这样:

$arguments = @("-H", 'Authorization: {\"apiKey\":\"ResetAPIKey,\"username\":\"admin\"}', "-d", '"username=someUserName&email=somebody@domain.com&firstName=Joe&lastName=Friday"', 'http://URL:8080/v1/users/create')

& "curl.exe" $arguments
powershell  curl  cmd