Author Posts

October 16, 2013 at 11:26 pm


I have found a script online which allows me to post to a http page via powershell. The problem i have is that although it shows no errors and seems to get responses back ok, i never actually see any posted data. Anyone got any ideas?

### Post the results to a web page

$webRequest = [System.Net.WebRequest]::Create($url)
$webRequest.ContentType = "text/html"
$PostStr = [System.Text.Encoding]::UTF8.GetBytes($detailstoPOST)
write-host $poststr
$webrequest.ContentLength = $PostStr.Length
$webRequest.ServicePoint.Expect100Continue = $false

$webRequest.Method = "POST"
$requestStream = $webRequest.GetRequestStream()
$requestStream.Write($PostStr, 0,$PostStr.length)

[System.Net.WebResponse]$resp = $webRequest.GetResponse();
$rs = $resp.GetResponseStream();
[System.IO.StreamReader]$sr = New-Object System.IO.StreamReader -argumentList $rs;
[string]$results = $sr.ReadToEnd();

$HttpWebResponse = $WebRequest.GetResponse();
write-host "The HTTP response code is"
Write-Host -Object $HttpWebResponse.StatusCode.value__;

October 17, 2013 at 6:21 am

Without knowing what the Web page is doing on the other end, it's impossible to say. It could be looking for a cookie you haven't passed, it might not be intended to return any data, it might be checking referrer strings, it could be a million things.

October 18, 2013 at 12:08 am

Apologies. That was a bit of a non post wasnt it.

At present im just testing so it's a quick and dirty little ASP page:

< % response.write(request("mac")) response.write(" " & request("tp")) tp = request("tp") Dim fso Dim tst Set fso = Server.CreateObject("Scripting.FileSystemObject") Set tst = fso.OpenTextFile("c:\inetpub\wwwroot\text\reading.txt", 2, true) tst.writeline Request.Form("mac") tst.writeline Request.Form("tp") tst.close Set tst = Nothing Set fso = Nothing %>
This is a test of my < %=tp%> variable


Im posting the following to the form, but no matter what i put in there, it does not show up in the form

$detailstoPOST = "nic=Backup&mac=00:50:56:A4:4B:C4&ip= fe80::e5a2:7720:a830:abed&tp=8Eh<2)

October 18, 2013 at 6:57 am

So, when you do a POST, the form fields go into the HTTP request, but they have to be formatted in a specific way. The server decodes that and, in the case of ASP, give you the Request object. My guess is that the information isn't going into the right format or spot, so the server doesn't "see" it. Manually constructing a set of POST headers is a little outside my expertise, though.

So, you're not technically "posting the following to the form." You're posting it to the server. The server doesn't "insert the values into the form fields;" your actual HTML form tags aren't being used by your PowerShell code.

This would be a lot easier with a GET. You just call your Web page with URL parameters: mywebpage.asp?nic=xxx&mac=abc123. Then you don't have to construct POST headers and whatnot. But it still doesn't "fill in the form fields;" it's just sending the values AS IF the form had been manually filled out.

October 18, 2013 at 7:10 am

Thanks for getting back to me. So how would i do the same with a get request then?

October 18, 2013 at 7:14 am

You put the data into the URL, as I suggested.

Invoke-WebRequest -Uri http://myserver/mypage.asp?nic=xxxxx&mac=abcdef1234567

(Invoke-WebRequest is in PowerShell v3 and later; it's a lot easier than using the WebRequest .NET class directly, but you're welcome to use the class instead of the cmdlet if you prefer.)

GET just involves putting your data into the URL, as I've shown here, rather than building a set of POST headers.

October 21, 2013 at 1:26 am


Thanks for getting back to me. Im afraid that my last post was very lazy and a really easy google so apologies if i wasted your time at all.

I used (New-Object System.Net.WebClient).DownloadString("") in the end since im needing to support Powershell 2 as well.

I guess the problem that im trying to get around is that i was hoping to post to ssl thereby encrypting the data. If it's in the URL string then the thats not going to work so well.

This does however kind of of prove that the process is achievable. All be it without the desired end result 🙂