Page 1 of 1 1
Topic Options
#72880 - 2003-02-05 10:04 PM Running a script as a service error...
whaler Offline
Fresh Scripter

Registered: 2002-11-27
Posts: 20
Loc: Mississauga, Canada
I have a kix script installed as a service on a server that is started by a remote user using the "sc.exe" command. The user is connected to the box as an admin, required to be able to start the service, and the service is configured to run as the local administrator.

When I run the script on the box, locally, everything works fine and as expected. When executed as a "service" the script terminates bad, as it leaves "excel.exe" running, as seen in the Task Manager, and the value of $huh seems to be 2 as it always copies out the little text message.

$name.txt is a file created by the client on the local server with a server name.

$open.txt is a "control" file that I use. I have about 9 different versions of this for checking our various environments. This way only one can be run at a time.

code:
SLEEP 20
IF EXIST ("d:\nowmi\open.txt") <> 0
DEL ("d:\nowmi\name.txt")
EXIT
ENDIF

$NULL = OPEN (1,"d:\nowmi\open.txt",5)
$NULL = CLOSE (1)

$NULL = OPEN (1,"d:\nowmi\name.txt",2)
IF @ERROR <> 0
$NULL = CLOSE (1)
DEL ("d:\nowmi\name.txt")
EXIT
ENDIF
$Name = READLINE (1)
$NULL = CLOSE (1)
DEL ("d:\nowmi\name.txt")
$UName = UCASE("$Name")

$COMPUTER = "navacbzw1"
$PROC = "excel.exe"

ENDPROC($COMPUTER,$PROC)

FUNCTION ENDPROC($COMPUTER,$PROC)
For each $Process in GetObject("winmgmts:{impersonationLevel=impersonate}!//$COMPUTER").ExecQuery("select * from Win32_Process where Name='$PROC'")
$ = $Process.Terminate
Next
ENDFUNCTION

$cfilename = "d:\security\servers.xls"
READEXCEL($cfilename)

FUNCTION READEXCEL($cfilename)
IF $cfilename = ""
GOTO END ; -- Excel file not found"
ENDIF
$oxl = Createobject("Excel.application")
$rc = $oxl.workbooks.open($cfilename)
$nrow = 1
$huh = 2
WHILE $oxl.cells($nrow, 1).value <> ""
$nrow = $nrow + 1
IF $oxl.cells($nrow, 4).value = ""
$oxl.quit
$oxl = ""
RETURN
ENDIF
$value1 = $oxl.cells($nrow, 4).value ; -- Get the Hostname
$Uvalue1 = UCASE("$value1")
IF $UValue1 = $UName
$value0 = $oxl.cells($nrow, 6).value ; -- Get the Environment Name
$value2 = $oxl.cells($nrow, 7).value ; -- Get the UserID
$value3 = $oxl.cells($nrow, 8).value ; -- Get the Password
$value4 = $oxl.cells($nrow, 5).value ; -- Get the IP Address
USE "\\$Value4\ipc$" /user:$value2 /password:$value3
IF @ERROR = 0
$NULL = OPEN (1,"d:\HFNetCHK\hostfile.txt",5)
$NULL = WRITELINE (1,"$Value4"+@CRLF)
$NULL = CLOSE (1)
$huh = 1
ELSE
$NULL = OPEN (1,"d:\HFNetCHK\noonemap.txt",5)
$NULL = WRITELINE (1,"Unable to map $Value1 in $Value0 on @DATE"+@CRLF)
$NULL = CLOSE (1)
SHELL 'net use x: \\49.0.0.1\d$ /user:administrator password'
COPY "d:\hfnetchk\noonemap.txt" "x:\inetpub\wwwroot\mspatch\noonemap.txt"
DEL ("x:\inetpub\wwwroot\mspatch\mapone.txt")
$huh = 0
ENDIF
ENDIF
LOOP
$oxl.quit
$oxl = ""
ENDFUNCTION

SELECT
CASE $huh = 1
GOTO Continue
CASE $huh = 2
GOTO Nothing
CASE $huh = 0
GOTO END
ENDSELECT

GOTO END

:Continue

SELECT

