0

通过文本文件中的几个链接需要使用 Invoke-RestMethod 的 PowerShell 脚本的帮助:

我没有并且适用于单个站点:

$username = 'username'
$password = 'password'
$accept = 'application/xml'
$uri = 'https://example.com/ping/id/31'
$headers = @{
    "Accept"=$accept
}
$secpwd = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($username, $secpwd)
Invoke-RestMethod -Method Post -Uri $uri -Headers $headers -Credential $credential

但是需要对多个链接执行此操作。任何建议如何做到这一点?

4

1 回答 1

2

假设您想为每个 URL 使用相同的用户名和密码等,请尝试以下操作:

$username = 'username'
$password = 'password'
$accept = 'application/xml'
$headers = @{
    "Accept"=$accept
}
$secpwd = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($username, $secpwd)
Get-Content .\file_with_one_url_per_line | ForEach-Object { Invoke-RestMethod -Method Post -Uri $_ -Headers $headers -Credential $credential }

ForEach-Object cmdlet 将执行您为文件中的每一行提供的代码块中的任何内容,并替换$_为该行。

于 2020-07-08T22:56:46.577 回答