如何将获取X-SDS-AUTH-TOKEN的Curl命令转为PowerShell脚本?
Convert Curl Auth Request to PowerShell Script
Here's a direct translation of your curl command into PowerShell, along with steps to extract the X-SDS-AUTH-TOKEN:
Basic Script (PowerShell 6+)
# Replace with your actual credentials $username = "username" $password = "password" # Encode credentials for Basic Authentication (equivalent to curl's -u flag) $authBase64 = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$username`:$password")) # Define request headers $headers = @{ "Authorization" = "Basic $authBase64" } try { # Send request, mirroring curl's -L (follow redirects), -k (ignore SSL), -v (verbose) $response = Invoke-WebRequest -Uri "https://172.31.110.16:4443/login" ` -Headers $headers ` -SkipCertificateCheck ` -MaximumRedirection 10 ` -Verbose # Extract the auth token from response headers $authToken = $response.Headers["X-SDS-AUTH-TOKEN"] if ($authToken) { Write-Host "X-SDS-AUTH-TOKEN: $authToken" } else { Write-Host "Token not found in response headers." } } catch { Write-Error "Request failed: $_" }
For Windows PowerShell 5.1
If you're using the older Windows PowerShell 5.1, the -SkipCertificateCheck switch isn't available. Add this code at the start of your script to bypass SSL certificate validation (equivalent to curl's -k):
# Bypass SSL certificate validation (only for Windows PowerShell 5.1) Add-Type @" using System.Net; using System.Security.Cryptography.X509Certificates; public class TrustAllCertsPolicy : ICertificatePolicy { public bool CheckValidationResult( ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem) { return true; } } "@ [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
Key Equivalents to Your Curl Command
$authBase64construction: Replaces curl's-u username:password(Basic Authentication header)-SkipCertificateCheck: Replaces curl's-k(disables SSL certificate verification)-MaximumRedirection 10: Replaces curl's-L(follows up to 10 redirects; adjust the number if needed)-Verbose: Provides detailed request/response logs like curl's-v
Troubleshooting Tips
- If the token isn't returned, verify if the login endpoint expects a
POSTrequest instead ofGET. If so, add the-Method POSTparameter toInvoke-WebRequest. - Check if the service requires additional headers (e.g.,
Content-Type). You can add these to the$headershashtable. - Inspect the full response content with
$response.Contentto see if the token is embedded in the body instead of headers.
内容的提问来源于stack exchange,提问作者j-onathan
相关产品推荐
相关产品推荐

