Solved

VBS Script to click a button in a Terminal Session

Posted on 2015-01-24
22
135 Views
Last Modified: 2015-02-22
I've written a VBS script which automatically loads a script-defined secure webpage (https://)  in IE and submits a user's log-in credentials.

This causes a second webpage to load in a second tab (which becomes the active tab) in IE.

I know the name of the second page (let's say it's URL = "http://domain.com/secondpage")

My script then clicks on a link on this second page.  This causes the webpage to start a Terminal Session on a remote computer (my work computer).

My work computer (or terminal server? ... I'm out of my depth here) open a new page? with an OK button (this page no longer appears to be within a browser; I assume it is in a terminal window; for example, if minimized, this window shows up on the taskbar as a separate 'app' window with my work computer's ID such as 'CorpPC1234').

I would like to add an action to my existing script (or have my script call another script) to click the OK button on the page the terminal session served (this is part of a secondary log-in sequence).  If I can at least get past this OK button, I can then continue to automate the next log-in steps on subsequent terminal pages.

My problem is that unlike when I'm in the browser where I can target elements using for example the .getElementbyID function, once I'm in the Terminal Session I no longer know how to inspect the page or it's elements or tags or objects.

How can I go about this?
0
Comment
Question by:qeng
  • 13
  • 7
22 Comments
 

Author Comment

by:qeng
ID: 40568783
The first and second webpages my script navigates through are served by Junos Pulse Secure Access Service.  This is the service which launches the Terminal Session referred to above.
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40569253
The only way to control a Remote Desktop session is by simulating keyboard and mouse input with SendKeys. That shouldn't be an issue much, but requires some fiddeling because of the timing. I've not done that yet for that reason, being in a similar situation (I used PowerShell to do the VPN login stuff).
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40569262
Above is using Junipert Network Connect, the other technology used by Juniper very simular to Pulse. With JunOS Pulse we use the full client to get a networked connection, which again allows us to use direct RDP we can supply credentials aso. much better.
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

Author Comment

by:qeng
ID: 40569665
Qlemo,

Thanks for the input.  I'm not sure how to use the advice though short of looking into SendKeys (which I'm not familiar with).

Can my initial VBS login script be appended to send a keystroke command (if I put a suitable delay in) which would be seen by the Terminal Session?

If so, what would the VBS commands be to trigger this send?
0
 

Author Comment

by:qeng
ID: 40572325
The last few pieces I need to add to my script are:

add a delay
send a return keystroke
add a delay
send a return keystroke
send a password
send a return keystroke
0
 

Author Comment

by:qeng
ID: 40572402
Qlemo, my apologies.  When you mentioned SendKeys, I thought it was a third party app to 'send keystrokes' and not a WshShell command, hence my comment.  I'm brand new to scripting.


I've used WshShell.SendKeys "{ENTER}" etc. along with delays where I thought I needed them.

The problem I have left is that the correct keystrokes seem to be getting sent but they are not sent into the Terminal Session my script got me logged into, they are sent to the original desktop session where the script started executing (this is likely not the correct IT terminology).

How can I target the sent keystrokes to go be applied to the Terminal Session which Junos Pulse Secure Access Service started after my script logged me in.
0
 

Author Comment

by:qeng
ID: 40577126
I'm guessing (strictly a guess) that I have to first set the object to be the terminal session window prior to sending my keystrokes (otherwise they are sent to the original object?) but I don't know how to do that.
0
 
LVL 69

Assisted Solution

by:Qlemo
Qlemo earned 500 total points
ID: 40581350
Sorry, looks like I have not received notifs for this question for your last comments.

You are correct, you need to make sure the window you want to send keystrokes to is active. You do that by calling WshShell.AppActivate "start of window title or Process ID".
But I had no luck of getting the SendKeys working against a MSTSC window - as soon as a keystroke is sent, the login dialog is cancelled.
0
 

Author Comment

by:qeng
ID: 40581424
thx Qlemo,

I'm assuming that "start of window title or Process ID" is a label which I'm to change to whatever my window title or Process ID is.  Is that correct?

If it is, how can I inspect (figure out) what the window title or Process ID is (for example by using some equivalent to a Task Manager when you want to know which tasks are active)?
0
 
LVL 69

Assisted Solution

