I have inherited the following Python script:
import urllib2
a = urllib2.urlopen('http://mysite/mypage.aspx?action=dosomething')
a.read()
a.close()
and I would like to replace it with a powershell script. I have googled a little but everything I find launches a browser window.
This script is going to be scheduled, so I'd like to just "post and forget" if possible?
Any help very gratefully received :)
Here is a script I wrote a while back that shows how to post tweets with Geotags. This uses WebClient.
http://www.ravichaganti.com/blog/?p=979
Pasting the code here for easy reference.
Function
urlopen
looks like HTTP GET. Then you can useWebClient
:For HTTP POST I use this function:
Starting with PowerShell v3, you can use
Invoke-WebRequest
or its aliasiwr
.If you need the output the same as the Python script, use the
Content
property of the response.As @stej wrote, the Python code looks like a GET, but if you do need POST you can use the
Method
parameter.You can use the
Webclient
class from .NET, especiallyUploadString
orUploadValues
. Do aNew-Object System.Net.WebClient
to get a webclient object, and the rest should be straightforward.