Page 1 of 1 1
Topic Options
#209911 - 2015-02-05 06:20 PM Read value of shell output?
Robdutoit Offline
Hey THIS is FUN
***

Registered: 2012-03-27
Posts: 363
Loc: London, England
I have this command in my kixtart startup script

 Code:
shell 'netsh advfirewall firewall add rule name="WSUS Pub Host" dir=in program="%winDir%\System32\SvcHost.exe" remoteip=localsubnet action=allow'


This adds the firewall rule beautifully. What I did not realise is that it adds a new rule every single time the computer is restarted.

I have determined that I need to run this command to check if rule exists.
 Code:
netsh advfirewall firewall show rule name="WSUS Pub Host


However, I cannot work out how to do a logic like this: I know the coding is wrong!

 Code:
if shell 'netsh advfirewall firewall show rule name="Allow WSUS Package Publisher Service Host"' = 1
? "Firewall rule does exist"
else
? "Firewall rule does not exist"
shell 'netsh advfirewall firewall add rule name="WSUS Pub Host" dir=in program="%winDir%\System32\SvcHost.exe" remoteip=localsubnet action=allow'
endif


How do I get a shell to output success or failure in the above example. Thanks dudes!

Top
#209912 - 2015-02-05 06:24 PM Re: Read value of shell output? [Re: Robdutoit]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4574
Loc: USA
WSHPipe() - Pipes the results of a shell command to an array
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=83201

How to use UDFs -
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=81943#Post81943

The rest of the UDFs are here -
http://www.kixtart.org/forums/ubbthreads.php?ubb=postlist&Board=7&page=1
_________________________
(... better days ahead)

Top
#209913 - 2015-02-05 08:09 PM Re: Read value of shell output? [Re: Allen]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4403
Loc: New Jersey
"Find is your friend"!!

Try this:
 Code:
; Rule exists
$Cmd = '%COMSPEC% /c netsh advfirewall firewall show rule name="SNMP Trap Service (UDP In)" | find /i "No rules match" >NUL: 2>&1'
Shell $Cmd
$IsPresent = @ERROR
'Rule is present - Result: ' $IsPresent ?

; Rule does not exist (search for "UDP Out" instead of "UDP In")
$Cmd = '%COMSPEC% /c netsh advfirewall firewall show rule name="SNMP Trap Service (UDP Out)" | find /i "No rules match" >NUL: 2>&1'
Shell $Cmd
$IsPresent = @ERROR
'Rule is not present - Result: ' $IsPresent ?
When a rule is not present, a string containing "No rules match..." is displayed. Using FIND.EXE to search for this text will return a 0 when found and a 1 when not.. which translates to 1=Present, 0=Not Present. From there, the logic should be pretty easy. \:\) If Not $IsPresent {add-rule} EndIf.

To clarify - FIND will return SUCCESS (0) when it finds the search text, and FAIL (1) when it doesn't. This creates a simple Boolean result that is returned to Kix in the @ERROR macro.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#209917 - 2015-02-06 02:00 PM Re: Read value of shell output? [Re: Glenn Barnas]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
Yea. Find is what makes command line bearable, sometimes
_________________________
!

download KiXnet

Top
#209918 - 2015-02-06 02:29 PM Re: Read value of shell output? [Re: Glenn Barnas]
Robdutoit Offline
Hey THIS is FUN
***

Registered: 2012-03-27
Posts: 363
Loc: London, England
I speak under correction, but some research years ago indicated that %comspec% should not be used unless absolutely necessary and was only really necessary 9x systems and possibly Windows XP? I long since removed comspec from my scripts with no adverse effects.

I have never seen the find command in a script.

How is WSUS Package Publisher working out for you Glenn. I have successfully deployed Flash player, Firefox, PDF Xchange Viewer with it and soon I will be trying out acrobat reader and java!

Top
#209919 - 2015-02-06 04:09 PM Re: Read value of shell output? [Re: Robdutoit]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4403
Loc: New Jersey
I don't know where you got that info from, but %COMSPEC% is required when you CALL/RUN a command that is either built into the command shell or you require command piping or redirection, since these are handled by the command processor. The example above both pipes the output from NetSH to Find and redirects output to the Bit Bucket.

If anything should have been removed (or updated, actually) in old scripts were the references to "COMMAND". This was the name of the command processor under "Wintendo" systems (3.x, 9x, ME), after which it was replaced with "CMD" - the 32-bit enhanced version of the shell.

Using %COMSPEC% allowed a script to run properly on all platforms, as it referenced the proper command interpreter for the target platform.

I haven't had time to work with the WSUS tools yet..

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#209920 - 2015-02-06 04:22 PM Re: Read value of shell output? [Re: Glenn Barnas]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4574
Loc: USA
 Quote:
If anything should have been removed (or updated, actually) in old scripts were the references to "COMMAND". This was the name of the command processor under "Wintendo" systems (3.x, 9x, ME), after which it was replaced with "CMD" - the 32-bit enhanced version of the shell.


