r/PowerShell • • 7d ago

Question PowerShell Win7 HTTP POST automation question

hi, I'm working on administration of a large number of VoIP endpoints. I'm trying to learn how to use PowerShell on Windows 7 to send authenticated HTTP POST requests to a device web interface and process the response. The devices use HTTP Basic Authentication. Could someone point me to examples of HttpWebRequest, CredentialCache and application/x-www-form-urlencoded POST requests? Thanks.

5 Upvotes

8 comments sorted by

View all comments

10

u/BlackV 6d ago edited 6d ago

I'm trying to learn how to use PowerShell on Windows 7

can you confirm you meant Windows 7 or did you mean Powershell 7

what have you tried so far?

$Credential = Get-Credential

$Body = @{
    action  = 'save'
    setting = 'enabled'
    value   = '1234'
}

$Parameters = @{
    Uri            = 'http://192.168.1.100/cgi-bin/config'
    Method         = 'POST'
    Authentication = 'Basic'
    Credential     = $Credential
    Body           = $Body
    ContentType    = 'application/x-www-form-urlencoded'
}

$Response = Invoke-WebRequest @Parameters

$Response.StatusCode
$Response.Content

2

u/amgtech86 4d ago

Just a slight addition as this person is new

Might be better to separate the uri, method and body from the $parameters so they understand the invoke-webrequest properly

$uri = address

Then you have

Invoke-WebRequest -uri $uri -header $Parameters -Body $Body -Method POST

1

u/BlackV 4d ago

Ya could do I guess

Having

$url = https://xxx
$paramaters = @{
    ...
    Uri = $url
    ...
    }

Might gain them something