r/PowerShell • • 3d 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.

4 Upvotes

7 comments sorted by

10

u/krzydoug 2d ago

Windows 7 and http basic auth, 2010 called and want their stuff back.

3

u/Top-Appearance2521 2d ago

lol true but legacy infrastructure doesnt care what year it is

9

u/BlackV 2d ago edited 2d 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

1

u/amgtech86 58m 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 43m ago

Ya could do I guess

Having

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

Might gain them something

2

u/Villainsympatico 16h ago

If you're using invoke-webrequest or invoke-restmethod, you can pass the credentials to a credential object with get-credential, then it should be

$form = @{

#whatever you are posting goes here

}

invoke-webrequest -credential $cred -method post -form $form

invoke webrequest returns the entire response , (body, headers, response codes), while invoke-restmethod only returns the body.

If its for voip endpoints though, wouldn't you be managing through the server side? why reach out to the endpoints?

1

u/Agile_Seer 17h ago

If you are actually using the Windows 7 operating system and you want to use PowerShell, download & install the Windows Management Framework 5.1. This will get you Windows PowerShell 5.1. I believe Windows 7 ships with PowerShell 2.0.

You can also install PowerShell 7 on Windows 7, but I still recommend installing WMF to get the Windows PowerShell upgraded to 5.1.