Efficiency

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 14 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
spectre87
Posts: 103
Last visit: Thu Jul 18, 2013 9:53 am

Efficiency

Post by spectre87 »

The following script works fine, I just wonder if it could work more efficiently. Any ideas or is it fine the way it is?

Code: Select all

' Remote Script
set oFS = CreateObject("Scripting.FileSystemObject")
set oTS = oFS.OpenTextFile("serverdatabase.csv")
oFS.CopyFile ">srv1groupsIT GROUP DATAHQDRDR StartupPlannedStartupRemote.vbs", "srv1groupsIT GROUP DATAHQDRDR Startup"
' srv1groupsIT GROUP DATAHQDR
Do Until oTS.AtEndOfStream

Dim wshShell
' Set it up
set wshShell=CreateObject("wscript.shell")
'title for popup window
strTitle= "Script Status"
strLine = Trim(oTS.ReadLine)
arrSplit = Split(strLine, ",")
sServer = arrSplit(0)    'Server Name
sIPHQ = arrSplit(1)       'HQ IP Address
sIPDR = arrSplit(2)       'DR IP Address
sSubnetHQ = arrSplit(3)   'HQ Subnet
sSubnetDR = arrSplit(4)   'DR Subnet
sGatewayHQ = arrSplit(5)  'HQ Gateway
sGatewayDR = arrSplit(6)  'DR Gateway
sDNS1HQ = arrSplit(7)     'HQ DNS1
sDNS1DR = arrSplit(8)     'DR DNS1
sDNS2HQ = arrSplit(9)     'HQ DNS2
sDNS2DR = arrSplit(10)    'DR DNS2
sSVC1 = arrSplit(11)      'Service 1 - WMI - Will need to be in Final script!
sSVC2 = arrSplit(12)      'Service 2
sSVC3 = arrSplit(13)      'Service 3
sSVC4 = arrSplit(14)      'Service 4

' Get IP address for servers to display in the ping block
strcomputer = sServer
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!" & strComputer & "rootcimv2")
Set colItems = objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
For Each objitem in colitems
strIPAddress = Join(objitem.IPAddress, ",")
IP = stripaddress
Next

' Ping servers
Set CPingResults = GetObject ("winmgmts://./root/cimv2").ExecQuery("SELECT * " & "FROM Win32_PingStatus WHERE Address ='" & sServer & "'")
For Each oPingResult In CPingResults
 If oPingResult.StatusCode = 0 Then
  wshShell.Popup sServer & " resolves" & VbCrLf & "IP Address: " & IP,5,strTitle,vbOKOnly+vbInformation
   Else
    wshShell.Popup sServer & " did not resolve, run local script" & VbCrLf & "IP Address: " & IP,7,strTitle,vbOKOnly+vbInformation
  End If
 Next
 
' Start services
strComputer = sServer
' Bind services to WMI from csv
set oShell = CreateObject("WScript.Shell")
aServices = Array(sSVC1, sSVC2, sSVC3, sSVC4)
For Each sService In aServices
 oShell.Run "Net.exe start "&  Chr(34) & sService & Chr(34), 0, True
Next


' Run local script on servers that will run batch file
strComputer = sServer
strCommand = "cscript.exe " & sServer & "C$ScriptsStartup.vbs"
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!" & strComputer & "rootcimv2")
Set objProcess = objWMIService.Get("Win32_Process")
errReturn = objProcess.Create(strCommand, null, null, intProcessID)
 If errReturn = 0 Then
  wshShell.Popup "Local script on " & sServer & " has been initiated",3,strTitle,vbOKOnly+vbInformation
 Else
  wshShell.Popup "Local script on " & sServer,3,strTitle,vbOKOnly+vbInformation
 End If
 
Loop
User avatar
rasimmer
Posts: 182
Last visit: Fri Apr 25, 2014 7:00 am

Efficiency

Post by rasimmer »



