#126341 - 2004-09-09 02:10 PM
Problem with shell command
|
Louie
Fresh Scripter
Registered: 2004-09-09
Posts: 12
|
I'm looking for some internal information as to how the shell command operates. The following code snippet doesn't work (services.tmp isn't created), and the command works fine from the command line.
shell '%COMSPEC% /C net start > services.tmp'
I've seen that this works correctly for nearly all of the Windows XP systems in our enterprise, but is failing on a third (11K) of our Windows NT systems. Ultimately, I'm looking for a non-WMI way to determine whether or not a given service is running, and sending the output from net start to a file and then parsing it is the best thing I've found so far.
@ERROR ends up being set to 255 for the above command. @ERROR is 0 when I remove everything from the > to the target filename. Other commands seem to work, but I can't get their output since I can't seem to get redirection working.
I'm wondering if my problem is related to some configuration setting on the problem systems. Does anyone know which API call is being made by the shell command and what parameters are used in the call? COMSPEC is set correctly, and it doesn't matter if I hard-code in the full path to CMD.EXE. I'm using version 4.22 (latest) of KiXtart.
|
|
Top
|
|
|
|
#126342 - 2004-09-09 02:34 PM
Re: Problem with shell command
|
Kdyer
KiX Supporter
   
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
Louie,
Have a look at our UDF Library and grab a copy of the WSHPIPE() UDF.
BTW, Welcome to the board!
Kent
|
|
Top
|
|
|
|
#126345 - 2004-09-09 02:47 PM
Re: Problem with shell command
|
Shawn
Administrator
   
Registered: 1999-08-13
Posts: 8611
|
Might try reformulating the command to fix the problem, and even avoid having to read-back-in and parse the output file, this trick will search for a running service and return @ERROR = 0 if true ...
Code:
break on
shell '%COMSPEC% /C net start | find /i "Event Log" >nul 2>nul'
if @error = 0
?"Event log is running"
else
?"Event log is NOT running"
endif
exit 0
-Shawn
|
|
Top
|
|
|
|
#126347 - 2004-09-09 03:02 PM
Re: Problem with shell command
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
There was a spate of problems recently with embedded spaces or some-such in the %COMSPEC% value.
Try this: Code:
shell '"'+%COMSPEC%+'" /C net start >services.tmp 2>services.tmp'
|
|
Top
|
|
|
|
#126348 - 2004-09-09 03:34 PM
Re: Problem with shell command
|
Louie
Fresh Scripter
Registered: 2004-09-09
Posts: 12
|
Thanks for all of the quick replies! I had no idea this board was so active.
COMSPEC on the system I'm working with to debug this problem is: C:\WINNT\system32\cmd.exe
Nothing weird with COMSPEC, I think, and I've also tried hard-coding the whole thing, which hasn't made a difference.
I want to avoid trying things like WSHPIPE(), since I can't be certain the target systems have WSH, WMI, etc. installed. I really want to restrict myself to just using the net start command, unless there's some COM-ish way I can do this that doesn't involve any other dependencies.
Also, xnet.exe isn't available to the target systems, but I would run into the same problem if I used it anyway. Redirection here appears to be the problem and not the command executed.
Shawn - good suggestion on piping to find. I just tried it and it's also returning an @ERROR of 255. This happens regardless of what value I search for (even if it is in the list of running services). "Error Log" = 255 and "Error Logasdfjdf" also returns 255.
Also, in case it matters, I've also tried:
%COMSPEC% /C net start > services.tmp 2>&1
(added redirection of STDERR). This didn't do anything different for me, either. It almost seems like it just doesn't like > or | characters, but they really are required for what I'm looking to do.
Any other thoughts? I'm not looking to shoot good suggestions down, but I've tried eveything I can think of and this *SHOULD* work.
|
|
Top
|
|
|
|
#126349 - 2004-09-09 03:40 PM
Re: Problem with shell command
|
Louie
Fresh Scripter
Registered: 2004-09-09
Posts: 12
|
Also, in case it's helpful, here's the output from a set command on the system I'm working on:
COMPUTERNAME=TEST9837545 ComSpec=C:\WINNT\system32\cmd.exe NUMBER_OF_PROCESSORS=1 OS=Windows_NT Os2LibPath=C:\WINNT\system32\os2\dll; Path=C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\WBEM PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH PROCESSOR_ARCHITECTURE=x86 PROCESSOR_IDENTIFIER=x86 Family 6 Model 5 Stepping 2, GenuineIntel PROCESSOR_LEVEL=6 PROCESSOR_REVISION=0502 PROMPT=$P$G SystemDrive=C: SystemRoot=C:\WINNT USERPROFILE=C:\WINNT\Profiles\Default User windir=C:\WINNT
I'm getting to it (right now) using PsExec, hence the Default User stuff above.
|
|
Top
|
|
|
|
#126350 - 2004-09-09 03:44 PM
Re: Problem with shell command
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Have you tried using %COMSPEC% with anything else?
Try just a simple: Code:
Shell %COMSPEC%+' /C echo "%%COMSPEC%%" is a valid path!'
|
|
Top
|
|
|
|
#126353 - 2004-09-09 03:48 PM
Re: Problem with shell command
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
When you test the command by typing it on the command line, do you include the command interpreter?
I.e., do you type:
Quote:
c:\> %COMSPEC% /C net start ...
|
|
Top
|
|
|
|
#126355 - 2004-09-09 04:26 PM
Re: Problem with shell command
|
Louie
Fresh Scripter
Registered: 2004-09-09
Posts: 12
|
I tried it on both my system (Windows XP) and the test one and it ran fine on mine: "C:\WINDOWS\system32\cmd.exe" is a valid path!
I just got an @ERROR of 255 and no output when trying the same code on the test system.
I guess that's why I'm asking about the internals of the shell command, but maybe this is related to CMD.EXE? I looked through autoexec.nt and config.nt on both my system and the test one and found no differences. These two files may only affect things executing under COMMAND.COM, though.
|
|
Top
|
|
|
|
#126356 - 2004-09-09 04:28 PM
Re: Problem with shell command
|
Louie
Fresh Scripter
Registered: 2004-09-09
Posts: 12
|
Quote:
Dumb question but is there anything at all different about cmd.exe and it's properties on these good wkstns versus bad ones ? It has to be something related to cmd.exe, by virtue of | and > not working ...
That's what I'm wondering. I haven't tried comparing CMD.EXE file sizes or anything, but maybe I should. I'll go take a peek at the 11K systems and comment on this soon.
|
|
Top
|
|
|
|
#126357 - 2004-09-09 04:32 PM
Re: Problem with shell command
|
Shawn
Administrator
   
Registered: 1999-08-13
Posts: 8611
|
In terms of internals, I can only guess that Ruud (developer) is using either the native win32 CreateProcess api, details here:
CreateProcess
or maybe even the native C runtime library function called system(), (which would probably just call CreateProcess anyways), reading the info (quicky) yielded nothing obvious (to me anyways), give a read.
-Shawn
|
|
Top
|
|
|
|
#126358 - 2004-09-09 04:43 PM
Re: Problem with shell command
|
Louie
Fresh Scripter
Registered: 2004-09-09
Posts: 12
|
Quote:
Jooel has a good point there. You should never assume that the current dir is suitable to write the file to. Try using a complete path where you know that write rights exist. shell '%COMSPEC% /C net start > "%temp%\services.tmp"'
I tried your suggestion, but it didn't make a difference.
|
|
Top
|
|
|
|
#126359 - 2004-09-09 04:54 PM
Re: Problem with shell command
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Quote:
I tried it on both my system (Windows XP) and the test one and it ran fine on mine: "C:\WINDOWS\system32\cmd.exe" is a valid path!
I just got an @ERROR of 255 and no output when trying the same code on the test system.
I guess that's why I'm asking about the internals of the shell command, but maybe this is related to CMD.EXE? I looked through autoexec.nt and config.nt on both my system and the test one and found no differences. These two files may only affect things executing under COMMAND.COM, though.
Urk! That's truly bizarre
Sorry for asking again, but are you sure that "%COMSPEC%" on the failing system points to a valid exe?
I have seen instances where %COMSPEC% is set to an expected (and apparently valid) value, but the actual EXE is not at the location.
Try typing at the command prompt:
Code:
%COMSPEC% /C echo OK on the failing machine.
|
|
Top
|
|
|
|
#126360 - 2004-09-09 05:00 PM
Re: Problem with shell command
|
Louie
Fresh Scripter
Registered: 2004-09-09
Posts: 12
|
Quote:
Quote:
I tried it on both my system (Windows XP) and the test one and it ran fine on mine: "C:\WINDOWS\system32\cmd.exe" is a valid path!
I just got an @ERROR of 255 and no output when trying the same code on the test system.
I guess that's why I'm asking about the internals of the shell command, but maybe this is related to CMD.EXE? I looked through autoexec.nt and config.nt on both my system and the test one and found no differences. These two files may only affect things executing under COMMAND.COM, though.
Urk! That's truly bizarre 
Sorry for asking again, but are you sure that "%COMSPEC%" on the failing system points to a valid exe?
I have seen instances where %COMSPEC% is set to an expected (and apparently valid) value, but the actual EXE is not at the location.
Try typing at the command prompt: Code:
%COMSPEC% /C echo OK on the failing machine.
Welcome to my world 
I just ran what you asked me to and got...
Code:
Microsoft(R) Windows NT(TM) (C) Copyright 1985-1996 Microsoft Corp.
C:\WINNT\system32>%COMSPEC% /C echo OK OK
|
|
Top
|
|
|
|
Moderator: Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart
|
1 registered
(Allen)
and 1198 anonymous users online.
|
|
|