【问题标题】:Invoke-RestMethod from multiple URLs from text file从文本文件中的多个 URL 调用 RestMethod
【发布时间】:2020-07-08 22:40:47
【问题描述】:

需要通过文本文件中的几个链接使用 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

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

【问题讨论】:

    标签: powershell invoke-restmethod


    【解决方案1】:

    假设您想为每个 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 将执行您为文件中的每一行提供的代码块中的任何内容,将 $_ 替换为该行。

    【讨论】:

    • 非常感谢!这很完美!抱歉可以投票没有足够的代表:)
    猜你喜欢
    • 2021-03-29
    • 2017-02-04
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 2010-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多