I would recommend utilizing Option Explicit which will require you to declare your variables
Pick a naming convention. Is a string identified by s or str...
You had defined strComputer = sServer. Your not modifying the variable, so I changed your code to just use the sServer variable
Ensure you are indenting your code correctly. It's hard to tell in the code window, but thought I would mention it.
See comments in the code

Option Explicit' Remote Script
Dim oFS : Set oFS = WScript.CreateObject("Scripting.FileSystemObject")Dim oTs : Set oTS = oFS.OpenTextFile("serverdatabase.csv")Dim objWSHShell : Set objWSHShell = WScript.CreateObject("WScript.Shell")oFS.CopyFile "srv1groupsIT GROUP DATAHQDRDR StartupPlannedStartupRemote.vbs", "srv1groupsIT GROUP DATAHQDRDR Startup"' srv1groupsIT GROUP DATAHQDRDim strWindowsTitle: strWindowsTitle= "Script Status"
Do Until oTS.AtEndOfStream Dim strLine = Trim(oTS.ReadLine) Dim arrSplit : arrSplit = Split(strLine, ",") Dim sServer : sServer = arrSplit(0) 'Server Dim sNamesIPHQ : sNamesIPHQ = arrSplit(1) 'HQ IPs Address Dim sIPDR : sIPDR = arrSplit(2) 'DR IPs Address Dim sSubnetHQ : sSubnetHQ = arrSplit(3) 'HQ Subnet Dim sSubnetDR: sSubnetDR = arrSplit(4) 'DR Subnet Dim sGatewayHQ : sGatewayHQ = arrSplit(5) 'HQ Gateway Dim sGatewayDR : sGatewayDR = arrSplit(6) 'DR Gateway Dim sDNS1HQ : sDNS1HQ = arrSplit(7) 'HQ DNS1 Dim sDNS1DR : sDNS1DR = arrSplit(8) 'DR DNS1 Dim sDSN2HQ : sDNS2HQ = arrSplit(9) 'HQ DNS2 Dim sDNS2DR : sDNS2DR = arrSplit(10) 'DR DNS2 Dim sSVC1 : sSVC1 = arrSplit(11) 'Service 1 - WMI - Will need to be in Final script! Dim sSVC2 : sSVC2 = arrSplit(12) 'Service 2 Dim sSVC3 : sSVC3 = arrSplit(13) 'Service 3 Dim sSVC4 : sSVC4 = arrSplit(14) 'Service 4 ' Get IP address for servers to display in the ping block Dim objWMIService : Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!" & sServer & "rootcimv2") Dim colItems : Set colItems = objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True") Dim objItem For Each objitem in colitems Dim strIPAddress : strIPAddress = Join(objitem.IPAddress, ",") Dim IP : IP = stripaddress Next
' Ping servers Dim CPingResults : Set CPingResults = GetObject ("winmgmts://./root/cimv2").ExecQuery("SELECT * " & "FROM Win32_PingStatus WHERE Address ='" & sServer & "'") Dim oPingResult For Each oPingResult In CPingResults If oPingResult.StatusCode = 0 Then objWSHShell.Popup sServer & " resolves" & VbCrLf & "IP Address: " & IP,5,strTitle,vbOKOnly+vbInformation Else objWSHShell.Popup sServer & " did not resolve, run local script" & VbCrLf & "IP Address: " & IP,7,strTitle,vbOKOnly+vbInformation End If Next
' Start services ' Bind services to WMI from csv 'set oShell = CreateObject("WScript.Shell") You already created this object above Dim aServices : aServices = Array(sSVC1, sSVC2, sSVC3, sSVC4) Dim sService For Each sService In aServices oShell.Run "Net.exe start "& Chr(34) & sService & Chr(34), 0, True Next
' Run local script on servers that will run batch file
Dim strCommand : strCommand = "cscript.exe " & sServer & "C$ScriptsStartup.vbs" 'Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!" & sServer & "rootcimv2") You already created this object above On Error Resume Next
'You were attempting to capture a error here, but didn't have error handling enabled. Dim objProcess : Set objProcess = objWMIService.Get("Win32_Process") Dim errReturn : errReturn = objProcess.Create(strCommand, null, null, intProcessID) If errReturn = 0 Then objWSHShell.Popup "Local script on " & sServer & " has been initiated",3,strTitle,vbOKOnly+vbInformation Else objWSHShell.Popup "Local script on " & sServer,3,strTitle,vbOKOnly+vbInformation End If On Error GoTo 0Looprasimmer2009-12-04 10:56:07
User avatar
spectre87
Posts: 103
Last visit: Thu Jul 18, 2013 9:53 am

