winpro2000
asked on
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?
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?
ASKER
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
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
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","D09790 RING","D09 790RING") ' 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.
arrServices = Array("Print Spooler","Windows Audio","VNC Server")
strServer1 = "D09790RING" ' to stop services
strServer2 = "D09790RING" ' to start services
arrServers = Array("D09790RING","D09790
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.
ASKER
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
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
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.
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.
ASKER
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
What do i need, or they need to have to run this utility??
Thanks in advance.
Ehab
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.
Regards,
Rob.
Try this script:
'========================= =======
If LCase(Right(Wscript.FullNa me, 11)) = "wscript.exe" Then
strPath = Wscript.ScriptFullName
strCommand = "%comspec% /k cscript """ & strPath & """"
Set objShell = CreateObject("Wscript.Shel l")
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=imper sonate}!\\ " _
& 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=imper sonate}!\\ " _
& 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.Fi leSystemOb ject")
Const intForReading = 1
For Each strServer In arrConfigServers
strReplaceWith = strServer
strFile = "\\" & strServer & "\" & Replace(strConfigFile, ":", "$")
If objFSO.FileExists(strFile) = True Then
Set objFile = objFSO.OpenTextFile(strFil e, intForReading, False)
strContents = Replace(objFile.ReadAll, strReplaceWhat, strReplaceWith)
objFile.Close
Set objFile = objFSO.CreateTextFile(strF ile, 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.Shel l")
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.
'=========================
If LCase(Right(Wscript.FullNa
strPath = Wscript.ScriptFullName
strCommand = "%comspec% /k cscript """ & strPath & """"
Set objShell = CreateObject("Wscript.Shel
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=imper
& 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
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=imper
& 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
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.Fi
Const intForReading = 1
For Each strServer In arrConfigServers
strReplaceWith = strServer
strFile = "\\" & strServer & "\" & Replace(strConfigFile, ":", "$")
If objFSO.FileExists(strFile)
Set objFile = objFSO.OpenTextFile(strFil
strContents = Replace(objFile.ReadAll, strReplaceWhat, strReplaceWith)
objFile.Close
Set objFile = objFSO.CreateTextFile(strF
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.Shel
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.
ASKER
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.
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.
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.FullNa me, 11)) = "wscript.exe" Then
strPath = Wscript.ScriptFullName
strCommand = "%comspec% /k cscript """ & strPath & """"
Set objShell = CreateObject("Wscript.Shel l")
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=imper sonate}!\\ " _
& 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=imper sonate}!\\ " _
& 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.Fi leSystemOb ject")
Const intForReading = 1
For Each strServer In arrConfigServers
'strReplaceWith = strServer
strFile = "\\" & strServer & "\" & Replace(strConfigFile, ":", "$")
If objFSO.FileExists(strFile) = True Then
Set objFile = objFSO.OpenTextFile(strFil e, intForReading, False)
strContents = Replace(objFile.ReadAll, strReplaceWhat, strReplaceWith)
objFile.Close
Set objFile = objFSO.CreateTextFile(strF ile, 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.Shel l")
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.
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.FullNa
strPath = Wscript.ScriptFullName
strCommand = "%comspec% /k cscript """ & strPath & """"
Set objShell = CreateObject("Wscript.Shel
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=imper
& 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
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=imper
& 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
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.Fi
Const intForReading = 1
For Each strServer In arrConfigServers
'strReplaceWith = strServer
strFile = "\\" & strServer & "\" & Replace(strConfigFile, ":", "$")
If objFSO.FileExists(strFile)
Set objFile = objFSO.OpenTextFile(strFil
strContents = Replace(objFile.ReadAll, strReplaceWhat, strReplaceWith)
objFile.Close
Set objFile = objFSO.CreateTextFile(strF
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.Shel
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.
ASKER
Rob,
I will try it again tonight and let you know hoe did it go!
I will try it again tonight and let you know hoe did it go!
ASKER
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.
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.
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\confi g.cfg
Hope that helps.
Regards,
Rob.
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\confi
Hope that helps.
Regards,
Rob.
ASKER
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
Regards,
EA
Is V Drive a physical drive? Can you access the file via:
\\computername\V$\folder\f ile.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.
\\computername\V$\folder\f
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.
ASKER
V is a physical drive
i can go to \\servername\v$
But the file doesn't get changed..
i can go to \\servername\v$
But the file doesn't get changed..
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.
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.
Try this, it should be a case insensitive replacement:
'========================= =======
If LCase(Right(Wscript.FullNa me, 11)) = "wscript.exe" Then
strPath = Wscript.ScriptFullName
strCommand = "%comspec% /k cscript """ & strPath & """"
Set objShell = CreateObject("Wscript.Shel l")
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=imper sonate}!\\ " _
& 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=imper sonate}!\\ " _
& 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.Fi leSystemOb ject")
Const intForReading = 1
For Each strServer In arrConfigServers
'strReplaceWith = strServer
strFile = "\\" & strServer & "\" & Replace(strConfigFile, ":", "$")
If objFSO.FileExists(strFile) = True Then
Set objFile = objFSO.OpenTextFile(strFil e, 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(strF ile, 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.Shel l")
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.
'=========================
If LCase(Right(Wscript.FullNa
strPath = Wscript.ScriptFullName
strCommand = "%comspec% /k cscript """ & strPath & """"
Set objShell = CreateObject("Wscript.Shel
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=imper
& 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
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=imper
& 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
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.Fi
Const intForReading = 1
For Each strServer In arrConfigServers
'strReplaceWith = strServer
strFile = "\\" & strServer & "\" & Replace(strConfigFile, ":", "$")
If objFSO.FileExists(strFile)
Set objFile = objFSO.OpenTextFile(strFil
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(strF
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.Shel
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.
ASKER
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.
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.
ASKER
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?
Does that make sense to you?
Sorry, no, not really. Could you provide an example in terms of ServerA and ServerB, etc...
Regards,
Rob.
Regards,
Rob.
ASKER
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.
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.
ASKER
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.FullNa me, 11)) = "wscript.exe" Then
strPath = Wscript.ScriptFullName
strCommand = "%comspec% /k cscript """ & strPath & """"
Set objShell = CreateObject("Wscript.Shel l")
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=imper sonate}!\\ " _
& 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=imper sonate}!\\ " _
& 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.Fi leSystemOb ject")
Const intForReading = 1
For Each strServer In arrConfigServers
'strReplaceWith = strServer
strFile = "\\" & strServer & "\" & Replace(strConfigFile, ":", "$")
If objFSO.FileExists(strFile) = True Then
Set objFile = objFSO.OpenTextFile(strFil e, 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(strF ile, 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.Shel l")
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."
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.FullNa
strPath = Wscript.ScriptFullName
strCommand = "%comspec% /k cscript """ & strPath & """"
Set objShell = CreateObject("Wscript.Shel
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
'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=imper
& 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
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=imper
& 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
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.Fi
Const intForReading = 1
For Each strServer In arrConfigServers
'strReplaceWith = strServer
strFile = "\\" & strServer & "\" & Replace(strConfigFile, ":", "$")
If objFSO.FileExists(strFile)
Set objFile = objFSO.OpenTextFile(strFil
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(strF
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.Shel
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
\\172.16.8.112\v:\Program Files\test1\test2\file.exe
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
\\172.16.8.112\v:\Program Files\test1\test2\file.exe
To hide the whole process, change the top bit:
If LCase(Right(Wscript.FullNa me, 11)) = "wscript.exe" Then
strPath = Wscript.ScriptFullName
strCommand = "%comspec% /k cscript """ & strPath & """"
Set objShell = CreateObject("Wscript.Shel l")
objShell.Run(strCommand), 1, True
Wscript.Quit
End If
to this:
If LCase(Right(Wscript.FullNa me, 11)) = "wscript.exe" Then
strPath = Wscript.ScriptFullName
strCommand = "%comspec% /c cscript """ & strPath & """"
Set objShell = CreateObject("Wscript.Shel l")
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.
If LCase(Right(Wscript.FullNa
strPath = Wscript.ScriptFullName
strCommand = "%comspec% /k cscript """ & strPath & """"
Set objShell = CreateObject("Wscript.Shel
objShell.Run(strCommand), 1, True
Wscript.Quit
End If
to this:
If LCase(Right(Wscript.FullNa
strPath = Wscript.ScriptFullName
strCommand = "%comspec% /c cscript """ & strPath & """"
Set objShell = CreateObject("Wscript.Shel
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.
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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"
ASKER
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?
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?
ASKER
Rob,
Could you help within the next hour? I have to present the prod scrip to the management.
Could you help within the next hour? I have to present the prod scrip to the management.
ASKER
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
Thanks Rob for all your help.
Ehab
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.FullNa me, 11)) = "wscript.exe" Then
strPath = Wscript.ScriptFullName
strCommand = "%comspec% /c cscript """ & strPath & """"
Set objShell = CreateObject("Wscript.Shel l")
objShell.Run(strCommand), 0, True
Wscript.Quit
End If
back to this:
If LCase(Right(Wscript.FullNa me, 11)) = "wscript.exe" Then
strPath = Wscript.ScriptFullName
strCommand = "%comspec% /k cscript """ & strPath & """"
Set objShell = CreateObject("Wscript.Shel l")
objShell.Run(strCommand), 1, True
Wscript.Quit
End If
does it work correctly?
Regards,
Rob.
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
Also, hiding the script output should not have made a difference to the actual actions performed....
if you change this
If LCase(Right(Wscript.FullNa
strPath = Wscript.ScriptFullName
strCommand = "%comspec% /c cscript """ & strPath & """"
Set objShell = CreateObject("Wscript.Shel
objShell.Run(strCommand), 0, True
Wscript.Quit
End If
back to this:
If LCase(Right(Wscript.FullNa
strPath = Wscript.ScriptFullName
strCommand = "%comspec% /k cscript """ & strPath & """"
Set objShell = CreateObject("Wscript.Shel
objShell.Run(strCommand), 1, True
Wscript.Quit
End If
does it work correctly?
Regards,
Rob.
'=========================
If LCase(Right(Wscript.FullNa
strPath = Wscript.ScriptFullName
strCommand = "%comspec% /k cscript """ & strPath & """"
Set objShell = CreateObject("Wscript.Shel
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=imper
& 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
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=imper
& 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
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","D09790
strFileName = "C:\Temp\Temp\Test Script\TextFile.txt"
strReplaceWhat = "the"
Set objFSO = CreateObject("Scripting.Fi
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(strFil
strContents = Replace(objFile.ReadAll, strReplaceWhat, strReplaceWith)
objFile.Close
Set objFile = objFSO.CreateTextFile(strF
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.Shel
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.