powershell-将动态令牌值传递给从csv文件读取的Rest API URL
我正在尝试通过powershell进行REST API调用,如下所示。我想通过csv或文本文件传递标记的动态值 非常感谢您的帮助powershell-将动态令牌值传递给从csv文件读取的Rest API URL,powershell,restapi,Powershell,Restapi,我正在尝试通过powershell进行REST API调用,如下所示。我想通过csv或文本文件传递标记的动态值 非常感谢您的帮助 $user = 'username' $pass = 'password' $pair = "$($user):$($pass)" $encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair)) $basicAuthVal
$user = 'username'
$pass = 'password'
$pair = "$($user):$($pass)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"
$Params = @{
"URI" = '<domain>/api/v1/validate-token?transactionId=486275931&**token**=741963&sourceIdentifier=IND&partnerId=159753&aggregatorId=852963741'
"Method" = 'GET'
"Headers" = @{
Authorization = $basicAuthValue
token =
}
}
$user='username'
$pass='password'
$pair=“$($user):$($pass)”
$encodedCreds=[System.Convert]::tobase64字符串([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue=“基本$encodedCreds”
$Params=@{
“URI”='/api/v1/validate token?transactionId=486275931&**token**=741963&sourceIdentifier=IND&partnerId=159753&aggregatorId=852963741”
“方法”=“获取”
“标题”=@{
授权=$basicAuthValue
令牌=
}
}
如果要替换URI中以及标记=
值中的标记(可能还有其他变量),则需要在URI字符串周围使用双引号字符,或者使用更优雅的(IMO)-f
格式运算符
假设您的CSV如下所示:
partnerId,aggregatorId,transactionId,token
159753,852963741,486275931,123456
888888,999999999,789789789,654321
然后你可以做:
- 这是使用双引号和
$()
“动态值”基于什么?您可以尝试使用类似于
“token”=$(获取内容。\test.txt)
的东西,如果您要从csv获取它-您可以尝试导入csv
cmdlet。谢谢,它完全按照预期工作。
Import-Csv -Path '<path\to\the\file.csv>' | ForEach-Object {
$Params = @{
"URI" = "<domain>/api/v1/validate-token?transactionId=$($_.transactionId)&token=$($_.token)&sourceIdentifier=IND&partnerId=$($_.partnerId)&aggregatorId=$($_.aggregatorId)"
"Method" = 'GET'
"Headers" = @{
Authorization = $basicAuthValue
token = $_.token
}
}
# the rest of your code
}
# for readability create a template URI string where the variables from the CSV are to be inserted
$url = '<domain>/api/v1/validate-token?transactionId={0}&token={1}&sourceIdentifier=IND&partnerId={2}&aggregatorId={3}'
Import-Csv -Path '<path\to\the\file.csv>' | ForEach-Object {
$Params = @{
"URI" = $url -f $_.transactionId, $_.token, $_.partnerId, $_.aggregatorId
"Method" = 'GET'
"Headers" = @{
Authorization = $basicAuthValue
token = $_.token
}
}
# the rest of your code
}