52

Possible Duplicate:
Get $webclient.downloadstring to write to text file in Powershell
Powershell http post with .cer for auth

I have an SMS system that provides me the ability to send an SMS from an HTTP GET request:

http://smsserver/SNSManager/msgSend.jsp?uid&to=smartsms:*+001XXXXXX&msg="text of the message"&encoding=windows-1255

I want to enter the details to the text from PowerShell and just surf to this URL. How can I do it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
alex
  • 916
  • 4
  • 12
  • 26
  • 16
    I've nominated for reopen as this seems to be the simplest question that *just* asks about doing a `GET`; the linked 'dupes' ask about writing the result to a file, and doing a post. – AakashM Apr 10 '14 at 09:31
  • 3
    `Invoke-WebRequest -UseBasicParsing -Uri http://example.com/` – kenorb Feb 13 '18 at 12:11
  • Related: [PowerShell equivalent of curl](https://superuser.com/q/344927/87805). – kenorb Feb 13 '18 at 12:12

2 Answers2

57

In PowerShell v3, have a look at the Invoke-WebRequest and Invoke-RestMethod e.g.:

$msg = Read-Host -Prompt "Enter message"
$encmsg = [System.Web.HttpUtility]::UrlEncode($msg)
Invoke-WebRequest -Uri "http://smsserver/SNSManager/msgSend.jsp?uid&to=smartsms:*+001XXXXXX&msg=$encmsg&encoding=windows-1255"
Emil Lerch
  • 4,416
  • 5
  • 33
  • 43
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • 5
    In case you want to look at the HTTP response, the output object from `Invoke-WebRequest` leaves much to be desired, though you could output the `RawContent` property which includes the headers. Otherwise `Invoke-RestMethod` has better output. – Guss Oct 31 '14 at 12:58
24

Downloading Wget is not necessary; the .NET Framework has web client classes built in.

$wc = New-Object system.Net.WebClient;
$sms = Read-Host "Enter SMS text";
$sms = [System.Web.HttpUtility]::UrlEncode($sms);
$smsResult = $wc.downloadString("http://smsserver/SNSManager/msgSend.jsp?uid&to=smartsms:*+001XXXXXX&msg=$sms&encoding=windows-1255")
alroc
  • 27,574
  • 6
  • 51
  • 97
  • 5
    This worked for me, but I had to run `Add-Type -AssemblyName System.Web` before calling UrlEncode(). – Vimes May 22 '13 at 03:22