Re: VBScript with Sessionid and RDP
- From: "Pegasus [MVP]" <news@xxxxxxxxxxxxx>
- Date: Tue, 12 May 2009 17:37:45 +0200
Sorry, can't tell without sitting in front of you machine. You need to
execute the following commands at a Command Prompt in order to examine its
output:
set task=MyTask.exe
tasklist /v /fo csv /fi "imagename eq %task%
If you get no output then there you must run this command so that you can
tell the correct image name:
tasklist /v /fo List | more
"Big Passeron" <BigPasseron@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:72A29975-B448-4938-BFE9-51B3EC533F9D@xxxxxxxxxxxxxxxx
Pegasus,
your suggestions made me go a little further.
Now, if 2 instances are running (one in console and the other one in a
Remote Desktop Session), only RD Session instance is killed.
If only console instance is running nothing happens as I want it to be.
The problem occurs if I have no instances running: in this case a new
instance is thrown (that's ok), but process is execute inside a Remote
Desktop session.
I don't know if this behavior is caused by scheduled task be executed as
user Administrator, while user logged inside Console session is a Standard
User (for security purposes).
To make a long story short, I've made some little modifications to your
script (among the other things, I want the process to be launched with
StandardUser credentials, should an instance not be running), but it still
doesn't work as expected. The code follows:
"[1]@echo on
[2]SetLocal EnableDelayedExpansion
[3]set active=no
[4]set task=MyTask.exe
[5]for /F "tokens=1-3 delims=," %%a in ('tasklist /v /fo csv /fi
"imagename
eq %task%"') do (
[6]for /F %%i in (%%b) do set pid=%%~i
[7]if /i [%%c]==[""] (set active=yes) else (taskkill /f /pid !pid!)
[8)
[9]if %active%==no Runas /user:MyServer\StandardUser /savecred
"C:\MyFolder\%task%"
"
Thanks for your help
"Pegasus [MVP]" wrote:
There were several problems with your version of the script:
- Most software houses avoid issuing executables with embedded spaces in
the
name, because of known issues with simple scripts. Yours uses embedded
spaces, which causes the batch file to fail.
- You added a set of quotes in line [3]. Sorry, can't do this.
- You merged line [6] in my original code with line [5]. Sorry, can't do
this.
- I used the /pid switch for taskkill. You changed it to /im. This will
cause taskkill to fail.
Here is a modified version that can cope with executables that have
embedded
spaces in the name.
[01] @echo off
[02] SetLocal EnableDelayedExpansion
[03] set active=no
[04] set task=My Software 1.00.exe
[05] for /F "tokens=1-3 delims=," %%a in ('tasklist /v /fo csv ^| find /i
"%task%"') do (
[06] for /F %%i in (%%b) do set pid=%%~i
[07] if /i [%%c]==[Console] (set active=yes) else (echo taskkill /f
/pid
!pid!)
[08] )
[09] if %active%==no echo "C:\My Folder\%task%"
"Big Passeron" <BigPasseron@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:AC285389-B482-471F-B573-A08FF5417061@xxxxxxxxxxxxxxxx
Ok, a few things. This is my script right now:
[1]@ECHO on
[2]set active=no
[3](set task='My Software 1.00.exe')
[4]for /F "tokens=1-3" %%a in ('tasklist /v /fo table ^| find /i
"%task%"')
do (
[5]if /i [%%c]==[Console] (set active=yes) else ( taskkill /f /im
%%b ))
[6]if %active%==no "C:\My Folder\%task%"
A problem is with task's name that's composed by more than one word and
from
uncertainty with single and double quotes.
As a consequence, this is the output given by the command:
C:\My Folder\>set active=no
C:\My Folder\>(set task='My Software 1.00.exe' )
C:\My Folder\>for /F "tokens=1-3" %a in ('tasklist /v /fo table | find
/i
"'My Software 1.00.exe'"') do (if /I [%c] == [Console] (set
active=yes )
else (taskkill /f /im %b ) )
C:\My Folder\>if no == no "C:\My Folder\'My Software 1.00.exe'"
I guess the problem is with the single quote before my tasks's name. I
also
tried with double quotes but the script fails to execute.
"Pegasus [MVP]" wrote:
By numbering my lines, I made it absolutely clear where each line
starts
and
ends. You did not add any line numbers, hence I cannot tell if you
unwrapped
the lines correctly. I do not even know which line raises the error
message - how about starting the batch file with "echo on"?
"Big Passeron" <BigPasseron@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message
news:5DDF9E04-D175-4050-B62D-38D5503F9F33@xxxxxxxxxxxxxxxx
Hi Pegasus,
I didn't know taskkill until now.
Abyway, I've followed your suggestion, removed echo from line 5 and
7
but
I
get a "The syntax of the command is incorrect"
The code follows:
@ECHO off
set active=no
set task="MySoftware.exe"
for /F "tokens=1-3" %%a in ('tasklist /v /fo table ^| find /i
"%task%"')
do (
if /i [%%c]==[Console] (set active=yes) else (taskkill /f /pid %%b )
)
if %active%==no "C:\MyFolder\%task%"
"Pegasus [MVP]" wrote:
"Big Passeron" <BigPasseron@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message
news:A17A3778-0B0D-4289-B1C1-622A7C54FDA0@xxxxxxxxxxxxxxxx
Hi Al,
I've tried to take advantage of variable "SESSIONNAME" inside my
script
but
I couldn't manage to do it.
Furthermore, I'm everything but a programmer.
Eventually this (simple?) script would save me a lot of
headache,
so
I'm
asking for futher assistance.
Can someone point me to the right direction with more
straightforward
suggestions?
Thanks.
WMI is a wonderful thing. However, when someone has already done
most
of
the
work and delivered a suitable tool then it would make sense to use
that
tool. Tasklist.exe / Taskkill.exe are such tools. Tasklist tells
you
unambiguously whether a process is run from the console or from an
RDP
session and it also gives you a process-ID that you can feed into
taskkill.exe. Try the following batch file:
[1] @ECHO off
[2] set active=no
[3] set task=MySoftware.exe
[4] for /F "tokens=1-3" %%a in ('tasklist /v /fo table ^| find /i
"%task%"')
do (
[5] if /i [%%c]==[Console] (set active=yes) else (echo taskkill
/f
/pid
%%b)
[6] )
[7] if %active%==no echo "c:\Program Files\Passeron\%task%"
To activate the batch file, remove the word "echo" from lines [5]
and
[7].
Post again if you want to know what each line of code does. Make
sure
to
unwrap wrapped lines properly!
.
- Follow-Ups:
- Re: VBScript with Sessionid and RDP
- From: Big Passeron
- Re: VBScript with Sessionid and RDP
- References:
- Re: VBScript with Sessionid and RDP
- From: Big Passeron
- Re: VBScript with Sessionid and RDP
- From: Pegasus [MVP]
- Re: VBScript with Sessionid and RDP
- From: Big Passeron
- Re: VBScript with Sessionid and RDP
- From: Pegasus [MVP]
- Re: VBScript with Sessionid and RDP
- From: Big Passeron
- Re: VBScript with Sessionid and RDP
- From: Pegasus [MVP]
- Re: VBScript with Sessionid and RDP
- From: Big Passeron
- Re: VBScript with Sessionid and RDP
- Prev by Date: Re: can VBScript determine the active tab of a dialog?
- Next by Date: Re: can VBScript determine the active tab of a dialog?
- Previous by thread: Re: VBScript with Sessionid and RDP
- Next by thread: Re: VBScript with Sessionid and RDP
- Index(es):
Relevant Pages
|