Shaun Cassells at MyITForum.com

SMS 2003 and ConfigMgr 2007, PowerShell, Scripting, Finance, Fitness and Fun

News

Locations of visitors to this page

Sending a HTTP post from a command prompt (scripted)

Below is a quick script to send HTTP information to a website.

This is extremly powerful for submitting information from a workstation to a SQL database.   How does it get to a database from an HTTP post?  

  1. create an *.asp page on an webserver
  2. have the *.asp page take command line parameters and pass them to a SQL insert string
    • Why do SQL on server?  Well it keeps the password and / or security rights on your website and not distributed to clients 

'==========================================================================
'
' NAME: HTTPPost.vbs
'
'
' COMMENT: Sends HTTP Post commands to a website
'
' ARGUEMENTS:  Two separated by a SPACE.
'    First arguement is the website to post To
'    Second arg is the status to post
'    Example:
'    HTTPPost.vbs "http://<WebSite>/default.asp" "Cname=MyComputer&Site=SMS"
'
'==========================================================================

On Error Resume Next
Set objShell = WScript.CreateObject("WScript.Shell")
Set objArgs = WScript.Arguments
If objArgs.Count < 2 Then
' ERROR:  Missing Arguments
 iErrorCode = 3503
 strErrorDescr = "The command contains an invalid number of arguments."
 objShell.LogEvent 1, "HTTPPostFailed with error code:" & iErrorCode & "[" & strErrorDescr & "]"
 WScript.quit iErrorCode
Else
 ' Set arguemented URL
 strURL = objArgs(0)
 ' Set argumented Request
 strRequest = objArgs(1)
 If objArgs.Count > 2 Then
  For I = 2 To objArgs.Count - 1
     strRequest = strRequest & " " & objArgs(I)
  Next
 End If
End If

HTTPPost strURL,strRequest
If Err.Number <> 0 Then
 objShell.LogEvent 1, "HTTPPostFailed with error code:" & Err.Number & "[" & Err.Description & "]"
 WScript.Quit Err.Number
End If

Set objArgs = Nothing
WScript.Quit

Function HTTPPost(sUrl, sRequest)
  set oHTTP = CreateObject("Microsoft.XMLHTTP")
  oHTTP.open "POST", sUrl,false
  oHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  oHTTP.setRequestHeader "Content-Length", Len(sRequest)
  oHTTP.send sRequest
  HTTPPost = oHTTP.responseText
End Function