I think Glenn hit right on the head here.

Maybe my feeble mind is misleading me but I can't say I have ever seen anyone post a combination of the shell and find in the same line like that before. Very useful. Amazing how something I use elsewhere never popped into my mind to use with Kix. \:\)
_________________________
(... better days ahead)

Top
#209921 - 2015-02-06 04:33 PM Re: Read value of shell output? [Re: Allen]
Robdutoit Offline
Hey THIS is FUN
***

Registered: 2012-03-27
Posts: 363
Loc: London, England
I have just checked the article again. I see that you do need the comspec if you are command piping, which is obviously the case here. this is what I was reading up where they advise not to use comspec unless really needed!

comspec and kixtart

Top
#209922 - 2015-02-06 07:51 PM Re: Read value of shell output? [Re: Robdutoit]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
Yes, great site at one time. Amazing it's still around as I'm not sure the creator of the site is still with us or not.

http://home.online.nl/scripting/

Copyright © 2003 scripting@wanadoo.nl - created at 15 July 2000/last updated on 15 November 2003

Top
#209924 - 2015-02-06 09:08 PM Re: Read value of shell output? [Re: Allen]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4403
Loc: New Jersey
WshPipe is definitely the way to go to collect the output of a command. The same rules for using or not using %COMSPEC% apply there.

The Shell command might be a better choice in this instance as the actual output of the command stream isn't used (and is thrown into the bit-bucket, in fact). We just need to know the success/fail status of the Find command. We get that command's status in the Kix @ERROR macro because it's the last command to execute.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#209926 - 2015-02-06 09:19 PM Re: Read value of shell output? [Re: Robdutoit]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4403
Loc: New Jersey
The advice is pretty clear:
* Use %COMSPEC% and not CMD or COMMAND
* Don't use %COMSPEC% unless you need it for redirection or internal commands

Using COMSPEC to run a standard command should be avoided, not because it causes problems, but because it adds an additional and unnecessary command to the mix.

One other thing that came up recently that required the use of COMSPEC - I have a Kix script that performs a test and outputs "running" or "" to indicate when a process is running (or not). I call it from a network monitoring system, specifying the command as "Kix32.exe ShowStat.Kix --C:commandname". The monitoring system tags ">%TMPDIR%\0321984093.txt" to the end of the command, assuming that all output will be collected. I kept getting an empty file as a result - I finally modified the script, changing the "$Result @CRLF" line to "Shell '%COMSPEC% Echo ' + $Result". This allowed the external output redirection to work, and my remote management console to use Kix scripts to check and resolve problems. Since it used a random number as the temp output file, there wasn't any other method available.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#209927 - 2015-02-06 10:07 PM Re: Read value of shell output? [Re: Glenn Barnas]
Robdutoit Offline
Hey THIS is FUN
***

Registered: 2012-03-27
Posts: 363
Loc: London, England
Yes the advice is pretty clear. I should perhaps mention that I read that years ago and I was reciting that from memory. My memory got it a bit wrong. The point I was making was the statement

Don't use comspec unless you actually need to. I had forgotten it wasn't to do with w9x etc.

Top
#209929 - 2015-02-07 05:19 AM Re: Read value of shell output? [Re: Allen]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
 Originally Posted By: Allen

Maybe my feeble mind is misleading me but I can't say I have ever seen anyone post a combination of the shell and find in the same line like that before. Very useful. Amazing how something I use elsewhere never popped into my mind to use with Kix. \:\)


http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Board=7&Number=208166
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Board=7&Number=208164

or
http://www.kixtart.org/forums/ubbthreads...=true#Post40802

now, it would be way nicer to be able to set the errorlevel on that same line...
I've tried all kinds of for tokens crap but never got cmd.exe to like me.
_________________________
!

download KiXnet

Top
#209930 - 2015-02-07 03:18 PM Re: Read value of shell output? [Re: Lonkero]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4574
Loc: USA
Edit...

"Maybe my feeble mind is misleading me but I can't say I have ever seen anyone post a combination of the shell and find in the same line like that" in the past 10 years
_________________________
(... better days ahead)

Top
#209931 - 2015-02-07 03:51 PM Re: Read value of shell output? [Re: Allen]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
really? didn't even look at the dates. I guess the age is catching up to me.
_________________________
!

download KiXnet

Top
#209932 - 2015-02-07 04:10 PM Re: Read value of shell output? [Re: Allen]
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Yo Allen, your feeble mind misleads you there .. or maybe you haven't seen those posts before

Wshpipe and such are not that old ;\)
_________________________



Top
#209933 - 2015-02-07 04:16 PM Re: Read value of shell output? [Re: Jochen]
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
there (was the first crossed my mind)

http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Board=7&Number=181578
_________________________



Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Henriques) and 2516 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.077 seconds in which 0.033 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org