#72880 - 2003-02-05 10:04 PM
Running a script as a service error...
|
whaler
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]](images/icons/smile.gif) [ 06. February 2003, 06:06: Message edited by: whaler ]
|
|
Top
|
|
|
|
#72883 - 2003-02-05 10:18 PM
Re: Running a script as a service error...
|
whaler
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
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.
|
|
Top
|
|
|
|
#72887 - 2003-02-05 10:51 PM
Re: Running a script as a service error...
|
Kdyer
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
|
|
Top
|
|
|
|
#72888 - 2003-02-06 01:57 AM
Re: Running a script as a service error...
|
whaler
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
|
|
|
|
#72890 - 2003-02-06 06:02 AM
Re: Running a script as a service error...
|
whaler
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. 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
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
|
|
|
|
#72893 - 2003-02-07 04:39 PM
Re: Running a script as a service error...
|
whaler
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
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
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 483 anonymous users online.
|
|
|