Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t translate curl调用PowerShell中的WebRequest(-unsecure/-k未找到)_Powershell_Curl - Fatal编程技术网

Can';t translate curl调用PowerShell中的WebRequest(-unsecure/-k未找到)

Can';t translate curl调用PowerShell中的WebRequest(-unsecure/-k未找到),powershell,curl,Powershell,Curl,我有一个原始的curl调用,据说它是在Unix环境中工作的(或者在提供者的办公室使用的任何东西) 使用,我将标志和属性交换到以下内容 Invoke-WebRequest -User ybeepbeepbeepa:eboopboopboopa -Method POST -Headers @{"Content-Type"="application/x-www-form-urlencoded"} -Uri "https://xxx/oauth2/token?grant_type

我有一个原始的curl调用,据说它是在Unix环境中工作的(或者在提供者的办公室使用的任何东西)

使用,我将标志和属性交换到以下内容

Invoke-WebRequest 
  -User ybeepbeepbeepa:eboopboopboopa 
  -Method POST 
  -Headers @{"Content-Type"="application/x-www-form-urlencoded"} 
  -Uri "https://xxx/oauth2/token?grant_type=mobile&customerId=SE.B2C/abcd&pin=1234&scope=openid" 
我唯一没有翻译的部分是-k,它应该相当于-不安全。通过查看上述文档,我找到了一些可能的、虽然牵强的替代方案(如-AllowUnencryptedAuthentication),但都失败了,我也没有主意了

  • 在PowerShell的Invoke WebRequest中,curl的--unsecure(或-k)的等价物是什么(它意外地被批准为curl,由于标志不同,这与duck混淆)
  • 命令的其余部分是否正确移植到PowerShell?(我已经收缩了一些标志,并将它们与URL一起烘焙为querty字符串。我还不能完全确定标题的语法。)

  • 代替
    -k
    ,您需要使用
    ServicePointManager
    类为应用程序域设置证书验证例程:

    [System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
    
    对于
    -u
    标志,您需要:

    function Get-BasicAuthCreds {
        param([string]$Username,[string]$Password)
        $AuthString = "{0}:{1}" -f $Username,$Password
        $AuthBytes  = [System.Text.Encoding]::Ascii.GetBytes($AuthString)
        return [Convert]::ToBase64String($AuthBytes)
    }
    
    $Headers = @{"Content-Type"="application/x-www-form-urlencoded"} 
    $Headers['Authorization'] = "Basic $(Get-BasicAuthCreds ybeepbeepbeepa eboopboopboopa)"
    
    Invoke-WebRequest -Method POST -Headers $Headers -Uri "https://xxx/oauth2/token?grant_type=mobile&customerId=SE.B2C/abcd&pin=1234&scope=openid"
    
    如果要内联生成凭证字符串,可以这样做(尽管它有点笨重):


    当您拥有自签名证书并且使用5.x版或更低版本的powershell时,前一篇文章应该可以帮助您使用-k:是否可以将其内联表达而不是函数?太棒了。不过,仍将有两行执行。我希望有一个内联的和一行,我可以发送给我的同事作为一个神奇的药丸(你知道-粘贴在这个,并按下回车键,没有思考)。不过,我认为,只要我们能够在PowerShell中执行变量声明,这就足够了。(例如,访问$PROFILE文件时,我们受到了极大的限制。)
    -替换“\r?\n”,;”;-)
    
    function Get-BasicAuthCreds {
        param([string]$Username,[string]$Password)
        $AuthString = "{0}:{1}" -f $Username,$Password
        $AuthBytes  = [System.Text.Encoding]::Ascii.GetBytes($AuthString)
        return [Convert]::ToBase64String($AuthBytes)
    }
    
    $Headers = @{"Content-Type"="application/x-www-form-urlencoded"} 
    $Headers['Authorization'] = "Basic $(Get-BasicAuthCreds ybeepbeepbeepa eboopboopboopa)"
    
    Invoke-WebRequest -Method POST -Headers $Headers -Uri "https://xxx/oauth2/token?grant_type=mobile&customerId=SE.B2C/abcd&pin=1234&scope=openid"
    
    $Headers = @{
      "Content-Type"  = "application/x-www-form-urlencoded"} 
      "Authorization" = "Basic $([Convert]::ToBase64String([System.Text.Encoding]::Ascii.GetBytes('ybeepbeepbeepa:eboopboopboopa')))"
    }