找不到 powershell 命令异常 -方法



在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 

我得到以下异常,如图所示,旧的 -Method 异常仍然存在。

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 

使用反引号 ' 字符。

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

最新更新