Efficiency

Post by spectre87 »

I am getting an error on line 11, char 18. Expected end of satement.

Line 11:

Code: Select all

Dim strLine = Trim(oTS.ReadLine)
User avatar
rasimmer
Posts: 182
Last visit: Fri Apr 25, 2014 7:00 am

Efficiency

Post by rasimmer »

Ooopss..

Dim strLine : strLine = Trim(oTS.ReadLine)
User avatar
spectre87
Posts: 103
Last visit: Thu Jul 18, 2013 9:53 am

Efficiency

Post by spectre87 »

I had to make a few more changes to the code you helped me with, but now it is 100%. Thank you again. I have highlighted the changes I made. I think I highlighted them all at least.

Code: Select all

Option Explicit
' Remote Script
Dim oFS : Set oFS = WScript.CreateObject("Scripting.FileSystemObject")
Dim oTs : Set oTS = oFS.OpenTextFile("serverdatabase.csv")
Dim objWSHShell : Set objWSHShell = WScript.CreateObject("WScript.Shell")
oFS.CopyFile "srv1groupsIT GROUP DATAHQDRDR StartupPlannedStartupRemote.vbs", "srv1groupsIT GROUP DATAHQDRDR Startup"
' srv1groupsIT GROUP DATAHQDR
Dim strWindowsTitle: strWindowsTitle= "Script Status"
Do Until oTS.AtEndOfStream
     
     Dim strLine : strLine = Trim(oTS.ReadLine)
     Dim arrSplit : arrSplit = Split(strLine, ",")
     Dim sServer : sServer = arrSplit(0) 'Server
     Dim sNamesIPHQ : sNamesIPHQ = arrSplit(1) 'HQ IPs Address
     Dim sIPDR : sIPDR = arrSplit(2) 'DR IPs Address
     Dim sSubnetHQ : sSubnetHQ = arrSplit(3) 'HQ Subnet
     Dim sSubnetDR: sSubnetDR = arrSplit(4) 'DR Subnet
     Dim sGatewayHQ : sGatewayHQ = arrSplit(5) 'HQ Gateway
     Dim sGatewayDR : sGatewayDR = arrSplit(6) 'DR Gateway
     Dim sDNS1HQ : sDNS1HQ = arrSplit(7) 'HQ DNS1
     Dim sDNS1DR : sDNS1DR = arrSplit(8) 'DR DNS1
     Dim sDNS2HQ : sDNS2HQ = arrSplit(9) 'HQ DNS2
     Dim sDNS2DR : sDNS2DR = arrSplit(10) 'DR DNS2
     Dim sSVC1 : sSVC1 = arrSplit(11) 'Service 1 - WMI - Will need to be in Final script!
     Dim sSVC2 : sSVC2 = arrSplit(12) 'Service 2
     Dim sSVC3 : sSVC3 = arrSplit(13) 'Service 3
     Dim sSVC4 : sSVC4 = arrSplit(14) 'Service 4
    
    
    Dim objWMIService : Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!" & sServer & "rootcimv2")
    Dim colItems : Set colItems = objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
    Dim objItem
    For Each objitem in colitems
        Dim strIPAddress : strIPAddress = Join(objitem.IPAddress, ",")
        Dim IP : IP = stripaddress
    Next
    ' Ping servers
    Dim CPingResults : Set CPingResults = GetObject ("winmgmts://./root/cimv2").ExecQuery("SELECT * " & "FROM Win32_PingStatus WHERE Address ='" & sServer & "'")
    Dim oPingResult
    For Each oPingResult In CPingResults
        If oPingResult.StatusCode = 0 Then
            objWSHShell.Popup sServer & " resolves" & VbCrLf & "IP Address: " & IP,5,strWindowsTitle,vbOKOnly+vbInformation
        Else
            objWSHShell.Popup sServer & " did not resolve, run local script" & VbCrLf & "IP Address: " & IP,7,strWindowsTitle,vbOKOnly+vbInformation
        End If
    Next
    ' Start services
    ' Bind services to WMI from csv
    'set oShell = CreateObject("WScript.Shell") You already created this object above
    Dim aServices : aServices = Array(sSVC1, sSVC2, sSVC3, sSVC4)
    Dim sService
    For Each sService In aServices
        objWSHShell.Run "Net.exe start "&  Chr(34) & sService & Chr(34), 0, True
    Next
