Windows 找不到powershell命令异常-方法

Windows 找不到powershell命令异常-方法,windows,powershell,Windows,Powershell,在powershell中使用以下内容时,它从网站复制,通过应用程序处理在线支付。我在写这篇文章时更改了访问令牌和唯一密钥 $authHeader = @{ Authorization = 'Bearer {0}' -f "{{ACCESS_TOKEN}}" } $body = '{ "idempotency_key": "{{UNIQUE-KEY}}", "autocomplete": true, "amount_money": { "amount": 100,

在powershell中使用以下内容时,它从网站复制,通过应用程序处理在线支付。我在写这篇文章时更改了访问令牌和唯一密钥

$authHeader = @{ Authorization = 'Bearer  {0}' -f "{{ACCESS_TOKEN}}" }
$body = '{
   "idempotency_key": "{{UNIQUE-KEY}}",
   "autocomplete": true,
   "amount_money": {
     "amount": 100,
     "currency": "USD"
   },
   "source_id": "cnon:card-nonce-ok"
   }
}'

Invoke-RestMethod -Uri https://connect.squareupsandbox.com/v2/payments |
  -Method Post |
  -ContentType "application/json" |
  -Headers $authHeader |
  -Body $body 
我得到以下例外

-Method : The term '-Method' is not recognized as the name of a cmdlet, function, script file, or operable program.
 Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
  At line:14 char:4   
     +    -Method Post |                                      
 +    ~~~~~~~                                                                                                               
 + CategoryInfo          : ObjectNotFound: (-Method:String) [], CommandNotFoundException                                
 + FullyQualifiedErrorId : CommandNotFoundException   
或者,如果我删除这些|并像评论所说的那样使用空格

$authHeader = @{ Authorization = 'Bearer  {0}' -f "{{ACCESS_TOKEN}}" }
$body = '{
   "idempotency_key": "{{UNIQUE-KEY}}",
   "autocomplete": true,
   "amount_money": {
     "amount": 100,
     "currency": "USD"
   },
   "source_id": "cnon:card-nonce-ok"
   }
}'

Invoke-RestMethod -Uri https://connect.squareupsandbox.com/v2/payments 
  -Method Post 
  -ContentType "application/json" 
  -Headers $authHeader 
  -Body $body 
我得到以下异常,如图所示,旧方法异常仍然存在

Invoke-RestMethod : {"errors": [{"code": "UNAUTHORIZED","detail": "Your request did not include an `Authorization`
http header with an access token. The header value is expected to be of the format \"Bearer TOKEN\"  (without
quotation marks), where TOKEN is to be replaced with your access token  (e.g. \"Bearer {{Access token}}\"). For
more information, see https://docs.connect.squareup.com/api/connect/v2/#requestandresponseheaders. If you are seeing
this error message while using one of our officially supported client libraries, please report this to
developers@squareup.com. ","category": "AUTHENTICATION_ERROR"}]}
At line:13 char:1
+ Invoke-RestMethod -Uri https://connect.squareupsandbox.com/v2/payment ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
-Method : The term '-Method' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:14 char:4
+    -Method Post
+    ~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-Method:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

-ContentType : The term '-ContentType' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:15 char:4
+    -ContentType "application/json"
+    ~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-ContentType:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

-Headers : The term '-Headers' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:16 char:4
+    -Headers $authHeader
+    ~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-Headers:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

-Body : The term '-Body' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:17 char:4
+    -Body $body
+    ~~~~~
    + CategoryInfo          : ObjectNotFound: (-Body:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

虽然管道
|
可用于将一行拆分为多行,但该语句仅适用于首先使用管道的一系列命令

绑定到命令的参数不能像这样被管道分隔

你有3个选择

将所有参数放在同一行上,空格分隔,如下所示:

Invoke-RestMethod -Uri https://connect.squareupsandbox.com/v2/payments -Method Post -ContentType "application/json" -Headers $authHeader -Body $body
使用Splatting并将参数定义到哈希表中,然后将它们应用到命令中,使用
@
指定要传递的是参数列表和这些值,而不仅仅是单个哈希表参数

$params = @{
    ContentType = "application/json"
    Method = 'Post'
    Body = $body
    Headers = $authHeader
    Uri = 'https://connect.squareupsandbox.com/v2/payments'
}
Invoke-RestMethod @params 
使用backtick`字符

Invoke-RestMethod -Uri https://connect.squareupsandbox.com/v2/payments `
    -Method Post `
    -ContentType "application/json" `
    -Headers $authHeader `
    -Body $body 

虽然管道
|
可用于将一行拆分为多行,但该语句仅适用于首先使用管道的一系列命令

绑定到命令的参数不能像这样被管道分隔

你有3个选择

将所有参数放在同一行上,空格分隔,如下所示:

Invoke-RestMethod -Uri https://connect.squareupsandbox.com/v2/payments -Method Post -ContentType "application/json" -Headers $authHeader -Body $body
使用Splatting并将参数定义到哈希表中,然后将它们应用到命令中,使用
@
指定要传递的是参数列表和这些值,而不仅仅是单个哈希表参数

$params = @{
    ContentType = "application/json"
    Method = 'Post'
    Body = $body
    Headers = $authHeader
    Uri = 'https://connect.squareupsandbox.com/v2/payments'
}
Invoke-RestMethod @params 
使用backtick`字符

Invoke-RestMethod -Uri https://connect.squareupsandbox.com/v2/payments `
    -Method Post `
    -ContentType "application/json" `
    -Headers $authHeader `
    -Body $body 

不要使用管道字符
|
分隔参数,它们应该是空格separated@arco444它给了我另一个关于授权的错误,还有关于-Method的错误。不要使用管道字符
|
来分隔参数,它们应该是空间separated@arco444它给了我另一个关于授权的错误,仍然是关于-Method的错误。