by:Qlemo
Qlemo earned 500 total points
ID: 40581445
You should be able to spot the window title yourself. Usually the Web TS client starts with the application name, or the name/IP of the target. You only need to provide as much as is making the title unique.
Both window title and PID can be retrieved with the native tool tasklist /v if you know the image name. In VBS you usually run WMI queries (see e.g. http://windowsitpro.com/systems-management/q-how-can-i-check-process-using-vbscript) to get process info.

But again, I don't think it is worth the effort, because it won't work most likely.
0
 

Author Comment

by:qeng
ID: 40581467
Using Windows Task Manager, I can see that under the Applications Tab, it lists the active Tasks.

     -  The Task i'm interested in targeting to send my keystrokes is called (for example):  ABCD123456

Also using Windows Task Manager, I can see under the Processes Tab, that it lists the Image Name.

     -  The Image Name I'm interested in targeting to send my keystrokes is called (for example):  dsTermServ.exe

Do I use the WshShell.AppActivate command in my VBScript with the Task Name, or the Image Name or neither?
0
 
LVL 69

Assisted Solution

by:Qlemo
Qlemo earned 500 total points
ID: 40581490
The "task" name (which is the window title).
You could use the image name with taskklist /im dsTermServ.exe or WMI to get the PID, but I guess the window title is easier to use.
0
 

Author Comment

by:qeng
ID: 40585959
Thx.  I will try this and get back.
0
 

Author Comment

by:qeng
ID: 40602872
I have not had a chance to get back to this issue.  My problem is still trying to target the terminal session with the keystrokes.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40603629
Eventually I had the opportunity to test with JunOS Pulse dsTermServ, and as expected no dice. The process ignores SendKey, at least for login stuff.
The only way able to perform automted actions in the terminal session is (probably) by running an application remote. That would, however, require you to have access outside of the terminal session - unlikely.

On another note, why can't you just execute whatever you need on the remote system, e.g. as scheduled task?
0
 

Author Comment

by:qeng
ID: 40610006
Qlemo,

Thanks for checking this.

I'm not sure what you mean by 'why can't you just execute whatever you need on the remote system'.

The sequence of actions I'm trying to automate is one I have to use dozens of times a day which involves having to log-on to our corporate network.  I do this from home, as well as while I'm moving around within the company (everytime I close my laptop, such as when going from one meeting to another, I have to log back in).

For now, I've been able to automate the my remote access up to the point where the remote server serves the login page which I would normally see if I were logging in to my desktop at the office.

It's at that point that I lose the ability to execute any further commands from my script, or more particularly, that I don't know how to interact with the active terminal session (my terminology).

So I'm not sure what you mean by using a scheduled task (in this context).
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40610085
I'm still not clear why you should need to dig thru several web pages and click a lot of buttons just to get access to your office. This sounds like intentional abstruction. There are better ways to get secure access. E.g. Pulse allows a network connection, and that again opens up options for remote control.

As hinted before, you are out of luck without changing the login procedure. After having logged in, options are available again to start automation.
0
 

Author Comment

by:qeng
ID: 40620905
Qlemo, thanks.  

I'll try to explain this in a subsequent post but have sent in a support question to see if this question can be continued in privacy mode.  I don't want to expose any system vulnerabilities in providing the additional information i'd like to share here.
0
 

Author Comment

by:qeng
ID: 40621011
Thanks Qlemo,

I'll upgrade to cut down on some of the exposure for some of my questions but understood as it relates to visitors and member access to the questions.

I'll close off this question and email you the specifics in case there is any additional clarity you can help me gain. I'll do that by way mostly of screen grabs which should be fairly obvious to you since you're already pretty tuned in to this topic. I'll assume unless you advise otherwise that the contents of my email to you will remain 'private' (I'll hideout the credentials and corporate identifiers).

It may well be that I can't advance my 'automation' quest any further (I certainly don't doubt your advice, it's more that I may not be explaining things as clearly as necessary to give you the right info to help me on) but my gut tells me that if I can send the required information at some step in my process via the keyboard, there must be some way that at the very least, I can have on my laptop, some service running in the background which responds to some custom keyboard shortcut, regardless of whether i'm in a terminal session or not.  Presumably, with the right background process running, the system would respond to that short cut the same way  as it responds to function keys being pressed when I'm just entering the terminal session.

Thx for your feedback.
0
 

Author Closing Comment

by:qeng
ID: 40624357
This problem doesn't appear to be straightforward.  Although I wasn't able to accomplish what I was hoping for, Qlemo provided good support and insight into the problem.  I will revisit this issue when I have more time available to troubleshoot further and have addressed potential privacy issues.
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

860 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