' Run local script on servers that will run batch file
    Dim strCommand : strCommand = "cscript.exe " & sServer & "C$ScriptsStartup.vbs"
    'Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!" & sServer & "rootcimv2") You already created this object above

    On Error Resume Next
    'You were attempting to capture a error here, but didn't have error handling enabled.
    Dim objProcess : Set objProcess = objWMIService.Get("Win32_Process")
    Dim errReturn : errReturn = objProcess.Create(strCommand, null, null, intProcessID)
    If errReturn = 0 Then
        objWSHShell.Popup "Local script on " & sServer & " has been initiated",3,strWindowsTitle,vbOKOnly+vbInformation
    Else
        objWSHShell.Popup "Local script on " & sServer,3,strWindowsTitle,vbOKOnly+vbInformation
    End If
    On Error GoTo 0
Loop
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Efficiency

Post by jvierra »

It is VB code. VBScript is a subset of VB. You need to get Visual Studio 6 from somewhere.

http://msdn.microsoft.com/en-us/vbasic/default.aspx

YOu could compile in VB.NET but would have a big conversion job. YOu probably need to use a programmer to help with this. This rask is normally outside the realm of this forum.

Whyt would you want to convert it to VB anyway? VB won't run any faster or be any more reliable thanthe vbscript code.


User avatar
spectre87
Posts: 103
Last visit: Thu Jul 18, 2013 9:53 am

Efficiency

Post by spectre87 »

The reason for it is simple, GUI. I am a C# programmer by trade and a vb dabbler. I have already made the vb application, but can't get the output the same.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Efficiency

Post by jvierra »

You say this is a remote script? If ist is to run remotely then it cannot have a GUI or it will fail under most circumstances.

User avatar
spectre87
Posts: 103
Last visit: Thu Jul 18, 2013 9:53 am

Efficiency

Post by spectre87 »

The only way I can keep alive without failing when being ran remotely is maybe to give it an error handling feature from heck. I would have to do a lot of automation in the administration part of the application, huh?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Efficiency

Post by jvierra »

As best I can see the script runs a remote script - startup.vbs - but it is running it incorrectly as it is running it from a share and should - must - run locally or you will get errors.

The remote script is not specified so we don't have a clue what is happening.

The code that you have checks for services on remote server but attempts to run them on local server.

You use NET and commandline utilities to do things that can be more reliably done using WMI. Nothing I see requires a command line or a remoting capability. Just use WMI for all.

I suspect the most of you "startup" script does not need to be run the way you are running.

THe code designshows a significant lack of knowledge of WMI and WIndows management scripting techniques. I bet itf you started over with a good basic spec this could be written in a few lines of simple code.

Consider, as a programmer, that the use of functions (subroutines) would be much cleaner and easier.

Use Win32_Process to start and check processes not NET START.

Consider using an HTA to provide a GUI. It will be easier to maintain in the future. Compiled code is not needed here.

This topic is 14 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked