markdavies1978
(Fresh Scripter)
2008-10-30 06:50 PM
Calling a .bat/.exe during logon

Hi all,

I'm a novice user of Kix - Only use it to map drives, printers etc.

What I would like to be able to do is during my logon script is to run a bat or exe file in the background/at the same time of logon.

Is this possible?

Thanks.


Gargoyle
(MM club member)
2008-10-30 07:46 PM
Re: Calling a .bat/.exe during logon

Simply put - yes -

Look at the manual for the command "RUN"


markdavies1978
(Fresh Scripter)
2008-10-30 08:12 PM
Re: Calling a .bat/.exe during logon

Ok, got my bat file to run but it's running within the login script window.

Anyway I can get this to run either silently or in it's own window?


Gargoyle
(MM club member)
2008-10-30 08:19 PM
Re: Calling a .bat/.exe during logon

What is in your batch file. If it is just calling another executable, run the executable and do away with the batch file.

As to running silently, it depends on what you are doing and if the application that you are running supports it.

Many factors can come into play, but need more information to give advice.


markdavies1978
(Fresh Scripter)
2008-10-30 08:27 PM
Re: Calling a .bat/.exe during logon

It is a script to add a SQL DSN to certain PC's.

eriqjaffe
(Hey THIS is FUN)
2008-10-30 08:51 PM
Re: Calling a .bat/.exe during logon

Something like this?

 Code:
RUN ("%COMSPEC% /C START /MIN batchfile.bat")

That should spawn your batch file in another minimized window.


LonkeroAdministrator
(KiX Master Guru)
2008-10-30 10:14 PM
Re: Calling a .bat/.exe during logon

unless you are running wkix32.exe
it shouldn't be able to "spawn"...


BradV
(Seasoned Scripter)
2008-10-31 11:18 AM
Re: Calling a .bat/.exe during logon

You can probably do it directly from kix. Here is a link to a script to do it in basic.

http://support.microsoft.com/kb/q184608/

This is using a gui. You could use kixforms, or since you know the data for the DSN, you could just pass it to it (maybe put it in a ini file so that the code is generic).

Regards,

Brad


LonkeroAdministrator
(KiX Master Guru)
2008-10-31 11:23 AM
Re: Calling a .bat/.exe during logon

yep.
looks like simple registry write.


markdavies1978
(Fresh Scripter)
2008-10-31 04:07 PM
Re: Calling a .bat/.exe during logon

Running the bat file isn't working for me, here it is:

IF INGROUP ("ODBC_UPDATE")
RUN "L:\Policy_Area\TEST.bat"

ENDIF

Should this work?


Mart
(KiX Supporter)
2008-10-31 04:16 PM
Re: Calling a .bat/.exe during logon

My guess would be yes if the person has an L drive mapped before starting the batch file and he/she has access to the drive and it's folders.

markdavies1978
(Fresh Scripter)
2008-10-31 04:24 PM
Re: Calling a .bat/.exe during logon

Doesn't seem to work with the above or if I use the full UNC path...

Mart
(KiX Supporter)
2008-10-31 04:28 PM
Re: Calling a .bat/.exe during logon

If you put some de bug lines in the batch file (echo to the screen, pauses and stuff) do you see those lines so you know it is running?

What does the batch file do? Can you post its contents?


markdavies1978
(Fresh Scripter)
2008-10-31 04:42 PM
Re: Calling a .bat/.exe during logon

The bat file contains the following:

cscript \\servername\path\odbc_ini.vbs


markdavies1978
(Fresh Scripter)
2008-10-31 04:45 PM
Re: Calling a .bat/.exe during logon

Oh, it works fine if I run it manually just not via the logon script...

Gargoyle
(MM club member)
2008-10-31 04:46 PM
Re: Calling a .bat/.exe during logon

What is in the VBS script, we can probably convert it to KiX and there would be no need to Run / Call anything then.

markdavies1978
(Fresh Scripter)
2008-10-31 04:52 PM
Re: Calling a .bat/.exe during logon

Content of VBS script:

'
' 16-bit ODBC setup to run >cscript odbc_ini.vbs
'
'
' Required entries in ODBC.ini - WIN.INI for db9server entry
'
' [ODBC Data Sources]
' db9server=SQL Server
'
' [db9server]
' Driver=C:\WINNT\SYSTEM\sqlsrvr.dll
' Description=
' Server=db9server
' Address=172.27.44.54,1443
' UseProcForPrepare=Yes
' QuotedId=Yes
' AnsiNPW=Yes
' OemToAnsi=No
' Trusted_Connection=


Const ForReading = 1
Const ForWriting = 2
found="N"

Set Shell = WScript.CreateObject("WScript.Shell")

windowsdir = shell.ExpandEnvironmentStrings("%windir%")



' ------------------------------------------------------------
' Add entry to WIN.INI
' ------------------------------------------------------------


