+ Post New Thread
Results 1 to 14 of 14
Scripts Thread, vb script help please in Coding and Web Development; Basically want to neaten the below chunk of code up as much as possible and it needs to do the ...
  1. #1

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,710
    Thank Post
    3,246
    Thanked 1,048 Times in 970 Posts
    Rep Power
    364

    vb script help please

    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\<USERNAME>\Application Data\Juniper Networks\Network Connect 6.3.0

    Again where <USERNAME> 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



    Code:
    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
    Last edited by mac_shinobi; 31st March 2011 at 10:15 AM.

  2. #2

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,689
    Thank Post
    334
    Thanked 515 Times in 483 Posts
    Rep Power
    179
    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
    Last edited by Steve21; 31st March 2011 at 10:38 AM.

  3. #3

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,710
    Thank Post
    3,246
    Thanked 1,048 Times in 970 Posts
    Rep Power
    364
    Quote Originally Posted by Steve21 View Post
    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

  4. #4

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,689
    Thank Post
    334
    Thanked 515 Times in 483 Posts
    Rep Power
    179
    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

  5. #5

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,710
    Thank Post
    3,246
    Thanked 1,048 Times in 970 Posts
    Rep Power
    364
    Quote Originally Posted by Steve21 View Post
    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 !

  6. #6

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,689
    Thank Post
    334
    Thanked 515 Times in 483 Posts
    Rep Power
    179
    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

  7. Thanks to Steve21 from:

    mac_shinobi (31st March 2011)

  8. #7

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,689
    Thank Post
    334
    Thanked 515 Times in 483 Posts
    Rep Power
    179
    Try this, to see if it works ok. Works here but I was basing it on notepad running :P 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 :P 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 :P

    But yeah gimme a shout, somethings bound to break :'(

    Code:
    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 :P Just seemed easier to restart

    Steve
    Last edited by Steve21; 31st March 2011 at 11:35 AM.

  9. #8

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,710
    Thank Post
    3,246
    Thanked 1,048 Times in 970 Posts
    Rep Power
    364
    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 )

    Code:
    Set WshShell = WScript.CreateObject("WScript.Shell")
    ProgramPath = "C:\Program Files\Juniper Networks\.\dsNetworkConnect.exe"
    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 ??
    Last edited by mac_shinobi; 31st March 2011 at 11:43 AM.

  10. #9

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,689
    Thank Post
    334
    Thanked 515 Times in 483 Posts
    Rep Power
    179
    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.

  11. #10

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,689
    Thank Post
    334
    Thanked 515 Times in 483 Posts
    Rep Power
    179
    Code:
    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

  12. #11

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,710
    Thank Post
    3,246
    Thanked 1,048 Times in 970 Posts
    Rep Power
    364
    Quote Originally Posted by Steve21 View Post
    Code:
    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 :

    Code:
    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 )
    Attached Images Attached Images
    Last edited by mac_shinobi; 31st March 2011 at 12:10 PM.

  13. #12

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,689
    Thank Post
    334
    Thanked 515 Times in 483 Posts
    Rep Power
    179
    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

  14. Thanks to Steve21 from:

    mac_shinobi (31st March 2011)

  15. #13

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,710
    Thank Post
    3,246
    Thanked 1,048 Times in 970 Posts
    Rep Power
    364
    Quote Originally Posted by Steve21 View Post
    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

  16. #14

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,710
    Thank Post
    3,246
    Thanked 1,048 Times in 970 Posts
    Rep Power
    364
    Here is what I have now - some code editing done on my part :

    Code:
    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()
    Next
    I 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

SHARE:
+ Post New Thread

Similar Threads

  1. Script
    By shafia2009 in forum Windows Server 2000/2003
    Replies: 15
    Last Post: 20th February 2011, 10:01 PM
  2. What Script?
    By mossj in forum Web Development
    Replies: 10
    Last Post: 28th March 2010, 12:15 AM
  3. Need a Script if Possible
    By Paul_L in forum Scripts
    Replies: 6
    Last Post: 2nd October 2008, 02:33 PM
  4. Logon script & Printer script question.
    By Galway in forum Windows
    Replies: 3
    Last Post: 29th August 2007, 11:00 AM
  5. Does anyone have a script...
    By wesleyw in forum Scripts
    Replies: 4
    Last Post: 5th July 2007, 01:58 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •