PowerShell的Invoke-RestMethod等价于curl -u(基本认证) [英] PowerShell's Invoke-RestMethod equivalent of curl -u (Basic Authentication)

查看:1416
本文介绍了PowerShell的Invoke-RestMethod等价于curl -u(基本认证)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相当于

  curl -u username:password ... 
Invoke-RestMethod
中的



<我试过这个:

  $ securePwd = ConvertTo-SecureStringpassword-AsPlainText -Force 
$ credential = Object System.Management.Automation.PSCredential($ username,$ securePwd)

Invoke-RestMethod -Credential $ credential ...

但它返回401,未授权。

解决方案

为我工作到目前为止:

  $ base64AuthInfo = [Convert] :: ToBase64String([Text.Encoding] :: ASCII.GetBytes ({0}:{1}-f $ username,$ password)))

Invoke-RestMethod -Headers @ {Authorization =(Basic {0}-f $ base64AuthInfo) } ...

但我不相信没有更好的办法。 >

What is the equivalent of

curl -u username:password ...

in PowerShell's Invoke-RestMethod? I tried this:

$securePwd = ConvertTo-SecureString "password" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($username, $securePwd)

Invoke-RestMethod -Credential $credential ...

but it returns 401, Unauthorized.

解决方案

This is the only method that worked for me so far:

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))

Invoke-RestMethod -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} ...

But I don't believe there isn't a better way.

这篇关于PowerShell的Invoke-RestMethod等价于curl -u(基本认证)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