Set objFSO = CreateObject("Scripting.FileSystemObject")
' Create Backup
objFSO.CopyFile windowsdir & "\win.ini",windowsdir & "\win.bak",true
WScript.Echo " ...Creating backup " & windowsdir & "\win.bak"

Set objTextFile = objFSO.OpenTextFile(windowsdir & "\win.ini", ForReading)
WScript.Echo " ...Updating " & windowsdir & "\win.ini"

Do Until objTextFile.AtEndOfStream

strNextLine = objTextFile.Readline

intLineFinder = InStr(strNextLine, "[SQLSERVER]")
If intLineFinder <> 0 Then

strNewFile = strNewFile & strNextLine & vbCrLf
strNextLine = objTextFile.Readline

Do Until InStr(strNextLine, "[") <> 0
if InStr(strNextLine, "db9server=") <> 0 then
found="Y"
WScript.Echo " ...WIN.INI Entry found!!"
End If
strNewFile = strNewFile & strNextLine & vbCrLf
strNextLine = objTextFile.Readline
Loop

If found="N" then
strNewFile = strNewFile & "db9server=DBMSSOC3,172.27.44.54,1443" & vbCrLf
WScript.Echo " ...Adding to Data Sources"
End If
End If

strNewFile = strNewFile & strNextLine & vbCrLf
Loop

objTextFile.Close

If found="N" then
WScript.Echo " ...Saving File"
Set objTextFile = objFSO.OpenTextFile(windowsdir & "\WIN.ini", ForWriting)
objTextFile.WriteLine strNewFile
objTextFile.Close
End If

' --------------------------------------------------------------
' Add entries to ODBC.ini
' --------------------------------------------------------------

found="N"
strNewFile=""

Set objFSO = CreateObject("Scripting.FileSystemObject")
' Create Backup
objFSO.CopyFile windowsdir & "\ODBC.ini",windowsdir & "\ODBC.bak",true
WScript.Echo " ...Creating backup " & windowsdir & "\ODBC.bak"

Set objTextFile = objFSO.OpenTextFile(windowsdir & "\ODBC.ini", ForReading)
WScript.Echo " ...Updating " & windowsdir & "\ODBC.ini"

Do Until objTextFile.AtEndOfStream

strNextLine = objTextFile.Readline

intLineFinder = InStr(strNextLine, "[ODBC Data Sources]")
If intLineFinder <> 0 Then

strNewFile = strNewFile & strNextLine & vbCrLf
strNextLine = objTextFile.Readline

Do Until InStr(strNextLine, "[") <> 0
if InStr(strNextLine, "db9server=") <> 0 then
found="Y"
WScript.Echo " ...ODBC Entry found!!"
End If
strNewFile = strNewFile & strNextLine & vbCrLf
strNextLine = objTextFile.Readline
Loop

If found="N" then
strNewFile = strNewFile & "db9server=SQL Server" & vbCrLf
WScript.Echo " ...Adding to Data Sources"
End If
End If

strNewFile = strNewFile & strNextLine & vbCrLf
Loop

If found="N" then
strNewFile = strNewFile & "[db9server]" & vbCrlf
strNewFile = strNewFile & "Driver=" & windowsdir & "\SYSTEM\sqlsrvr.dll" & vbCrlf
strNewFile = strNewFile & "Description=db9server" & vbCrlf
strNewFile = strNewFile & "Server=db9server" & vbCrlf
strNewFile = strNewFile & "Address=172.27.44.54,1443" & vbCrlf
strNewFile = strNewFile & "UseProcForPrepare=Yes" & vbCrlf
strNewFile = strNewFile & "QuotedId=Yes" & vbCrlf
strNewFile = strNewFile & "AnsiNPW=Yes" & vbCrlf
strNewFile = strNewFile & "OemToAnsi=No" & vbCrlf
strNewFile = strNewFile & "Trusted_Connection=" & vbCrlf
strNewFile = strNewFile & "LastUser=" & vbCrlf
WScript.Echo " ...Adding Entry"
End If

objTextFile.Close

If found="N" then
WScript.Echo " ...Saving File"
Set objTextFile = objFSO.OpenTextFile(windowsdir & "\ODBC.ini", ForWriting)
objTextFile.WriteLine strNewFile
objTextFile.Close
End If


Mart
(KiX Supporter)
2008-10-31 04:57 PM
Re: Calling a .bat/.exe during logon

Looks like it is just writing stuff to ini files. Not 100% sure because I'm almost blind in the VBS world. Kix can write to ini files for you so you don't need the VBS script anymore.

One other thing, you are writing to win.ini and odbc.ini in the %windir%. Regular users cannot write to those file with default permissions set on the files and folders.


markdavies1978
(Fresh Scripter)
2008-10-31 05:03 PM
Re: Calling a .bat/.exe during logon

Mart, thanks for your time on this.

