H
Highlander
Hello. As a novice in both VBScript and HTML, I usually find code
snippets, tweak 'em to my specifications, then bolt 'em all together to
accomplish what I need. Like in this case. The following script works
fine, but I'm sure it can be streamlined, corrected, and the redundancy
removed.
This HTA will be used to "change the database" of a remote server,
which involves 3 major tasks:
A. Make specific changes to the Registry (using .REG files)
B. Copy a specific Configuration.xml file
C. Restart IIS
The high level functions of the HTA are as follows:
1. Enter or select a server (opens that server's version.ini file once
selected)
2. Select a "release" (determines the source PATH for the
Configuration.xml and .REG files)
3. Select a "database" (determines the source Configuration.xml and
..REG files to be copied)
4. If the target Configuration.xml and .REG files exist and are Read
Only, remove the Read Only attribute
5. Enable the "Change Database" button only when the server, release
and database have been selected.
6. After the "Change Database" button is clicked and the script is
executed:
- reset all variables
- reset text and list boxes
- disable the "Change Database" button
Some obvious things that I imagine can be addressed:
- The conditional IF statement used to enable the "Change Database"
button...I've had to put it in multiple subroutines. Is there a better
way?
- I'm using the sysinternals.com utility PSEXEC to remotely change the
Registry. Is there a better way?
- I'm using the sysinternals.com utility PSEXEC to remotely restart
IIS. Is there a better way?
- I'm using repeated entries of the HTML " " to insert a large
number of spaces in between text and listboxes. Is there a better way?
Any suggestions would be greatly appreciated. Thanks!
- Dave
Watch for word wrap:
===== BEGIN Change_Database_Remotely.hta ====================
<html>
<head>
<title>Change Database Remotely</title>
<HTA:APPLICATION
ID="HTAUI"
APPLICATIONNAME="HTA User Interface"
SCROLL="no"
SINGLEINSTANCE="yes"
WINDOWSTATE="maximized"</head>
<style>
BODY
{
background-color: buttonface;
font-family: Helvetica;
font-size: 12pt;
margin-top: 2px;
margin-left: 8px;
margin-right: 3px;
margin-bottom: 3px;
}
</style>
<SCRIPT language="VBScript">
Dim strServer
Dim strVersionFile
Dim strRelease
Dim strReleasePath
Dim strDatabase
Dim strNewREGFile
Dim strREGPath
Dim s
Dim r
Dim fso
Dim f
Dim sFILE
Dim objShell
Dim objWshScriptExec
Dim objStdOut
Dim objFSO
Dim strREGFileSourcePATH
Dim strREGFile
Dim strTargetPATH
Dim strEXEFile
Dim strConfigPath
Dim strNewConfigFile
Dim strConfigFileSourcePATH
Dim strTargetConfigFile
Dim strREGImportCMD
Dim strIISResetCMD
Sub Window_Onload
self.Focus()
self.ResizeTo 600,500
self.MoveTo 200,50
' Clear all variables in order for the
' "Enable Change Database button" IF statement to work
strServer = ""
strRelease = ""
strDatabase = ""
End sub
Sub ServerButton
strServer = txtServerInput.Value
strCMD = "notepad"
Set objShell = CreateObject("WScript.Shell")
If strServer = "" Then
strServer = cboServerSelection.Value
strVersionFile = "\\" & strServer & "\D$\Program Files\Adss\Patch
Level\Version.ini"
Set objWshScriptExec = objShell.Exec (strCMD & " " & strVersionFile)
Else
strVersionFile = "\\" & strServer & "\D$\Program Files\Adss\Patch
Level\Version.ini"
Set objWshScriptExec = objShell.Exec (strCMD & " " & strVersionFile)
End If
' Enable Change Database button when all criteria is selected
If strServer <> "" and strRelease <> "" and strDatabase <> "" Then
ChangeDatabase_button.Disabled = False
spServer.InnerHTML = strServer
End Sub
Sub ReleaseSelectList
strRelease = ReleaseSelection.Value
If strRelease = "July" Then
strReleasePath = "July 2005 Release\"
Else
strReleasePath = ""
End If
' Enable Change Database button when all criteria is selected
If strServer <> "" and strRelease <> "" and strDatabase <> "" Then
ChangeDatabase_button.Disabled = False
spRelease.InnerHTML = strRelease
End Sub
Sub DBSelectList
strDatabase = DBSelection.Value
strNewREGFile = strDatabase & ".REG"
' Enable Change Database button when all criteria is selected
If strServer <> "" and strRelease <> "" and strDatabase <> "" Then
ChangeDatabase_button.Disabled = False
spDatabase.InnerHTML = strDatabase
End Sub
Sub ChangeDatabase
' Copy REG.EXE and Database REG file to target server temp folder
strREGPath = "\\ServerName\D$\ASISS Team\Tools\Reg Keys\"
strREGFileSourcePATH = strREGPath & strReleasePath & strNewREGFile
strTargetPATH = "\\" & strServer & "\C$\TEMP\"
strEXEFile = "\\ServerName\D$\ASISS Team\Tools\Batch\Reg.exe"
' Modify the Read Only attribute if necessary
If CreateObject("Scripting.FileSystemObject").FileExists(strTargetPATH
& strNewREGFile) Then
' Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(strTargetPATH & strNewREGFile)
' If the first bit of the attributes byte is set to 1,
' the file is Read Only.
' If this is the case, clear the first bit.
If f.attributes and 1 Then
f.attributes = f.attributes - 1
End If
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile strREGFileSourcePATH, strTargetPATH, True
objFSO.CopyFile strEXEFile, strTargetPATH, True
Set fso = Nothing
Set f = Nothing
Else
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile strREGFileSourcePATH, strTargetPATH, True
objFSO.CopyFile strEXEFile, strTargetPATH, True
End If
' Copy Database CONFIG file to target server Webview folder
strConfigPath = "\\ServerName\D$\ASISS Team\Tools\XML Config Files\"
' Check to see if "TEST" is in the file name
s = "TEST"
r = InStr(strDatabase, s)
If r = 1 Then
strConfigDatabase = right(strDatabase,2)
strNewConfigFile = "Configuration-" & strConfigDatabase & ".xml"
Else
strConfigDatabase = strDatabase
strNewConfigFile = "Configuration-" & strConfigDatabase & ".xml"
End If
strConfigFileSourcePATH = strConfigPath & strReleasePath &
strNewConfigFile
strTargetConfigFile = "\\" & strServer &
"\D$\Inetpub\wwwroot\WebView\Configuration.xml"
' Modify the Read Only attribute if necessary
' Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(strTargetConfigFile)
' If the first bit of the attributes byte is set to 1,
' the file is Read Only.
' If this is the case, clear the first bit.
If f.attributes and 1 Then
f.attributes = f.attributes - 1
End If
Set fso = Nothing
Set f = Nothing
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile strConfigFileSourcePATH, strTargetConfigFile, True
' Change Registry
strREGImportCMD = "Psexec" & " \\" & strServer & " " & "cmd /c
C:\TEMP\REG IMPORT"
Set objShell = CreateObject("WScript.Shell")
Set objWshScriptExec = objShell.Exec(strREGImportCMD & " " &
strNewREGFile)
' Restart IIS
strIISResetCMD = "Psexec" & " \\" & strServer & " " & "cmd /c
C:\WINNT\system32\IISRESET /RESTART"
Set objShell = CreateObject("WScript.Shell")
Set objWshScriptExec = objShell.Exec(strIISResetCMD)
Set objStdOut = objWshScriptExec.StdOut
' Display the output of strIISResetCMD
strOutput = objStdOut.ReadAll
MsgBox strOutput, vbInformation, "Change Database Remotely -
COMPLETED."
' Disable Change Database Button
ChangeDatabase_button.Disabled=True
' Clear variables
strServer = ""
strRelease = ""
strDatabase = ""
' Reset all text and list boxes
txtServerInput.value = ""
cboServerSelection.value = ""
DBSelection.value = ""
ReleaseSelection.value = ""
End Sub
' Cleanup
Set strServer = Nothing
Set strVersionFile = Nothing
Set strRelease = Nothing
Set strReleasePath = Nothing
Set strDatabase = Nothing
Set strNewREGFile = Nothing
Set strREGPath = Nothing
Set s = Nothing
Set r = Nothing
Set fso = Nothing
Set f = Nothing
Set sFILE = Nothing
Set objShell = Nothing
Set objWshScriptExec = Nothing
Set objStdOut = Nothing
Set objFSO = Nothing
Set strREGFileSourcePATH = Nothing
Set strREGFile = Nothing
Set strTargetPATH = Nothing
Set strEXEFile = Nothing
Set strConfigPath = Nothing
Set strNewConfigFile = Nothing
Set strConfigFileSourcePATH = Nothing
Set strTargetConfigFile = Nothing
Set strREGImportCMD = Nothing
Set strIISResetCMD = Nothing
</SCRIPT>
<BODY>
<H2 align="center">Change Database Remotely</H2>
<p align="left"><font face="serif" size="4"><b>Enter or select a server
(it's version.ini file
will open):</b></font><br/>
<align="left">
<input type="text" id="txtServerInput"/><br/>
<align="right">
<select id="cboServerSelection">
<option value="0"></option>
<option value="Server1">Server1</option>
<option value="Server2">Server2</option>
<option value="Server3">Server3</option>
<option value="Server4">Server4</option>
<option value="Server5">Server5</option>
<option value="Server6">Server6</option>
</select><br/>
<button onclick="ServerButton">Submit</button>
<p align="left"><font face="serif" size="4"><b>Select a
release: Select
a database:</b></font><br/>
<align="left">
<select id="ReleaseSelection" onChange="ReleaseSelectList">
<option value="0"></option>
<option value="July">July</option>
<option value="December">December</option>
</select> <select
id="DBSelection" onChange="DBSelectList">
<option value="0"></option>
<option value="TESTAJ">TESTAJ</option>
<option value="TESTAP">TESTAP</option>
<option value="TESTAT">TESTAT</option>
<option value="TESTZB">TESTZB</option>
<option value="TESTZE">TESTZE</option>
<option value="TESTZF">TESTZF</option>
<option value="TESTZG">TESTZG</option>
<option value="TESTZH">TESTZH</option>
<option value="TESTZJ">TESTZJ</option>
<option value="TESTZL">TESTZL</option>
<option value="TESTZS">TESTZS</option>
<option value="TESTZT">TESTZT</option>
<option value="TESTZU">TESTZU</option>
<option value="FPT">FPT</option>
<option value="PIONEER">PIONEER</option>
<option value="Production_Training">Production_Training</option>
<option value="PRODUCTION">PRODUCTION</option>
</select><br/>
<p>
<hr>
<span id =spServer></span>
<p>
<span id =spRelease></span>
<p>
<span id =spDatabase></span>
<hr>
<p align="left">
<input id=ChangeDatabaseButton class="button" type="button"
value="Change Database"
name="ChangeDatabase_button" onClick="ChangeDatabase" Disabled=True>
</BODY>
</html>
===== END Change_Database_Remotely.hta ====================
snippets, tweak 'em to my specifications, then bolt 'em all together to
accomplish what I need. Like in this case. The following script works
fine, but I'm sure it can be streamlined, corrected, and the redundancy
removed.
This HTA will be used to "change the database" of a remote server,
which involves 3 major tasks:
A. Make specific changes to the Registry (using .REG files)
B. Copy a specific Configuration.xml file
C. Restart IIS
The high level functions of the HTA are as follows:
1. Enter or select a server (opens that server's version.ini file once
selected)
2. Select a "release" (determines the source PATH for the
Configuration.xml and .REG files)
3. Select a "database" (determines the source Configuration.xml and
..REG files to be copied)
4. If the target Configuration.xml and .REG files exist and are Read
Only, remove the Read Only attribute
5. Enable the "Change Database" button only when the server, release
and database have been selected.
6. After the "Change Database" button is clicked and the script is
executed:
- reset all variables
- reset text and list boxes
- disable the "Change Database" button
Some obvious things that I imagine can be addressed:
- The conditional IF statement used to enable the "Change Database"
button...I've had to put it in multiple subroutines. Is there a better
way?
- I'm using the sysinternals.com utility PSEXEC to remotely change the
Registry. Is there a better way?
- I'm using the sysinternals.com utility PSEXEC to remotely restart
IIS. Is there a better way?
- I'm using repeated entries of the HTML " " to insert a large
number of spaces in between text and listboxes. Is there a better way?
Any suggestions would be greatly appreciated. Thanks!
- Dave
Watch for word wrap:
===== BEGIN Change_Database_Remotely.hta ====================
<html>
<head>
<title>Change Database Remotely</title>
<HTA:APPLICATION
ID="HTAUI"
APPLICATIONNAME="HTA User Interface"
SCROLL="no"
SINGLEINSTANCE="yes"
WINDOWSTATE="maximized"</head>
<style>
BODY
{
background-color: buttonface;
font-family: Helvetica;
font-size: 12pt;
margin-top: 2px;
margin-left: 8px;
margin-right: 3px;
margin-bottom: 3px;
}
</style>
<SCRIPT language="VBScript">
Dim strServer
Dim strVersionFile
Dim strRelease
Dim strReleasePath
Dim strDatabase
Dim strNewREGFile
Dim strREGPath
Dim s
Dim r
Dim fso
Dim f
Dim sFILE
Dim objShell
Dim objWshScriptExec
Dim objStdOut
Dim objFSO
Dim strREGFileSourcePATH
Dim strREGFile
Dim strTargetPATH
Dim strEXEFile
Dim strConfigPath
Dim strNewConfigFile
Dim strConfigFileSourcePATH
Dim strTargetConfigFile
Dim strREGImportCMD
Dim strIISResetCMD
Sub Window_Onload
self.Focus()
self.ResizeTo 600,500
self.MoveTo 200,50
' Clear all variables in order for the
' "Enable Change Database button" IF statement to work
strServer = ""
strRelease = ""
strDatabase = ""
End sub
Sub ServerButton
strServer = txtServerInput.Value
strCMD = "notepad"
Set objShell = CreateObject("WScript.Shell")
If strServer = "" Then
strServer = cboServerSelection.Value
strVersionFile = "\\" & strServer & "\D$\Program Files\Adss\Patch
Level\Version.ini"
Set objWshScriptExec = objShell.Exec (strCMD & " " & strVersionFile)
Else
strVersionFile = "\\" & strServer & "\D$\Program Files\Adss\Patch
Level\Version.ini"
Set objWshScriptExec = objShell.Exec (strCMD & " " & strVersionFile)
End If
' Enable Change Database button when all criteria is selected
If strServer <> "" and strRelease <> "" and strDatabase <> "" Then
ChangeDatabase_button.Disabled = False
spServer.InnerHTML = strServer
End Sub
Sub ReleaseSelectList
strRelease = ReleaseSelection.Value
If strRelease = "July" Then
strReleasePath = "July 2005 Release\"
Else
strReleasePath = ""
End If
' Enable Change Database button when all criteria is selected
If strServer <> "" and strRelease <> "" and strDatabase <> "" Then
ChangeDatabase_button.Disabled = False
spRelease.InnerHTML = strRelease
End Sub
Sub DBSelectList
strDatabase = DBSelection.Value
strNewREGFile = strDatabase & ".REG"
' Enable Change Database button when all criteria is selected
If strServer <> "" and strRelease <> "" and strDatabase <> "" Then
ChangeDatabase_button.Disabled = False
spDatabase.InnerHTML = strDatabase
End Sub
Sub ChangeDatabase
' Copy REG.EXE and Database REG file to target server temp folder
strREGPath = "\\ServerName\D$\ASISS Team\Tools\Reg Keys\"
strREGFileSourcePATH = strREGPath & strReleasePath & strNewREGFile
strTargetPATH = "\\" & strServer & "\C$\TEMP\"
strEXEFile = "\\ServerName\D$\ASISS Team\Tools\Batch\Reg.exe"
' Modify the Read Only attribute if necessary
If CreateObject("Scripting.FileSystemObject").FileExists(strTargetPATH
& strNewREGFile) Then
' Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(strTargetPATH & strNewREGFile)
' If the first bit of the attributes byte is set to 1,
' the file is Read Only.
' If this is the case, clear the first bit.
If f.attributes and 1 Then
f.attributes = f.attributes - 1
End If
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile strREGFileSourcePATH, strTargetPATH, True
objFSO.CopyFile strEXEFile, strTargetPATH, True
Set fso = Nothing
Set f = Nothing
Else
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile strREGFileSourcePATH, strTargetPATH, True
objFSO.CopyFile strEXEFile, strTargetPATH, True
End If
' Copy Database CONFIG file to target server Webview folder
strConfigPath = "\\ServerName\D$\ASISS Team\Tools\XML Config Files\"
' Check to see if "TEST" is in the file name
s = "TEST"
r = InStr(strDatabase, s)
If r = 1 Then
strConfigDatabase = right(strDatabase,2)
strNewConfigFile = "Configuration-" & strConfigDatabase & ".xml"
Else
strConfigDatabase = strDatabase
strNewConfigFile = "Configuration-" & strConfigDatabase & ".xml"
End If
strConfigFileSourcePATH = strConfigPath & strReleasePath &
strNewConfigFile
strTargetConfigFile = "\\" & strServer &
"\D$\Inetpub\wwwroot\WebView\Configuration.xml"
' Modify the Read Only attribute if necessary
' Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(strTargetConfigFile)
' If the first bit of the attributes byte is set to 1,
' the file is Read Only.
' If this is the case, clear the first bit.
If f.attributes and 1 Then
f.attributes = f.attributes - 1
End If
Set fso = Nothing
Set f = Nothing
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile strConfigFileSourcePATH, strTargetConfigFile, True
' Change Registry
strREGImportCMD = "Psexec" & " \\" & strServer & " " & "cmd /c
C:\TEMP\REG IMPORT"
Set objShell = CreateObject("WScript.Shell")
Set objWshScriptExec = objShell.Exec(strREGImportCMD & " " &
strNewREGFile)
' Restart IIS
strIISResetCMD = "Psexec" & " \\" & strServer & " " & "cmd /c
C:\WINNT\system32\IISRESET /RESTART"
Set objShell = CreateObject("WScript.Shell")
Set objWshScriptExec = objShell.Exec(strIISResetCMD)
Set objStdOut = objWshScriptExec.StdOut
' Display the output of strIISResetCMD
strOutput = objStdOut.ReadAll
MsgBox strOutput, vbInformation, "Change Database Remotely -
COMPLETED."
' Disable Change Database Button
ChangeDatabase_button.Disabled=True
' Clear variables
strServer = ""
strRelease = ""
strDatabase = ""
' Reset all text and list boxes
txtServerInput.value = ""
cboServerSelection.value = ""
DBSelection.value = ""
ReleaseSelection.value = ""
End Sub
' Cleanup
Set strServer = Nothing
Set strVersionFile = Nothing
Set strRelease = Nothing
Set strReleasePath = Nothing
Set strDatabase = Nothing
Set strNewREGFile = Nothing
Set strREGPath = Nothing
Set s = Nothing
Set r = Nothing
Set fso = Nothing
Set f = Nothing
Set sFILE = Nothing
Set objShell = Nothing
Set objWshScriptExec = Nothing
Set objStdOut = Nothing
Set objFSO = Nothing
Set strREGFileSourcePATH = Nothing
Set strREGFile = Nothing
Set strTargetPATH = Nothing
Set strEXEFile = Nothing
Set strConfigPath = Nothing
Set strNewConfigFile = Nothing
Set strConfigFileSourcePATH = Nothing
Set strTargetConfigFile = Nothing
Set strREGImportCMD = Nothing
Set strIISResetCMD = Nothing
</SCRIPT>
<BODY>
<H2 align="center">Change Database Remotely</H2>
<p align="left"><font face="serif" size="4"><b>Enter or select a server
(it's version.ini file
will open):</b></font><br/>
<align="left">
<input type="text" id="txtServerInput"/><br/>
<align="right">
<select id="cboServerSelection">
<option value="0"></option>
<option value="Server1">Server1</option>
<option value="Server2">Server2</option>
<option value="Server3">Server3</option>
<option value="Server4">Server4</option>
<option value="Server5">Server5</option>
<option value="Server6">Server6</option>
</select><br/>
<button onclick="ServerButton">Submit</button>
<p align="left"><font face="serif" size="4"><b>Select a
release: Select
a database:</b></font><br/>
<align="left">
<select id="ReleaseSelection" onChange="ReleaseSelectList">
<option value="0"></option>
<option value="July">July</option>
<option value="December">December</option>
</select> <select
id="DBSelection" onChange="DBSelectList">
<option value="0"></option>
<option value="TESTAJ">TESTAJ</option>
<option value="TESTAP">TESTAP</option>
<option value="TESTAT">TESTAT</option>
<option value="TESTZB">TESTZB</option>
<option value="TESTZE">TESTZE</option>
<option value="TESTZF">TESTZF</option>
<option value="TESTZG">TESTZG</option>
<option value="TESTZH">TESTZH</option>
<option value="TESTZJ">TESTZJ</option>
<option value="TESTZL">TESTZL</option>
<option value="TESTZS">TESTZS</option>
<option value="TESTZT">TESTZT</option>
<option value="TESTZU">TESTZU</option>
<option value="FPT">FPT</option>
<option value="PIONEER">PIONEER</option>
<option value="Production_Training">Production_Training</option>
<option value="PRODUCTION">PRODUCTION</option>
</select><br/>
<p>
<hr>
<span id =spServer></span>
<p>
<span id =spRelease></span>
<p>
<span id =spDatabase></span>
<hr>
<p align="left">
<input id=ChangeDatabaseButton class="button" type="button"
value="Change Database"
name="ChangeDatabase_button" onClick="ChangeDatabase" Disabled=True>
</BODY>
</html>
===== END Change_Database_Remotely.hta ====================