CASE $value0 = "PAT"
SHELL 'hfnetchk -fip d:\hfnetchk\hostfile.txt -t 5 -s 2 -nosum -o tab -f d:\patchlogging\hfnetchk_PAT.txt'
SHELL 'dtsrun /S49.0.0.0 /Usa /Ppassword /NHFNETCHK_PAT_DTS'
CASE $value0 = "CPO"
SHELL 'hfnetchk -fip d:\hfnetchk\hostfile.txt -t 5 -s 2 -nosum -o tab -f d:\patchlogging\hfnetchk_CPO.txt'
SHELL 'dtsrun /S49.0.0.0 /Usa /Ppassword /NHFNETCHK_CPO_DTS'
CASE $value0 = "SOC"
SHELL 'hfnetchk -fip d:\hfnetchk\hostfile.txt -t 5 -s 2 -nosum -o tab -f d:\patchlogging\hfnetchk_SOC.txt'
SHELL 'dtsrun /S49.0.0.0 /Usa /Ppassword /NHFNETCHK_SOC_DTS'
CASE $value0 = "TREE"
SHELL 'hfnetchk -fip d:\hfnetchk\hostfile.txt -t 5 -s 2 -nosum -o tab -f d:\patchlogging\hfnetchk_TREE.txt'
SHELL 'dtsrun /S49.0.0.0 /Usa /Ppassword /NHFNETCHK_TREE_DTS'
CASE $value0 = "DEV"
SHELL 'hfnetchk -fip d:\hfnetchk\hostfile.txt -t 5 -s 2 -nosum -o tab -f d:\patchlogging\hfnetchk_DEV.txt'
SHELL 'dtsrun /S49.0.0.0 /Usa /Ppassword /NHFNETCHK_DEV_DTS'
CASE $value0 = "TREB"
SHELL 'hfnetchk -fip d:\hfnetchk\hostfile.txt -t 5 -s 2 -nosum -o tab -f d:\patchlogging\hfnetchk_TREB.txt'
SHELL 'dtsrun /S49.0.0.0 /Usa /Ppassword /NHFNETCHK_TREB_DTS'
ENDSELECT

SHELL 'dtsrun /S49.0.0.0 /Usa /Ppassword /NCMSPATCH1'

SHELL 'net use x: \\49.0.0.1\d$ /user:administrator password'
DEL ("x:\inetpub\wwwroot\mspatch\noonemap.txt")
COPY "d:\hfnetchk\hostfile.txt" "x:\inetpub\wwwroot\mspatch\mapone.txt"
GOTO END

:Nothing
SHELL 'net use x: \\49.0.0.1\d$ /user:administrator password'
DEL ("x:\inetpub\wwwroot\mspatch\noonemap.txt")
$NULL = REDIRECTOUTPUT ("X:\inetpub\wwwroot\mspatch\mapone.txt",1)
"For some reason the server name you entered could not be found"+@CRLF
"Please check your spelling. If this does not work, please verify"+@CRLF
"that the system exists in the security inventory spreadsheet."+@CRLF
" "+@CRLF
"SERVER NAME ENTERED = $name"
$NULL = REDIRECTOUTPUT ("")

:END

SHELL 'net use * /D /y'

DEL ("d:\HFNetCHK\hostfile.txt")
DEL ("d:\HFNetCHK\noonemap.txt")

DEL ("d:\hfnetchk\DFSD")
DEL ("d:\patchlogging\DFSD")
:END
DEL ("d:\nowmi\open.txt")

EXIT

Anyone with thoughts or hints... or general code improvements? [Smile]

[ 06. February 2003, 06:06: Message edited by: whaler ]

Top
#72881 - 2003-02-05 10:12 PM Re: Running a script as a service error...
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
1 thing:
if run as local admin (not net username), it can't access the net as it has no rights.

hang of excel... is the service able to run interactively?
_________________________
!

download KiXnet

Top
#72882 - 2003-02-05 10:17 PM Re: Running a script as a service error...
Waltz Offline
Seasoned Scripter

Registered: 2002-08-01
Posts: 485
Loc: Waterloo, Ontario, Canada
...typo... [Smile]
quote:
spreedsheet
_________________________
We all live in a Yellow Subroutine...

Top
#72883 - 2003-02-05 10:18 PM Re: Running a script as a service error...
whaler Offline
Fresh Scripter