I've allowed users to write to these files & tested this by running the vbs file or running my bat file manually.

It just appears to be getting this to run within KIX is my problem

Cheers.


BradV
(Seasoned Scripter)
2008-10-31 05:19 PM
Re: Calling a .bat/.exe during logon

What is the content of test.bat?

Gargoyle
(MM club member)
2008-10-31 05:25 PM
Re: Calling a .bat/.exe during logon

Try adding this to your KiX script and test it.

 Code:
$WinIni = %windir%+"\win.ini"
$ODBCIni = %windir%+"\odbc.ini"
$RC = WriteProfileString($WinIni,"db9server","Driver","C:\WINNT\SYSTEM\sqlsrvr.dll")
$RC = WriteProfileString($WinIni,"db9server","Description","")
$RC = WriteProfileString($WinIni,"db9server","Server","db9server")
$RC = WriteProfileString($WinIni,"db9server","Address","172.27.44.54,1443")
$RC = WriteProfileString($WinIni,"db9server","UseProcForPrepare","Yes")
$RC = WriteProfileString($WinIni,"db9server","QuotedId","Yes")
$RC = WriteProfileString($WinIni,"db9server","AnsiNPW","Yes")
$RC = WriteProfileString($WinIni,"db9server","OemtoAnsi","No")
$RC = WriteProfileString($WinIni,"db9server","Trusted_Connection","")

$RC = WriteProfileString($ODBCIni,"ODBC Data Sources","db9server","SQL Server")


markdavies1978
(Fresh Scripter)
2008-10-31 06:11 PM
Re: Calling a .bat/.exe during logon

 Originally Posted By: BradV
What is the content of test.bat?


cscript odbc_ini.vbs


markdavies1978
(Fresh Scripter)
2008-10-31 06:12 PM
Re: Calling a .bat/.exe during logon

 Originally Posted By: Gargoyle
Try adding this to your KiX script and test it.

 Code:
$WinIni = %windir%+"\win.ini"
$ODBCIni = %windir%+"\odbc.ini"
$RC = WriteProfileString($WinIni,"db9server","Driver","C:\WINNT\SYSTEM\sqlsrvr.dll")
$RC = WriteProfileString($WinIni,"db9server","Description","")
$RC = WriteProfileString($WinIni,"db9server","Server","db9server")
$RC = WriteProfileString($WinIni,"db9server","Address","172.27.44.54,1443")
$RC = WriteProfileString($WinIni,"db9server","UseProcForPrepare","Yes")
$RC = WriteProfileString($WinIni,"db9server","QuotedId","Yes")
$RC = WriteProfileString($WinIni,"db9server","AnsiNPW","Yes")
$RC = WriteProfileString($WinIni,"db9server","OemtoAnsi","No")
$RC = WriteProfileString($WinIni,"db9server","Trusted_Connection","")

$RC = WriteProfileString($ODBCIni,"ODBC Data Sources","db9server","SQL Server")


How can I run this for specific users only?


Gargoyle
(MM club member)
2008-10-31 06:46 PM
Re: Calling a .bat/.exe during logon

In this part of your script
 Code:
IF INGROUP ("ODBC_UPDATE")
RUN "L:\Policy_Area\TEST.bat"

ENDIF


Replace your run statement with the above code


markdavies1978
(Fresh Scripter)
2008-10-31 07:34 PM
Re: Calling a .bat/.exe during logon

Thanks Gargoyle, getting there!

It has now addedd the required ODBC entry apart from the IP address!


Gargoyle
(MM club member)
2008-10-31 07:55 PM
Re: Calling a .bat/.exe during logon

Glad to help. There are ways to make the code cleaner and faster if you ever want to explore those options, but that is your call. And welcome to the world of KiX.

markdavies1978
(Fresh Scripter)
2008-11-01 12:11 PM
Re: Calling a .bat/.exe during logon

I'm still stuggling to get the IP address added to the ODBC setting.

It adds the SQL entry & server name fine which is great just not the Network Address...

I think the code from the previous posts isn't adding the address to C:\Windows\win.ini & C:\Windows\ODBC.ini


Gargoyle
(MM club member)
2008-11-01 05:06 PM
Re: Calling a .bat/.exe during logon

This is the line that is not working ?

$RC = WriteProfileString($WinIni,"db9server","Address","172.27.44.54,1443")

Add this line directly after it and see what the error code is

 Code:
"Return code was "+@Error+" "+@SError ?


markdavies1978
(Fresh Scripter)
2008-11-03 08:28 PM
Re: Calling a .bat/.exe during logon

Gargoyle,

Having had a chat with 1 of my developers I simply had to adjust the example code you posted.

Thanks.


LonkeroAdministrator
(KiX Master Guru)
2008-11-03 10:53 PM
Re: Calling a .bat/.exe during logon

why use the .bat file, when you can just shell the same line with kix?

if you still want to keep on using that vbs-script, that is...