mac_shinobi Posted March 31, 2011 Posted March 31, 2011 (edited) Basically want to neaten the below chunk of code up as much as possible and it needs to do the following : Currently I have a pre configured ini file called versionInfo.ini which is located on the root of my usb memory stick in a folder called network connect ini 1. Need to get the path of the script I run which is in the same directory as the ini file and establish the ini file is there 2. Launch network connect which is generally in the following directory / path C:\Program Files\Juniper Networks\Network Connect 6.3.0\dsNetworkConnect.exe The above path will need to be checked as the version number can change ie 6.3.3 or 6.3.0 or otherwise so this will obviously effect the path and will make the script error if it is not the correct path to the exe ( dsNetworkConnect.exe ) 4. After a set time or after Network Connect exe has loaded it needs to be terminated ( the reason for doing this is so that network connect creates the relevant directories under the currently logged on users account which is where it creates a default ini file which will be replaced by my pre configured ini file. 5. Get the currently logged on username so John.Smith or the likes without pre fixed domain etc 6. From the path to the ini file on the memory stick ( step 1 ), username ( step 5 ) and the below path which is where network connects finds and uses the ini file we need to overwrite this ini file with my pre configured ini file C:\Documents and Settings\\Application Data\Juniper Networks\Network Connect 6.3.0 Again where is the username from step 5 and as above the version number in the path will effect where the pre configured ini file goes which is crucial because if the ini file goes in the wrong place then network connect will not pickup the relevant ini file 7. In order for the ini file to have any effect network connect needs to be terminated ( dsNetworkConnect.exe ) and also the juniper network connect service(s) need to be restarted - From what I can tell the main service name is dsNcService Below is what I have but seems to be very messy Dim WshShell, fso, Dir, strProgFold, x, strUser Dim strDir, objDir, aItem, iItem, bItem Set WshShell = WScript.CreateObject("WScript.Shell") Set fso = CreateObject("Scripting.FileSystemObject") Set wshNetwork = WScript.CreateObject("WScript.Network") Dim strCurrentFolder: strCurrentFolder = fso.getParentFolderName(Wscript.ScriptFullName): If Right(strCurrentFolder, 1) <> "\" Then strCurrentFolder = strCurrentFolder & "\" Dir = strCurrentFolder strProgFold = WshShell.ExpandEnvironmentStrings("%ProgramFiles%"): If Right(strProgFold, 1) <> "\" Then strProgFold = strProgFold & "\" strDir = "C:\Program Files\" getInfo(strDir) Function getInfo(Path) Set Folder = fso.GetFolder(Path) Set Files = Folder.Files For Each File in Files If LCase(File.Name) = "dsnetworkconnect.exe" Then Set WshShell = CreateObject("Wscript.Shell") MsgBox "File Found in " & File.Path WshShell.exec chr(34) & File.Path & chr(34) wscript.sleep 4000 KillExe() strUser = wshNetwork.UserName x = InStrRev(WScript.ScriptFullName,"\") 'copy ini file from mem stick to profile dim strMemFull dim strMem strMem = Left(Wscript.ScriptFullName,x) strMemFull = Left(Wscript.ScriptFullName,x) & "versionInfo.ini" msgbox strMem If fso.FileExists(strMemFull) Then fso.CopyFile strMemFull, "C:\Documents and Settings\" & strUser & "\Application Data\Juniper Networks\Network Connect 6.3.0\versionInfo.ini" End If Restart() ' path to current vbs - needs changing to get ini path to copy into profile WScript.Quit End If Next Set Subfolders = Folder.SubFolders For Each Subfolder in SubFolders getInfo(Subfolder.path) Next End Function Sub Restart() Dim objWMIService, objItem, objService Dim colListOfServices, strComputer, strService, intSleep strComputer = "." intSleep = 15000 WScript.Echo " Click OK, then wait " & intSleep/1000 & " seconds" 'On Error Resume Next ' NB strService is case sensitive. strService = " 'dsNcService' " Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & strComputer & "\root\cimv2") Set colListOfServices = objWMIService.ExecQuery _ ("Select * from Win32_Service Where Name ="_ & strService & " ") For Each objService in colListOfServices objService.StopService() WSCript.Sleep intSleep objService.StartService() Next WScript.Echo "Your "& strService & " service has Started" End Sub Sub KillExe() Dim objWMIService, objProcess, colProcess Dim strComputer, strProcessKill Set wshNetwork = WScript.CreateObject("WScript.Network") strComputer = wshNetwork.ComputerName strProcessKill = "'dsNetworkConnect.exe'" Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colProcess = objWMIService.ExecQuery("Select * from Win32_Process Where Name = " & strProcessKill ) For Each objProcess in colProcess objProcess.Terminate() Next WSCript.Echo "Just killed process " & strProcessKill & " on " & strComputer End Sub Edited March 31, 2011 by mac_shinobi
Steve21 Posted March 31, 2011 Posted March 31, 2011 (edited) C:\Program Files\Juniper Networks\Network Connect 6.3.0\dsNetworkConnect.exe Is that always the same file path "just" with diff version? But rest stays same? And: Currently I have a pre configured ini file called versionInfo.ini which is located on the root of my usb memory stick in a folder called network connect ini 1. Need to get the path of the script I run which is in the same directory as the ini file and establish the ini file is there Is the script in the root, or inside the network connect ini folder? Steve Edited March 31, 2011 by Steve21
mac_shinobi Posted March 31, 2011 Author Posted March 31, 2011 Is that always the same file path "just" with diff version? But rest stays same? And: Is the script in the root, or inside the network connect ini folder? Steve 1. From what I can gather and what I have seen it is the version number that changes and the rest is the same ( obviously there are 2 paths ) one for the dsnetworkconnect.exe ( inside of program files ) and the other for the ini file inside the users profile 2. Both the script and the ini file are both inside the network connect ini directory / folder which is on my USB Mem Stick ( want to rename the folder to just network connect though Thanks
Steve21 Posted March 31, 2011 Posted March 31, 2011 Also both in 4 and 7 you're terminating service, but never restarting it? Is this an omission, or is it supposed to restart somewhere? Steve
mac_shinobi Posted March 31, 2011 Author Posted March 31, 2011 Also both in 4 and 7 you're terminating service, but never restarting it? Is this an omission, or is it supposed to restart somewhere? Steve dsNetworkConnect.exe is the executable program ( so when you launch network connect this is the exe that runs in the processes tab in task manager ) which I am terminating. The dsNcService is the service ( start --> run --> services.msc ) which needs to be restarted dsNetworkConnect is the one that is being launched , wait until its loaded and terminate it, copy ini file across into relevant path in users profile, restart service dsNcService So the exe and service are seperate. If my script is doing it wrong then that is obviously my omission / fault in poor coding , doh !
Steve21 Posted March 31, 2011 Posted March 31, 2011 So really: 7. In order for the ini file to have any effect network connect needs to be terminated ( dsNetworkConnect.exe ) and Is all lies?!??!!? As it was terminated in step 4. Just restart service in Step 7? Steve 1
Steve21 Posted March 31, 2011 Posted March 31, 2011 (edited) Try this, to see if it works ok. Works here but I was basing it on notepad running Seeing I didn't have your program. Re-wrote most of it, seeing it was missing things like versions etc etc, but yeah shout if it works, or if it breaks Kinda cheated on returning version number from exe, but depends how the program names itself with versions as to if that will work aka 6.3.0 would work, something stupid like 6.00003.000000 would break etc etc But yeah gimme a shout, somethings bound to break :'( Set fso = CreateObject("Scripting.FileSystemObject") PathOfScript = fso.GetParentFolderName(wscript.ScriptFullName) If fso.FileExists(PathOfScript & "\versionInfo.ini") then Set WshShell = WScript.CreateObject("WScript.Shell") ProgramPath = "C:\Program Files\Juniper Networks\.\dsNetworkConnect.exe" Version = fso.GetFileVersion(ProgramPath) If fso.FileExists(ProgramPath) then WScript.sleep 1000 WshShell.Run ProgramPath WScript.sleep 10000 else WScript.Quit end if else WScript.Quit end if strComputer = "." Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colProcessList = objWMIService.ExecQuery ("SELECT * FROM Win32_Process WHERE Name = 'dsNetworkConnect.exe'") For Each objProcess in colProcessList objProcess.Terminate() Next Set objNet = CreateObject("WScript.NetWork") UserName = objNet.UserName PathOfIni = PathOfScript & "\versionInfo.ini" PathOfFile = "C:\Documents and Settings\" & UserName & "\Application Data\Juniper Networks\Network Connect " & Version & "\" fso.CopyFile PathOfIni, PathOfFile, True strServiceName = "dsNcService" Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name ='" & strServiceName & "'") For Each objService in colListOfServices objService.StopService() Next For Each objService in colListOfServices objService.StartService() Next * Edit - If you prefer your old one just tidied up could do that too Just seemed easier to restart Steve Edited March 31, 2011 by Steve21
mac_shinobi Posted March 31, 2011 Author Posted March 31, 2011 (edited) Running the script, checking the process tab in task manager and can't see cscript.exe or wscript.exe and there are no prompts ie msgbox / wscript.echo's to indicate progress Refreshing the directory where the ini file should appear after copying and no new ini file appears. Must be the path to trying to get the version of the exe ( I think ) Set WshShell = WScript.CreateObject("WScript.Shell") [b]ProgramPath = "C:\Program Files\Juniper Networks\.\dsNetworkConnect.exe"[/b] use FSO to check if a directory exists that starts with Network and if so then to return the folder / directory name and append that to the program files directory ?? Edited March 31, 2011 by mac_shinobi
Steve21 Posted March 31, 2011 Posted March 31, 2011 Could stick in some echo's for now, just for testing. Might be breaking out as soon as it starts, if it can't find file. Gimme a sec.
Steve21 Posted March 31, 2011 Posted March 31, 2011 Set fso = CreateObject("Scripting.FileSystemObject") PathOfScript = fso.GetParentFolderName(wscript.ScriptFullName) If fso.FileExists(PathOfScript & "\versionInfo.ini") then Set WshShell = WScript.CreateObject("WScript.Shell") ProgramPath = "C:\Program Files\Juniper Networks\.\dsNetworkConnect.exe" Version = fso.GetFileVersion(ProgramPath) If fso.FileExists(ProgramPath) then Wscript.echo "File Found, Running Program " & ProgramPath & " " Version WScript.sleep 1000 WshShell.Run ProgramPath WScript.sleep 10000 else Wscript.echo "Can't find dsNetworkConnect.exe" WScript.Quit end if else Wscript.echo "Can't find Version.ini" WScript.Quit end if Wscript.echo "Terminating Program" strComputer = "." Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colProcessList = objWMIService.ExecQuery ("SELECT * FROM Win32_Process WHERE Name = 'dsNetworkConnect.exe'") For Each objProcess in colProcessList objProcess.Terminate() Next Wscript.echo "Retrieving Username" Set objNet = CreateObject("WScript.NetWork") UserName = objNet.UserName Wscript.echo UserName PathOfIni = PathOfScript & "\versionInfo.ini" PathOfFile = "C:\Documents and Settings\" & UserName & "\Application Data\Juniper Networks\Network Connect " & Version & "\" Wscript.echo "Trying to copy file" fso.CopyFile PathOfIni, PathOfFile, True Wscript.echo "File copied" strServiceName = "dsNcService" Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name ='" & strServiceName & "'") For Each objService in colListOfServices objService.StopService() Next For Each objService in colListOfServices objService.StartService() Next Should help a bit, Steve
mac_shinobi Posted March 31, 2011 Author Posted March 31, 2011 (edited) Set fso = CreateObject("Scripting.FileSystemObject") PathOfScript = fso.GetParentFolderName(wscript.ScriptFullName) If fso.FileExists(PathOfScript & "\versionInfo.ini") then Set WshShell = WScript.CreateObject("WScript.Shell") ProgramPath = "C:\Program Files\Juniper Networks\.\dsNetworkConnect.exe" Version = fso.GetFileVersion(ProgramPath) If fso.FileExists(ProgramPath) then Wscript.echo "File Found, Running Program " & ProgramPath & " " Version WScript.sleep 1000 WshShell.Run ProgramPath WScript.sleep 10000 else Wscript.echo "Can't find dsNetworkConnect.exe" WScript.Quit end if else Wscript.echo "Can't find Version.ini" WScript.Quit end if Wscript.echo "Terminating Program" strComputer = "." Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colProcessList = objWMIService.ExecQuery ("SELECT * FROM Win32_Process WHERE Name = 'dsNetworkConnect.exe'") For Each objProcess in colProcessList objProcess.Terminate() Next Wscript.echo "Retrieving Username" Set objNet = CreateObject("WScript.NetWork") UserName = objNet.UserName Wscript.echo UserName PathOfIni = PathOfScript & "\versionInfo.ini" PathOfFile = "C:\Documents and Settings\" & UserName & "\Application Data\Juniper Networks\Network Connect " & Version & "\" Wscript.echo "Trying to copy file" fso.CopyFile PathOfIni, PathOfFile, True Wscript.echo "File copied" strServiceName = "dsNcService" Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name ='" & strServiceName & "'") For Each objService in colListOfServices objService.StopService() Next For Each objService in colListOfServices objService.StartService() Next Should help a bit, Steve Getting an error on line 12 - not sure how vbs counts the lines with the spaces or just the lines of code etc but when counting the lines of code it seems to be around the else command The If command seems a bit porked - too many else's without any if statements etc I tweaked the if statements to be : Set fso = CreateObject("Scripting.FileSystemObject") PathOfScript = fso.GetParentFolderName(wscript.ScriptFullName) If fso.FileExists(PathOfScript & "\versionInfo.ini") then Set WshShell = WScript.CreateObject("WScript.Shell") ProgramPath = "C:\Program Files\Juniper Networks\.\dsNetworkConnect.exe" Version = fso.GetFileVersion(ProgramPath) wscript.echo Version else Wscript.echo "Can't find Version.ini" WScript.Quit end if If fso.FileExists(ProgramPath) then Wscript.echo "File Found, Running Program " & ProgramPath & " " Version WScript.sleep 1000 WshShell.Run ProgramPath WScript.sleep 10000 else Wscript.echo "Can't find dsNetworkConnect.exe" WScript.Quit end if still erroring on the version line I think, putting the dot in the path I think is naffing it up ( possibly ) Edited March 31, 2011 by mac_shinobi
Steve21 Posted March 31, 2011 Posted March 31, 2011 The IFs are nested, so it looks weird, but I think I just missed a & Wscript.echo "File Found, Running Program " & ProgramPath & " " Version should be Wscript.echo "File Found, Running Program " & ProgramPath & " " & Version 1
mac_shinobi Posted March 31, 2011 Author Posted March 31, 2011 The IFs are nested, so it looks weird, but I think I just missed a & Wscript.echo "File Found, Running Program " & ProgramPath & " " Version should be Wscript.echo "File Found, Running Program " & ProgramPath & " " & Version Put it back to how it was and fixed the code line as you mention above by adding the extra ampersand symbol to append the version variable on the end and I get the error it can't find the dsnetworkconnect.exe
mac_shinobi Posted March 31, 2011 Author Posted March 31, 2011 Here is what I have now - some code editing done on my part : Set fso = CreateObject("Scripting.FileSystemObject") PathOfScript = fso.GetParentFolderName(wscript.ScriptFullName) If fso.FileExists(PathOfScript & "\versionInfo.ini") then Set WshShell = WScript.CreateObject("WScript.Shell") ProgramPath = "C:\Program Files\Juniper Networks\" Set objFolder = fso.GetFolder(ProgramPath) Set colSubfolders = objFolder.Subfolders For Each objSubfolder in colSubfolders If Left(objSubfolder.Name,7) = "Network" Then Wscript.Echo objSubfolder.Name Version = objSubfolder.Name ProgramPath = ProgramPath & objSubfolder.Name & "\dsNetworkConnect.exe" wscript.echo ProgramPath End If Next 'Version = fso.GetFileVersion(ProgramPath) If fso.FileExists(ProgramPath) then Wscript.echo "File Found, Running Program " & ProgramPath & " " & Version WScript.sleep 1000 wscript.echo ProgramPath WshShell.Exec ProgramPath WScript.sleep 10000 else Wscript.echo "Can't find dsNetworkConnect.exe" WScript.Quit end if else Wscript.echo "Can't find Version.ini" WScript.Quit end if Wscript.echo "Terminating Program" strComputer = "." Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colProcessList = objWMIService.ExecQuery ("SELECT * FROM Win32_Process WHERE Name = 'dsNetworkConnect.exe'") For Each objProcess in colProcessList objProcess.Terminate() Next Wscript.echo "Retrieving Username" Set objNet = CreateObject("WScript.NetWork") UserName = objNet.UserName Wscript.echo UserName PathOfIni = PathOfScript & "\versionInfo.ini" PathOfFile = "C:\Documents and Settings\" & UserName & "\Application Data\Juniper Networks\" & Version & "\" Wscript.echo "Trying to copy file" fso.CopyFile PathOfIni, PathOfFile, True Wscript.echo "File copied" strServiceName = "dsNcService" Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name ='" & strServiceName & "'") For Each objService in colListOfServices objService.StopService() Next For Each objService in colListOfServices objService.StartService() NextI can get rid of some of the wscript echo's / message boxes and put one at the end to advise said user it has finished and to re launch the app, I had to use exec instead of run as kept getting erorrs trying to use the run command ( wshshell.run ) Thanks for the help on neating the code
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now