Registered: 2002-11-27
Posts: 20
Loc: Mississauga, Canada
Lonkero,
Why would the service need more than local admin? Almost everything it is doing is on the local box - when it copies a file off, I map the drive before hand. BTW, it works fine when I am logged on as the local admin and run the script.
As for Interactive - I think you may have nailed my hanging issue. When I am logged in and run the script, no problem, but as a service, excel hangs. But, for the service "Log On" permissions, I user the local admin ID. How would this then be run "interactively"?
Thanks,
Shawn

Top
#72884 - 2003-02-05 10:21 PM Re: Running a script as a service error...
whaler Offline
Fresh Scripter

Registered: 2002-11-27
Posts: 20
Loc: Mississauga, Canada
Thanks Waltz - fixed. However - don't think that is the cause of all my headaches. [Roll Eyes]
Top
#72885 - 2003-02-05 10:26 PM Re: Running a script as a service error...
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
there is a tab in the service properties...
something like "allow this service to interact with the desktop"
_________________________
!

download KiXnet

Top
#72886 - 2003-02-05 10:47 PM Re: Running a script as a service error...
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Use a database instead of Excel. Then you can take advantage of ODBC and don't have to open Excel to retrive info. There are DB UDFs already available like DBCommand() in the UDF Forum. They support pretty much anything from Microsoft Access to SQL Server, Oracle, MySQL,...
_________________________
There are two types of vessels, submarines and targets.

Top
#72887 - 2003-02-05 10:51 PM Re: Running a script as a service error...
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Jens brings up a good point.

If your process runs amok, it will leave Excel up and running as a "background application." The only way to see this is to go to the process list on a machine and terminate it.

The other caveat is that there is a 67K limit of rows you can enter into Excel..

HTH,

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#72888 - 2003-02-06 01:57 AM Re: Running a script as a service error...
whaler Offline
Fresh Scripter

Registered: 2002-11-27
Posts: 20
Loc: Mississauga, Canada
Thanks for the replies guys. As for the service settings, this is a Win2K box - on the tab "log on" you can enable that feature IF you have it run as a local system account. Would this be a good thing?? Not to sure about running it with this, versus as a Local Admin.

Secondly, I would agree that using the SQL would be a lot nicer maybe. The excel piece was built beause it was fast to do, and the XLS file can be quickly modified and cut up for various tests. However, in regards to Excel being left open, that is not my main problem. The start of every script contains the UDF that allows me to kill any currently running ones. This is just an indicator of a bigger problem, that the script is not running properly and is erroring out at some point and leaving Excel open. Unless I fix this problem, would I not have the same issue with SQL queries. Regardless of where I parse the data from, there seems to be a bug in the script that doesn't process correctly, hence leaving Excel open and leaving the $huh set at the default of 2.

Thanks,
Shawn

Top
#72889 - 2003-02-06 04:27 AM Re: Running a script as a service error...
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
You should go with the better solution from the beginning as it is unlikely that you'll rewrite/rearchitect scripts once they're finished.

A Microsoft Access database with just one table is set up as fast as your Excel spreadsheet. Coding the information retrieval via SQL is basically a one-liner if you use DBCommand(). And you can have multiple users/scripts access the database at the same time.

It's also the "more correct" way to approach the problem, IMHO.

Another question: Why does it need to run as a service? I do not see anything in the code that woudl justify this. You could alwasy use the ScheduleTask() UDF to remotely schedule the execution of a script.

There are a couple of UDFs that control services remotely without external executables.

Finally, doesn't SC require administrative privileges for the remote server in order to control the service? What type of rights does yoru typical user have?

Oh, and do I see that correctly that you keep your user's passwords on file? That would be a big (HUGE) NO-NO with me. Also, you might want to clean up your posted code and remove that public IP address and that admin password that kept in there!
_________________________
There are two types of vessels, submarines and targets.

Top
#72890 - 2003-02-06 06:02 AM Re: Running a script as a service error...
whaler Offline
Fresh Scripter

