user3360847
user3360847

Reputation: 67

Invoke-RestMethod from multiple URLs from text file

Need help with PowerShell script with Invoke-RestMethod through several links from text file:

What I have not and works for single site:

$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

However need to do this for multiple links. Any suggestions how accomplish this?

Upvotes: 0

Views: 534

Answers (1)

Grace
Grace

Reputation: 641

Assuming you want to use the same username and password and such for each URL, try something like this:

$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 }

The ForEach-Object cmdlet will do whatever's in the code block you give it for each line in the file, replacing $_ with the line.

Upvotes: 2

Related Questions