Knowledge
  • Read Me
  • Programming
    • ASP.NET
      • .NET Libraries
      • ASP.NET Core
        • Helper
          • Encryption
          • CSV Helper
          • String Helper
        • Logging
          • Simple Serilog
        • Middlewares
          • IP Restrictions
          • Request Throttling
          • Request Logging
        • Console
          • Command Line with arguments
        • JSON
      • ASP.NET Framework
      • Testing
        • Resources
        • xUnit.net
      • Naming Conventions
      • REST API Guidelines
    • Database
      • SQL Style Guide
      • MSSQL
        • Installation
          • Install MSSQL on MacOS M1 (ARM64)
        • Looping
        • Table Valued Functions
        • Session State
        • SQL Cheat Sheet
        • Export Pipe Delimited CSV With cmdshell
      • Redis
        • Redis Installation on Mac OS
        • Redis Installation on Docker
    • Java
      • AWS SDK - SSM
      • mTLS HTTP Connection
      • Read Resource Files
    • Javascript
      • Javascript Libraries
    • Python
    • OpenSSL
      • One Way SSL & Two Way SSL
      • Common OpenSSL Commands
      • Create Self-Signed Certificate
    • Misc
      • Git Commands
      • Windows Commands
      • PowerShell Commands
      • Vulnerabilities Dependency Check
      • Replace Filename Command
      • JSON Web Token (JWT)
      • Rabbit MQ Message-Broker
      • Pandoc Convert Document
  • DevOps
    • What is DevOps
    • CI & CD
    • Azure DevOps
  • Tools
    • Development Tools
Powered by GitBook
On this page
  • Invoke-WebRequest
  • GET Invoke-WebRequest
  • POST Invoke-WebRequest
  • Set Powershell to skip SSL certificate checks
  • Reference

Was this helpful?

  1. Programming
  2. Misc

PowerShell Commands

PreviousWindows CommandsNextVulnerabilities Dependency Check

Last updated 3 years ago

Was this helpful?

Invoke-WebRequest

The Invoke-WebRequest cmdlet sends HTTP and HTTPS requests to a web page or web service.

GET Invoke-WebRequest

Sends a GET request and echo the response

$varUrl = "https://postman-echo.com/get"
Invoke-WebRequest -Method Get -Uri $varUrl -TimeoutSec 7200 | Select-Object -Expand Content

Sends a GET request and echo the response and

$varUrl = "https://postman-echo.com/get"

Try
{
    $response = Invoke-WebRequest -Method Get -Uri $varUrl -TimeoutSec 7200
    $response.StatusCode
    $response.Content
}
Catch
{
    $ErrorMessage = $_.Exception.Message
    Write-Output($ErrorMessage)
    $FailedItem = $_.Exception
    Write-Output($FailedItem)
    Break
}

POST Invoke-WebRequest

$varUrl = "https://postman-echo.com/post"

$params = @'
{"method": "POST"}
'@

Invoke-WebRequest -Method Post -Uri $varUrl -Body $params -ContentType "application/json" -TimeoutSec 7200 | Select-Object -Expand Content
$varUrl = "https://postman-echo.com/post"

$params = @'
{"method": "POST"}
'@

Try
{
    $response = Invoke-WebRequest -Method Post -Uri $varUrl -Body $params -ContentType "application/json" -TimeoutSec 7200
    $response.StatusCode
    $response.Content
}
Catch
{
    $ErrorMessage = $_.Exception.Message
    Write-Output($ErrorMessage)
    $FailedItem = $_.Exception
    Write-Output($FailedItem)
    Break
}

Set Powershell to skip SSL certificate checks

If you are trying to query a web site and you have invalid SSL certificates, Powershell is by default very strict on what it accepts. You will often end up with errors like:

Invoke-WebRequest : The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

For the work around, try to run the script below before sending the invoke commands. This will ignore the ssl certificate error validation in the current PowerShell Session.

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

Reference

Sends a POST request and echo the response

Sends a POST request and echo the response and

Content
Status Code
Content
Content
Status Code
Content
Microsoft Docs - Invoke-WebRequest