Registered: 2002-11-27
Posts: 20
Loc: Mississauga, Canada
Thanks for the info Jens. As for the "public" ip - not really. The powers that be here decided our internal network would use 49.x.x.x for some reason, so if there is a box on the Net with that IP, it isn't mine. [Smile] Going to convert these to .exe's, or something, once I get it all working. I also have another piece that deletes all the files of the user's computer when the script is done. As for SC, the script makes an IPC$ connection passing the admin ID fromthe user's PC, so that the "service" can be run. As for doing it as a scheduled task, I have had mixed results with this. Almost every "application"server here is in a workgroup, with the user's login into a seperate domain. When I used SCHEDULETASK, I always get the error:
quote:
[FAIL ] ITaskScheduler::SetTargetComputer hr=0x80070005
which basically means that JT.exe doesn't have the right permission on the server to create the task, I believe. Mapping an IPC$ connection, like I did for SC, didn't help either, hence the service approach. I am going to try coding it up using a SQL DB as a source and see what happens.

Top
#72891 - 2003-02-07 02:22 PM Re: Running a script as a service error...
whaler Offline
Fresh Scripter

Registered: 2002-11-27
Posts: 20
Loc: Mississauga, Canada
Well did some rework using the UDF DBCommand () that Jens wrote, I have it returning all the values I want. Now, my only problem, is getting the mapping working. Here is my code so far:

code:
$ConnDSN="DRIVER=SQL Server;SERVER=$Server;UID=$User;PWD=$Password;DATABASE=hfnetchk"
$sql="SELECT Location, IP1, NTUser, NTPassword from Contact WHERE Location='PAT'"
$recordset = DBCommand($ConnDSN,$sql)

$NULL=REDIRECTOUTPUT("c:\array.txt",0)
for $row=0 to ubound($recordset,1)
USE "\\'RTRIM($recordset[$row,1])'\IPC$" /user:'RTRIM($recordset[$row,2])' /password:'RTRIM($recordset[$row,3])'
? "\\"+RTRIM($recordset[$row,1])+"\IPC$ /user:"+RTRIM($recordset[$row,2])+" /password:"+RTRIM($recordset[$row,3])
next
$NULL=REDIRECTOUTPUT("")

I use the REDIRECT so I can see the "expected" output, and it looks perfect. But I cannot seem to get the syntax correct for either a USE command or a SHELL 'net use...' command. I have tried various ways to try and write it up. Always get the literal "RTRIM($recordset[$row,3])" passed in the statement.

Anyone have an idea of how to take the variables and input them into a mapping that works?

Thanks!
Shawn

Top
#72892 - 2003-02-07 04:10 PM Re: Running a script as a service error...
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Build the strings ahead of time and print them to the screen for evaluation:
code:
$driveletter='*'
$UNC='\\'+TRIM($recordset[$row,1])+'\IPC$$'
$user='/user:'+TRIM($recordset[$row,2])
$password='/password:'+TRIM($recordset[$row,3])
? $driveletter
? $UNC
? $user
? $password
use $driveletter $UNC $user $password
? 'Error = '+@ERROR+' - '+@SERROR

_________________________
There are two types of vessels, submarines and targets.

Top
#72893 - 2003-02-07 04:39 PM Re: Running a script as a service error...
whaler Offline
Fresh Scripter

Registered: 2002-11-27
Posts: 20
Loc: Mississauga, Canada
Jens,
Excellent, that should work. Noticed one thing in your code, the "unc" path needs to be in quotes for the USE command. Also, does the USE command allow for disconnected mappings, like to the IPC$ share without assigning a drive letter? I am mapping between 150 and 250 servers each run. That is one of the reasons why I initially used the SHELL with a "net use" statement.

Top
#72894 - 2003-02-07 04:46 PM Re: Running a script as a service error...
whaler Offline
Fresh Scripter

Registered: 2002-11-27
Posts: 20
Loc: Mississauga, Canada
Made some quick mods, thanks to all of Jens great ideas, and came up with this:

code:
$UNC='\\'+TRIM($recordset[$row,1])+'\IPC$$'
$user='/user:'+TRIM($recordset[$row,2])
$password=''+TRIM($recordset[$row,3])
? $UNC
? $user
? $password
SHELL 'net use $UNC $user $password'

Works like charm...

Top
#72895 - 2003-02-07 05:18 PM Re: Running a script as a service error...
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Have you tried it with $driveletter='*' or an empty drivelettter?
_________________________
There are two types of vessels, submarines and targets.

Top
Page 1 of 1 1


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

Who's Online
0 registered and 972 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.162 seconds in which 0.122 seconds were spent on a total of 11 queries. Zlib compression enabled.

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