Solved

Script to Test a Web Service and Returns the Response on a Message

Posted on 2011-02-16
6
247 Views
Last Modified: 2012-06-22
Hi,
I´m working on a script to consult a web service, execute a method and returns the response.
It is working fine and I´m loading it via WMI to the remote server. It writes the response on the event log of the server. But now I need that it returns the response on a message and that if the method is not available, returns an exit with an error code. Like "Wscript.Quit(4)"
How can I do that?
Thanks
This is the script:
 
Option Explicit
class WebService
  public Url
  public Method
  public Response
  public Parameters
 
 'Function to invoke the Web Service
  public function execute()
    dim xmlhttp
    Set xmlhttp = CreateObject("Microsoft.XMLHTTP")
    xmlhttp.open "POST", Url & "/" & Method, false
    xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"	
    xmlhttp.send Parameters.toString
    response = xmlhttp.responseText
    set xmlhttp = nothing
  end function
  
  Private Sub Class_Initialize()
    Set Parameters = new wsParameters
  End Sub
  
  Private Sub Class_Terminate()
    Set Parameters = Nothing
  End Sub
End class
 
'**************************************************** 
' Class wsParameters
'****************************************************

class wsParameters
  public mCol
  public function toString()
    dim nItem
    dim buffer
    buffer = ""
    for nItem = 1 to Count
      buffer = buffer & Item(nItem).toString & "&"
    next
    if right(buffer,1)="&" then
      buffer = left(buffer,len(buffer)-1)
    end if
    toString = buffer 
  end function
  public sub Clear
    set mcol = nothing 
    Set mCol = CreateObject("Scripting.Dictionary") 
  end sub
  public sub Add(pKey,pValue)
    dim newParameter
  
    set newParameter = new wsParameter
    newParameter.Key = pKey
    newParameter.Value = pValue
    mCol.Add mCol.count+1, newParameter
  
    set newParameter = nothing
  end sub
  public function Item(nKey)
    set Item=mCol.Item(nKey)
  end function
  public function ExistsXKey(pKey)
    dim nItem
  
    for nItem = 1 to mcol.count
      if mCol.Item(nItem).key = pKey then
        ExistsXKeyword = true
        exit for
      end if
    next
  end function
  public sub Remove(nKey)
    mCol.Remove(nKey)
  end sub
  public function Count()
    Count=mCol.count
  end function
  Private Sub Class_Initialize()
    Set mCol = CreateObject("Scripting.Dictionary")
  End Sub
  Private Sub Class_Terminate()
    Set mCol = Nothing
  End Sub
end class
 
'**************************************************** 
' Class wsParameter
'****************************************************
 
class wsParameter
   public Key
   public Value
   public function toString()
     toString = Key & "=" & Value
   end function
end class

dim ws, objShell
 
'Creates the object of the Web Service 
set ws = new webservice
ws.Url = "http://xxxxxxxxxxxxxxxx.asmx"
'Calls the method
ws.Method = "xxxxx"
'Fill consult parameters
ws.Parameters.Add "login", "1234390p"
ws.Parameters.Add "password","temp"
 
ws.execute

Const EVENT_SUCCESS = 0
Set objShell = Wscript.CreateObject("Wscript.Shell")
objShell.LogEvent EVENT_SUCCESS, FormatDateTime(Now) & " - WS is OK " & ws.Response
set objShell = Nothing
set ws = Nothing
WScript.Quit

Open in new window

0
Comment
Question by:andressk
  • 3
  • 3
6 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 34913065
Hi, maybe change this section:

 
dim ws, objShell
 
'Creates the object of the Web Service 
set ws = new webservice
ws.Url = "http://xxxxxxxxxxxxxxxx.asmx"
'Calls the method
ws.Method = "xxxxx"
'Fill consult parameters
ws.Parameters.Add "login", "1234390p"
ws.Parameters.Add "password","temp"
 
ws.execute

