Creating a batch or a script to stop services on one server, set them to manual, start them on another server, set them to automatic, modify a word in a file in 3 different servers.

As the title of the question explained:
The following are the task i woul like to achive:
Stop 3 services on one server and set them to Manual
Start the same services on another server and set them to automatic,
Replace the "server name" in a config file in 3 different servers.

Is this doable through a batch file or a script?
winpro2000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobSampsonCommented:
Hi, yes, this is possible.  Try this script:

'================================
If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
    strPath = Wscript.ScriptFullName
    strCommand = "%comspec% /k cscript  """ & strPath & """"
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run(strCommand), 1, True
    Wscript.Quit
End If

' Note: strService is case sensitive.
arrServices = Array("Print Spooler","Windows Audio","VNC Server")

' Server to stop services on
strServer1 = "D09790RING"

' Server to start service on
strServer2 = "D09790RING"

If Ping(strServer1) = True Then
      For Each strService In arrServices
            Set objWMIService = GetObject("winmgmts:" _
                  & "{impersonationLevel=impersonate}!\\" _
                  & strServer1 & "\root\cimv2")
            Set colListOfServices = objWMIService.ExecQuery _
                  ("Select * from Win32_Service Where DisplayName='"& strService & "'")
            On Error Resume Next
            For Each objService In colListOfServices
                  If Err.Number = 0 Then
                        On Error GoTo 0
                        objService.StopService()
                        WScript.Echo ""
                        WScript.Echo strService & " service stopped on " & strServer1
                        WScript.Sleep 2000
                        objService.ChangeStartMode "Manual"
                        WScript.Echo strService & " service StartMode changed to Manual."
                        WScript.Sleep 2000
                  Else
                        Err.Clear
                        On Error GoTo 0
                        WScript.Echo strService & " service was not found on " & strServer1
                  End If
            Next
      Next
Else
      WScript.Echo ""
      WScript.Echo strServer1 & " could not be pinged."
End If

If Ping(strServer2) = True Then
      For Each strService In arrServices
            Set objWMIService = GetObject("winmgmts:" _
                  & "{impersonationLevel=impersonate}!\\" _
                  & strServer2 & "\root\cimv2")
            Set colListOfServices = objWMIService.ExecQuery _
                  ("Select * from Win32_Service Where DisplayName='"& strService & "'")
            On Error Resume Next
            For Each objService In colListOfServices
                  If Err.Number = 0 Then
                        On Error GoTo 0
                        objService.ChangeStartMode "Automatic"
                        WScript.Echo ""
                        WScript.Echo strService & " service StartMode changed to Automatic."
                        WScript.Sleep 2000
                        objService.StartService()
                        WScript.Echo strService & " service started on " & strServer2
                  Else
                        Err.Clear
                        On Error GoTo 0
                        WScript.Echo strService & " service was not found on " & strServer2
                  End If
            Next
      Next
Else
      WScript.Echo ""
      WScript.Echo strServer2 & " could not be pinged."
End If
WScript.Echo ""
WScript.Echo "Script finished."

'========= Now replace servername in a config file =================
arrServers = Array("D09790RING","D09790RING","D09790RING")
strFileName = "C:\Temp\Temp\Test Script\TextFile.txt"
strReplaceWhat = "the"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
For Each strServer In arrServers
      strReplaceWith = strServer
      strFile = "\\" & strServer & "\" & Replace(strFileName, ":", "$")
      If objFSO.FileExists("\\" & strServer & "\" & Replace(strFileName, ":", "$")) = True Then
            Set objFile = objFSO.OpenTextFile(strFile, intForReading, False)
            strContents = Replace(objFile.ReadAll, strReplaceWhat, strReplaceWith)
            objFile.Close
            Set objFile = objFSO.CreateTextFile(strFile, True)
            objFile.Write strContents
            objFile.Close
            Set objFile = Nothing
            WScript.Echo strFile & " has been processed."
      Else
            WScript.Echo "Could not find " & strFile
      End If
Next

'==============

Function Ping(strComputer)
      Dim objShell, boolCode
      Set objShell = CreateObject("WScript.Shell")
      boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
      If boolCode = 0 Then
            Ping = True
      Else
            Ping = False
      End If
End Function
'================================


Regards,

Rob.
0
winpro2000Author Commented:
This will be too complicated for the support team to understand. Pls they will have to manually modify the script with the server name every month.
I would go with 3 seperate files:
1. Stop the 3 services on the first server and set the services to run manually from Automatic.
2. Replace the server name in a config file from server1 to server2 or from server2 to server1
3.Start the 3 services on the second server and set the services to run Automatic from manually.

Is this doable?

Thanks in advance
0
RobSampsonCommented:
There's not much that you need to change.  The only variables you need to worry about are:

arrServices = Array("Print Spooler","Windows Audio","VNC Server")
strServer1 = "D09790RING"   ' to stop services
strServer2 = "D09790RING"   ' to start services
arrServers = Array("D09790RING","D09790RING","D09790RING")    ' to change the config file on
strFileName = "C:\Temp\Temp\Test Script\TextFile.txt"   ' name of the config file
strReplaceWhat = "the"    ' text in the config file to replace

The strReplaceWhat text will always get replace with the server name it is changing it on, but if you don't want that, also change:

strReplaceWith = strServer


If you really want them in separate files, I can do that tomorrow.

Regards,

Rob.
0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

winpro2000Author Commented:
Rob,
I think it is better to split it into 3 files. Also is there an easy way were the support person will not have to make any modifications at all. Were he wil only double click on the script on each server.
EA
0
Jared LukerCommented:
You could change the variables to pop up a box for input:

Change
strServer1 = "D09790RING"
to
strserver1 = InputBox("Input Server Name","Stop Services")

That will let your people put in the server they want to deal with and will store whatever they type in as the strServer1 variable.
0
winpro2000Author Commented:
How can all the script work then?? i have to understand it before i present it .....
What do i need, or they need to have to run this utility??
Thanks in advance.

Ehab

0
RobSampsonCommented:
I will work on getting the one script to prompt the user which servers to work with.  One question though, will you want the services to be input as well, or will they always be the same?

Regards,

Rob.
0
RobSampsonCommented:
Try this script:

'================================
If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
    strPath = Wscript.ScriptFullName
    strCommand = "%comspec% /k cscript  """ & strPath & """"
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run(strCommand), 1, True
    Wscript.Quit
End If

' Note: strService is case sensitive.
strServices = "Print Spooler,Windows Audio,VNC Server"
arrServices = Split(strServices, ",")

' Server to stop services on
strStopServers = InputBox("Please enter a comma separated list of servers to STOP the following services on:" & _
      VbCrLf & strServices & VbCrLf & VbCrLf & "EG: server1,server2", "Stop Services on These Servers")
arrStopServers = Split(strStopServers, ",")

' Server to start service on
strStartServers = InputBox("Please enter a comma separated list of servers to START the following services on:" & _
      VbCrLf & strServices & VbCrLf & VbCrLf & "EG: server1,server2", "Start Services on These Servers")
arrStartServers = Split(strStartServers, ",")

' Specify the config file path to modify
strConfigFile = InputBox("Please enter the full path to the config file to modify:", "Config File to Modify")

' Text in the file to replace
strReplaceWhat = InputBox("Please enter the text to be replaced in " & VbCrLf & strConfigFile & VbCrLf & _
      "with each server name.", "Text to Replace")

' Server to modify config file on
strConfigServers = InputBox("Please enter a comma separated list of servers to modify the following file on:" & _
      VbCrLf & strConfigFile & VbCrLf & VbCrLf & "EG: server1,server2", "Servers to Modify File on")
arrConfigServers = Split(strConfigServers, ",")

For Each strServer1 In arrStopServers
      If Ping(strServer1) = True Then
            For Each strService In arrServices
                  Set objWMIService = GetObject("winmgmts:" _
                        & "{impersonationLevel=impersonate}!\\" _
                        & strServer1 & "\root\cimv2")
                  Set colListOfServices = objWMIService.ExecQuery _
                        ("Select * from Win32_Service Where DisplayName='"& strService & "'")
                  On Error Resume Next
                  For Each objService In colListOfServices
                        If Err.Number = 0 Then
                              On Error GoTo 0
                              objService.StopService()
                              WScript.Echo ""
                              WScript.Echo strService & " service stopped on " & strServer1
                              WScript.Sleep 2000
                              objService.ChangeStartMode "Manual"
                              WScript.Echo strService & " service StartMode changed to Manual."
                              WScript.Sleep 2000
                        Else
                              Err.Clear
                              On Error GoTo 0
                              WScript.Echo strService & " service was not found on " & strServer1
                        End If
                  Next
            Next
      Else
            WScript.Echo ""
            WScript.Echo strServer1 & " could not be pinged."
      End If
Next

For Each strServer2 In arrStartServers
      If Ping(strServer2) = True Then
            For Each strService In arrServices
                  Set objWMIService = GetObject("winmgmts:" _
                        & "{impersonationLevel=impersonate}!\\" _
                        & strServer2 & "\root\cimv2")
                  Set colListOfServices = objWMIService.ExecQuery _
                        ("Select * from Win32_Service Where DisplayName='"& strService & "'")
                  On Error Resume Next
                  For Each objService In colListOfServices
                        If Err.Number = 0 Then
                              On Error GoTo 0
                              objService.ChangeStartMode "Automatic"
                              WScript.Echo ""
                              WScript.Echo strService & " service StartMode changed to Automatic."
                              WScript.Sleep 2000
                              objService.StartService()
                              WScript.Echo strService & " service started on " & strServer2
                        Else
                              Err.Clear
                              On Error GoTo 0
                              WScript.Echo strService & " service was not found on " & strServer2
                        End If
                  Next
            Next
      Else
            WScript.Echo ""
            WScript.Echo strServer2 & " could not be pinged."
      End If
Next

WScript.Echo ""

'========= Now replace servername in a config file =================
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
For Each strServer In arrConfigServers
      strReplaceWith = strServer
      strFile = "\\" & strServer & "\" & Replace(strConfigFile, ":", "$")
      If objFSO.FileExists(strFile) = True Then
            Set objFile = objFSO.OpenTextFile(strFile, intForReading, False)
            strContents = Replace(objFile.ReadAll, strReplaceWhat, strReplaceWith)
            objFile.Close
            Set objFile = objFSO.CreateTextFile(strFile, True)
            objFile.Write strContents
            objFile.Close
            Set objFile = Nothing
            WScript.Echo strFile & " has been processed."
      Else
            WScript.Echo "Could not find " & strFile
      End If
Next

WScript.Echo ""
WScript.Echo "Script finished."
'==============

Function Ping(strComputer)
      Dim objShell, boolCode
      Set objShell = CreateObject("WScript.Shell")
      boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
      If boolCode = 0 Then
            Ping = True
      Else
            Ping = False
      End If
End Function
'================================

Regards,

Rob.
0
winpro2000Author Commented:
Rob, Thanks a bunch!! I thing i am getting very close...
The only thing i want the support team to do is to enter the server name they want to switch to and that same server they will input it will become the word that will be changed too in the file on the other 3 servers
Also the location of the file is always the same, but it is on 3 seperate servers. So again:
1 server with 3 applications to stop and being set to manual.
a second server with 3 applications to start and to be set to automatic.
3 servers with a file on the same location on all 3 that will be modified with the servers name that all 3 services started on.

I have also tested the script on a vista machine but i can't ping my own laptop for some reason! So the script wouldn't work.

0
RobSampsonCommented:
If you can't ping your own laptop, then you might have a firewall on or something....try turning that off.

I have changed the code so you only enter one server the services to stop, and one server for the services to start.  For the "servers to modify the file on", you can specify as many as you want.

Now, the last question I have (which I will assume the answer to, and will make the code that way), is "which" word in the config file do you want to change *to* the new server name?  I'm assuming that you want to change the server you stopped them on, to the server you started them on....

Oh, and to hard code the config file, change this line:
strConfigFile = "C:\Temp\Config.cfg"

'================================
If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
    strPath = Wscript.ScriptFullName
    strCommand = "%comspec% /k cscript  """ & strPath & """"
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run(strCommand), 1, True
    Wscript.Quit
End If

' Note: strService is case sensitive.
strServices = "Print Spooler,Windows Audio,VNC Server"
arrServices = Split(strServices, ",")

' Server to stop services on
strStopServers = InputBox("Please enter the server to STOP the following services on:" & _
      VbCrLf & strServices, "Stop Services on This Server")
arrStopServers = Split(strStopServers, ",")

' Server to start service on
strStartServers = InputBox("Please enter the server to START the following services on:" & _
      VbCrLf & strServices, "Start Services on This Server")
arrStartServers = Split(strStartServers, ",")

' Specify the config file path to modify
strConfigFile = "C:\Temp\Config.cfg"
'strConfigFile = InputBox("Please enter the full path to the config file to modify:", "Config File to Modify")

' Text in the file to Replace
strReplaceWhat = strStopServers
'strReplaceWhat = InputBox("Please enter the text to be replaced in " & VbCrLf & strConfigFile & VbCrLf & _
'      "with each server name.", "Text to Replace")
strReplaceWith = strStartServers

' Server to modify config file on
strConfigServers = InputBox("Please enter a comma separated list of servers to modify the following file on:" & _
      VbCrLf & strConfigFile & VbCrLf & VbCrLf & "EG: server1,server2", "Servers to Modify File on")
arrConfigServers = Split(strConfigServers, ",")

For Each strServer1 In arrStopServers
      If Ping(strServer1) = True Then
            For Each strService In arrServices
                  Set objWMIService = GetObject("winmgmts:" _
                        & "{impersonationLevel=impersonate}!\\" _
                        & strServer1 & "\root\cimv2")
                  Set colListOfServices = objWMIService.ExecQuery _
                        ("Select * from Win32_Service Where DisplayName='"& strService & "'")
                  On Error Resume Next
                  For Each objService In colListOfServices
                        If Err.Number = 0 Then
                              On Error GoTo 0
                              objService.StopService()
                              WScript.Echo ""
                              WScript.Echo strService & " service stopped on " & strServer1
                              WScript.Sleep 2000
                              objService.ChangeStartMode "Manual"
                              WScript.Echo strService & " service StartMode changed to Manual."
                              WScript.Sleep 2000
                        Else
                              Err.Clear
                              On Error GoTo 0
                              WScript.Echo strService & " service was not found on " & strServer1
                        End If
                  Next
            Next
      Else
            WScript.Echo ""
            WScript.Echo strServer1 & " could not be pinged."
      End If
Next

For Each strServer2 In arrStartServers
      If Ping(strServer2) = True Then
            For Each strService In arrServices
                  Set objWMIService = GetObject("winmgmts:" _
                        & "{impersonationLevel=impersonate}!\\" _
                        & strServer2 & "\root\cimv2")
                  Set colListOfServices = objWMIService.ExecQuery _
                        ("Select * from Win32_Service Where DisplayName='"& strService & "'")
                  On Error Resume Next
                  For Each objService In colListOfServices
                        If Err.Number = 0 Then
                              On Error GoTo 0
                              objService.ChangeStartMode "Automatic"
                              WScript.Echo ""
                              WScript.Echo strService & " service StartMode changed to Automatic."
                              WScript.Sleep 2000
                              objService.StartService()
                              WScript.Echo strService & " service started on " & strServer2
                        Else
                              Err.Clear
                              On Error GoTo 0
                              WScript.Echo strService & " service was not found on " & strServer2
                        End If
                  Next
            Next
      Else
            WScript.Echo ""
            WScript.Echo strServer2 & " could not be pinged."
      End If
Next

WScript.Echo ""

'========= Now replace servername in a config file =================
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
For Each strServer In arrConfigServers
      'strReplaceWith = strServer
      strFile = "\\" & strServer & "\" & Replace(strConfigFile, ":", "$")
      If objFSO.FileExists(strFile) = True Then
            Set objFile = objFSO.OpenTextFile(strFile, intForReading, False)
            strContents = Replace(objFile.ReadAll, strReplaceWhat, strReplaceWith)
            objFile.Close
            Set objFile = objFSO.CreateTextFile(strFile, True)
            objFile.Write strContents
            objFile.Close
            Set objFile = Nothing
            WScript.Echo strFile & " has been processed."
      Else
            WScript.Echo "Could not find " & strFile
      End If
Next

WScript.Echo ""
WScript.Echo "Script finished."
'==============

Function Ping(strComputer)
      Dim objShell, boolCode
      Set objShell = CreateObject("WScript.Shell")
      boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
      If boolCode = 0 Then
            Ping = True
      Else
            Ping = False
      End If
End Function
'================================

Regards,

Rob.
0
winpro2000Author Commented:
Rob,

I will try it again tonight and let you know hoe did it go!
0
winpro2000Author Commented:
Rob,
Everything works except the config file doesn't get modified. You think i am missing something?
The servers are on the same domain i am running the script from and i do have permission to modify the file.

0
RobSampsonCommented:
OK, so have you set this line:
strConfigFile = "C:\Temp\Config.cfg"

to the *local* path location to the file on each server?  Bear in mind this only allows for the file to be in the same location on each server.

For example, the line above would point to the C:\Temp\Config.cfg file on each server.  And it will replace whatever string is in the strStopServers with the string in strStartServers.

Also note, that when the script is accessing this file, it uses the admin share of the remote computers' drive to get to it.  So, using the example above, you should be able to access the file via
\\servername\c$\temp\config.cfg

Hope that helps.

Regards,

Rob.
0
winpro2000Author Commented:
I can i access the file through browsing the location. Also the file is located on the V drive on all 3 servers. Is that ok?

Regards,

EA
0
RobSampsonCommented:
Is V Drive a physical drive?  Can you access the file via:
\\computername\V$\folder\file.cfg

That is, does the V Drive have a share on it of V$?

If that's all OK, then strConfigFile like this should be OK:
strConfigFile = "V:\folder\file.cfg"

Regards,

Rob.
0
winpro2000Author Commented:
V is a physical drive
i can go to \\servername\v$

But the file doesn't get changed..

0
RobSampsonCommented:
Do you see either of these messages:
WScript.Echo strFile & " has been processed."

or
WScript.Echo "Could not find " & strFile

If you see the first one, then the file gets opened, otherwise it can't find the file to that path.

So, if you see the first one, perhaps it's a case sensitivity issue.

Just so I am sure, please post the line from the file that you actually want to change, as well as what you want to change it to (for example).

If it is a case sensitivity issue, I'll change it so that it doesn't care about that....

Regards,

Rob.
0
RobSampsonCommented:
Try this, it should be a case insensitive replacement:

'================================
If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
    strPath = Wscript.ScriptFullName
    strCommand = "%comspec% /k cscript  """ & strPath & """"
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run(strCommand), 1, True
    Wscript.Quit
End If

' Note: strService is case sensitive.
strServices = "Print Spooler,Windows Audio,VNC Server"
arrServices = Split(strServices, ",")

' Server to stop services on
strStopServers = InputBox("Please enter the server to STOP the following services on:" & _
      VbCrLf & strServices, "Stop Services on This Server")
arrStopServers = Split(strStopServers, ",")

' Server to start service on
strStartServers = InputBox("Please enter the server to START the following services on:" & _
      VbCrLf & strServices, "Start Services on This Server")
arrStartServers = Split(strStartServers, ",")

' Specify the config file path to modify
strConfigFile = "C:\Temp\Temp\Test Script\ZConfig.txt"
'strConfigFile = InputBox("Please enter the full path to the config file to modify:", "Config File to Modify")

' Text in the file to Replace
strReplaceWhat = strStopServers
'strReplaceWhat = InputBox("Please enter the text to be replaced in " & VbCrLf & strConfigFile & VbCrLf & _
'      "with each server name.", "Text to Replace")
strReplaceWith = strStartServers

' Server to modify config file on
strConfigServers = InputBox("Please enter a comma separated list of servers to modify the following file on:" & _
      VbCrLf & strConfigFile & VbCrLf & VbCrLf & "EG: server1,server2", "Servers to Modify File on")
arrConfigServers = Split(strConfigServers, ",")

For Each strServer1 In arrStopServers
      If Ping(strServer1) = True Then
            For Each strService In arrServices
                  Set objWMIService = GetObject("winmgmts:" _
                        & "{impersonationLevel=impersonate}!\\" _
                        & strServer1 & "\root\cimv2")
                  Set colListOfServices = objWMIService.ExecQuery _
                        ("Select * from Win32_Service Where DisplayName='"& strService & "'")
                  On Error Resume Next
                  For Each objService In colListOfServices
                        If Err.Number = 0 Then
                              On Error GoTo 0
                              objService.StopService()
                              WScript.Echo ""
                              WScript.Echo strService & " service stopped on " & strServer1
                              WScript.Sleep 2000
                              objService.ChangeStartMode "Manual"
                              WScript.Echo strService & " service StartMode changed to Manual."
                              WScript.Sleep 2000
                        Else
                              Err.Clear
                              On Error GoTo 0
                              WScript.Echo strService & " service was not found on " & strServer1
                        End If
                  Next
            Next
      Else
            WScript.Echo ""
            WScript.Echo strServer1 & " could not be pinged."
      End If
Next

For Each strServer2 In arrStartServers
      If Ping(strServer2) = True Then
            For Each strService In arrServices
                  Set objWMIService = GetObject("winmgmts:" _
                        & "{impersonationLevel=impersonate}!\\" _
                        & strServer2 & "\root\cimv2")
                  Set colListOfServices = objWMIService.ExecQuery _
                        ("Select * from Win32_Service Where DisplayName='"& strService & "'")
                  On Error Resume Next
                  For Each objService In colListOfServices
                        If Err.Number = 0 Then
                              On Error GoTo 0
                              objService.ChangeStartMode "Automatic"
                              WScript.Echo ""
                              WScript.Echo strService & " service StartMode changed to Automatic."
                              WScript.Sleep 2000
                              objService.StartService()
                              WScript.Echo strService & " service started on " & strServer2
                        Else
                              Err.Clear
                              On Error GoTo 0
                              WScript.Echo strService & " service was not found on " & strServer2
                        End If
                  Next
            Next
      Else
            WScript.Echo ""
            WScript.Echo strServer2 & " could not be pinged."
      End If
Next

WScript.Echo ""

'========= Now replace servername in a config file =================
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
For Each strServer In arrConfigServers
      'strReplaceWith = strServer
      strFile = "\\" & strServer & "\" & Replace(strConfigFile, ":", "$")
      If objFSO.FileExists(strFile) = True Then
            Set objFile = objFSO.OpenTextFile(strFile, intForReading, False)
            strContents = objFile.ReadAll
            intStart = InStr(LCase(strContents), LCase(strReplaceWhat))
            intCount = 0
            While intStart > 0 And intStart < Len(strContents)
                  intCount = intCount + 1
                  WScript.Echo strReplaceWhat & " was found in " & strFile & " " & intCount & " time(s)."
                  strContents = Left(strContents, intStart - 1) & strReplaceWith & Mid(strContents, intStart + Len(strReplaceWhat))
                  intStart = InStr(intStart + 1, LCase(strContents), LCase(strReplaceWhat))
            Wend
            objFile.Close
            Set objFile = objFSO.CreateTextFile(strFile, True)
            objFile.Write strContents
            objFile.Close
            Set objFile = Nothing
            WScript.Echo strFile & " has been processed."
      Else
            WScript.Echo "Could not find " & strFile
      End If
Next

WScript.Echo ""
WScript.Echo "Script finished."
'==============

Function Ping(strComputer)
      Dim objShell, boolCode
      Set objShell = CreateObject("WScript.Shell")
      boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
      If boolCode = 0 Then
            Ping = True
      Else
            Ping = False
      End If
End Function
'================================

Regards,

Rob.
0
winpro2000Author Commented:
Rob,
One last request...
Can we split the script into 2 seperate scripts were the support team doesn't need to do anything but double clicking on the the script that will stop services and set them to Manual. These services will be  indicated in the script. And a second script that will start all 3 services and set them to Automatic. These services will be indicated in the script also.
The last portion of the file that need to be modified we don't need to worry about.

And this will be my last request!

Thanks in advance.
0
winpro2000Author Commented:
I just found out that i will need 2 more scripts that will have the name of the active server. It could be the same script. Each one will be pointing to both servers. Depends on which server will be active, we will run that particular scipt.
Does that make sense to you?
0
RobSampsonCommented:
Sorry, no, not really.  Could you provide an example in terms of ServerA and ServerB, etc...

Regards,

Rob.
0
winpro2000Author Commented:
Your last scipt worked fine except they don't want to input any information to the script. In other words, no pop up messages and i will enter the server name in the script:
Script 1:
Stop service A,B,C  and set the service to manuel

Script 2:
Start services A,B,C and set the services to Automatic

Script 3:
Modify the file .config with Server A
I will modify the line where the server name is located.
Also i want to use UNC path rather than c:\file name to \\ServerA\c$\file name

Script 4:
Modify the file .config with Server B
I will modify the line where the server name is located.
Also i want to use UNC path rather than c:\file name to \\ServerA\c$\file name

Hope i explained myself well.
0
winpro2000Author Commented:
Rob,
I think i got it to work, please see below:

This is the script i used to do what i am looking for, and it works. My only concern is the servers where the conf file has to be modified are 3 different servers where this script i only had a place to enter only one server name.. Where can i replace the serverDev with Server Prod 1, Server Prod 2, Server Prod 3?

'================================
If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
    strPath = Wscript.ScriptFullName
    strCommand = "%comspec% /k cscript  """ & strPath & """"
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run(strCommand), 1, True
    Wscript.Quit
End If

' Note: strService is case sensitive.
strServices = "Service1, Service2, Service3"
arrServices = Split(strServices, ",")

' Server to stop services on
strStopServers = "Server 1"
arrStopServers = Split(strStopServers, ",")

' Server to start service on
strStartServers = "Server 2"
arrStartServers = Split(strStartServers, ",")

' Specify the config file path to modify
strConfigFile = "v:\Program Files\test1\test2\file.exe.config"
'strConfigFile = InputBox("Please enter the full path to the config file to modify:", "Config File to Modify")

' Text in the file to Replace
strReplaceWhat = strStopServers
'strReplaceWhat = InputBox("Please enter the text to be replaced in " & VbCrLf & strConfigFile & VbCrLf & _
'      "with each server name.", "Text to Replace")
strReplaceWith = strStartServers

' Server to modify config file on
strConfigServers = "172.16.8.112"
arrConfigServers = Split(strConfigServers, ",")

For Each strServer1 In arrStopServers
      If Ping(strServer1) = True Then
            For Each strService In arrServices
                  Set objWMIService = GetObject("winmgmts:" _
                        & "{impersonationLevel=impersonate}!\\" _
                        & strServer1 & "\root\cimv2")
                  Set colListOfServices = objWMIService.ExecQuery _
                        ("Select * from Win32_Service Where DisplayName='"& strService & "'")
                  On Error Resume Next
                  For Each objService In colListOfServices
                        If Err.Number = 0 Then
                              On Error GoTo 0
                              objService.StopService()
                              WScript.Echo ""
                              WScript.Echo strService & " service stopped on " & strServer1
                              WScript.Sleep 2000
                              objService.ChangeStartMode "Manual"
                              WScript.Echo strService & " service StartMode changed to Manual."
                              WScript.Sleep 2000
                        Else
                              Err.Clear
                              On Error GoTo 0
                              WScript.Echo strService & " service was not found on " & strServer1
                        End If
                  Next
            Next
      Else
            WScript.Echo ""
            WScript.Echo strServer1 & " could not be pinged."
      End If
Next

For Each strServer2 In arrStartServers
      If Ping(strServer2) = True Then
            For Each strService In arrServices
                  Set objWMIService = GetObject("winmgmts:" _
                        & "{impersonationLevel=impersonate}!\\" _
                        & strServer2 & "\root\cimv2")
                  Set colListOfServices = objWMIService.ExecQuery _
                        ("Select * from Win32_Service Where DisplayName='"& strService & "'")
                  On Error Resume Next
                  For Each objService In colListOfServices
                        If Err.Number = 0 Then
                              On Error GoTo 0
                              objService.ChangeStartMode "Automatic"
                              WScript.Echo ""
                              WScript.Echo strService & " service StartMode changed to Automatic."
                              WScript.Sleep 2000
                              objService.StartService()
                              WScript.Echo strService & " service started on " & strServer2
                        Else
                              Err.Clear
                              On Error GoTo 0
                              WScript.Echo strService & " service was not found on " & strServer2
                        End If
                  Next
            Next
      Else
            WScript.Echo ""
            WScript.Echo strServer2 & " could not be pinged."
      End If
Next

WScript.Echo ""

'========= Now replace servername in a config file =================
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
For Each strServer In arrConfigServers
      'strReplaceWith = strServer
      strFile = "\\" & strServer & "\" & Replace(strConfigFile, ":", "$")
      If objFSO.FileExists(strFile) = True Then
            Set objFile = objFSO.OpenTextFile(strFile, intForReading, False)
            strContents = objFile.ReadAll
            intStart = InStr(LCase(strContents), LCase(strReplaceWhat))
            intCount = 0
            While intStart > 0 And intStart < Len(strContents)
                  intCount = intCount + 1
                  WScript.Echo strReplaceWhat & " was found in " & strFile & " " & intCount & " time(s)."
                  strContents = Left(strContents, intStart - 1) & strReplaceWith & Mid(strContents, intStart + Len(strReplaceWhat))
                  intStart = InStr(intStart + 1, LCase(strContents), LCase(strReplaceWhat))
            Wend
            objFile.Close
            Set objFile = objFSO.CreateTextFile(strFile, True)
            objFile.Write strContents
            objFile.Close
            Set objFile = Nothing
            WScript.Echo strFile & " has been processed."
      Else
            WScript.Echo "Could not find " & strFile
      End If
Next

WScript.Echo ""
WScript.Echo "Script finished."
'==============
Function Ping(strComputer)
      Dim objShell, boolCode
      Set objShell = CreateObject("WScript.Shell")
      boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
      If boolCode = 0 Then
            Ping = True
      Else
            Ping = False
      End If
End Function
'================================




This is the result i got from the command prompt:





Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.


Service1 service stopped on Server 2
Service1 service StartMode changed to Manual.

Service 2 service stopped on Server 2
Service 2 service StartMode changed to Manual.

Service 3 Service service stopped on Server2
Service 3 Service service StartMode changed to Manual.

Service 3  service StartMode changed to Automatic.
Service 3 service started on Server 1

Service 3 service StartMode changed to Automatic.
Service 3 service started on Server 1

Service 3 service StartMode changed to Automatic.
Service 3 Service service started on Server 1

Server 2 was found in \\172.16.8.112\v:\Program Files\test1\test2\file.exe.config 1 time(s).
\\172.16.8.112\v:\Program Files\test1\test2\file.exe.config has been processed.

Script finished.

D:\Server Support>




Can we hide the whole process so the support team wouldn't see what happens behind the scene?
Also what does the last 2 lines mean?
"Server 2 was found in \\172.16.8.112\v:\Program Files\test1\test2\file.exe.config 1 time(s).
\\172.16.8.112\v:\Program Files\test1\test2\file.exe.config has been processed."



0
RobSampsonCommented:
To hide the whole process, change the top bit:
If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
    strPath = Wscript.ScriptFullName
    strCommand = "%comspec% /k cscript  """ & strPath & """"
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run(strCommand), 1, True
    Wscript.Quit
End If

to this:
If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
    strPath = Wscript.ScriptFullName
    strCommand = "%comspec% /c cscript  """ & strPath & """"
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run(strCommand), 0, True
    Wscript.Quit
End If


and the last two lines just tell you how many instances of strStopServers were replaced by strStartServers in the config file.

Regards,

Rob.
0
winpro2000Author Commented:
Thanks Rob,
What about the adding the 3 servers instead of one server in the script? Can i add them and separate them with a comma?

Regards,
Ehab
0
RobSampsonCommented:
Do want to use three Stop servers, or three Start servers?

Rob.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
winpro2000Author Commented:
What i meant is therer will be 3 servers instead of one server that need the config file to be modified at. In my example above, it is the line: strConfigServers = "172.16.8.112"


0
winpro2000Author Commented:
Rob,
For some reason the hiding the script didn't work. Nothing was stopped or started. Nothing happened at all when i click on the file.
Can we hide the whole process but get a "Compled " message?
0
winpro2000Author Commented:
Rob,

Could you help within the next hour? I have to present the prod scrip to the management.
0
winpro2000Author Commented:
Rob had help me customize a script and never gave up. He is the best scriptor by far!!
Thanks Rob for all your help.
Ehab
0
RobSampsonCommented:
Hi, sorry, I was very busy over the weekend and didn't check my emails....
Did you sort this out?  To change the config file on more than one server, change this
strConfigServers = "172.16.8.112"
to
strConfigServers = "172.16.8.112,172.16.8.113,172.16.8.114"

Also, hiding the script output should not have made a difference to the actual actions performed....
if you change this
If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
    strPath = Wscript.ScriptFullName
    strCommand = "%comspec% /c cscript  """ & strPath & """"
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run(strCommand), 0, True
    Wscript.Quit
End If

back to this:
If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
    strPath = Wscript.ScriptFullName
    strCommand = "%comspec% /k cscript  """ & strPath & """"
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run(strCommand), 1, True
    Wscript.Quit
End If

does it work correctly?

Regards,

Rob.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.