Const EVENT_SUCCESS = 0
Set objShell = Wscript.CreateObject("Wscript.Shell")
objShell.LogEvent EVENT_SUCCESS, FormatDateTime(Now) & " - WS is OK " & ws.Response
set objShell = Nothing
set ws = Nothing
WScript.Quit

Open in new window


to this:

 
On Error Resume Next
dim ws, objShell
 
'Creates the object of the Web Service 
set ws = new webservice
ws.Url = "http://xxxxxxxxxxxxxxxx.asmx"
'Calls the method
ws.Method = "xxxxx"
'Fill consult parameters
ws.Parameters.Add "login", "1234390p"
ws.Parameters.Add "password","temp"
 
ws.execute

If Err.Number = 0 Then

	Const EVENT_SUCCESS = 0
	Set objShell = Wscript.CreateObject("Wscript.Shell")
	objShell.LogEvent EVENT_SUCCESS, FormatDateTime(Now) & " - WS is OK " & ws.Response
	set objShell = Nothing
	set ws = Nothing
	WScript.Quit

Else

	Const EVENT_ERROR = 1
	Set objShell = Wscript.CreateObject("Wscript.Shell")
	objShell.LogEvent EVENT_ERROR, FormatDateTime(Now) & " - WS Error " & Err.Number & ": " & Err.Description
	set objShell = Nothing
	set ws = Nothing
	WScript.Quit(4)

End If

Open in new window


Regards,

Rob.
0
 

Author Comment

by:andressk
ID: 34918504
Hi Rob,

Thanks for your reply, I´ve tested the script and it works fine, it shows the error as expected. One last question is how can I add the ws.Response as a message to show the WS response without checking the windows log?

Regards
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34920240
If you want to show the message to screen, then under this:
      objShell.LogEvent EVENT_ERROR, FormatDateTime(Now) & " - WS Error " & Err.Number & ": " & Err.Description

you can add this:
      WScript.Echo FormatDateTime(Now) & " - WS Error " & Err.Number & ": " & Err.Description

But if you run the script as a scheduled task, it won't show up....it only works if you run it manually.

Regards,

Rob.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:andressk
ID: 34920631
Hi Rob,

I´ve made the suggested change but I can´t get the message with the Response, not as a window that shows up, but something like:
Wscript.Echo "Message: Response " & FormatDateTime(Now) & ws.Response
Thanks
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 34921149
Sorry, I'm confused.  The WScript.Echo statement will show a message box on the screen.  Where do you actually want the message to be displayed?

If you *do* want it as a message box on screen, then this will show a message whether it's OK or not.

Regards,

Rob.
On Error Resume Next
dim ws, objShell
 
'Creates the object of the Web Service 
set ws = new webservice
ws.Url = "http://xxxxxxxxxxxxxxxx.asmx"
'Calls the method
ws.Method = "xxxxx"
'Fill consult parameters
ws.Parameters.Add "login", "1234390p"
ws.Parameters.Add "password","temp"
 
ws.execute

If Err.Number = 0 Then

	Const EVENT_SUCCESS = 0
	Set objShell = Wscript.CreateObject("Wscript.Shell")
	objShell.LogEvent EVENT_SUCCESS, FormatDateTime(Now) & " - WS is OK " & ws.Response
	WScript.Echo FormatDateTime(Now) & " - WS is OK " & ws.Response
	set objShell = Nothing
	set ws = Nothing
	WScript.Quit

Else

	Const EVENT_ERROR = 1
	Set objShell = Wscript.CreateObject("Wscript.Shell")
	objShell.LogEvent EVENT_ERROR, FormatDateTime(Now) & " - WS Error " & Err.Number & ": " & Err.Description
	WScript.Echo FormatDateTime(Now) & " - WS Error " & Err.Number & ": " & Err.Description
	set objShell = Nothing
	set ws = Nothing
	WScript.Quit(4)

End If

Open in new window

0
 

Author Closing Comment

by:andressk
ID: 34922660
Thanks for your help
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now