You are here: Re: Please critique this HTA (Change Database Remotely) « HTML « IT news, forums, messages
Re: Please critique this HTA (Change Database Remotely)

Posted by Highlander on 11/30/05 05:40

Al Dunbar wrote:
> "Highlander" <tron9901@msn.com> wrote in message
> news:1133209896.037785.269910@o13g2000cwo.googlegroups.com...
> >
> > <snip>
> >
> > 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
>
> <snip>
>
> > 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
>
> Would not be as much of an issue if you avoided using global variables.

Since the "Enable Change Database button" IF statement has to be in all
3 subroutines (ServerButton, ReleaseSelectList and DBSelectList), I had
to make the 3 variables (strServer, strRelease and strDatabase) global.

> > strCMD = "notepad"
>
> recommend using "notepad.exe" instead.
>

Done.

> > 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
>
> Suggest extracting the common code from the true and false blocks above,
> like this:
>
> > If strServer = "" Then
> > strServer = cboServerSelection.Value
> > end if
> > strVersionFile = "\\" & strServer & "\D$\Program Files\Adss\Patch
> > Level\Version.ini"
> > Set objWshScriptExec = objShell.Exec (strCMD & " " & strVersionFile)
>
> /Al

Done.
Extracting the common code from the true and false blocks worked great.
I used this same logic in other areas of the script and overall I
trimmed between 25 and 30 lines. Thanks Al!

Any suggestions on my remaining questions?

- 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 "&nbsp;" to insert a large
number of spaces in between text and listboxes. Is there a better way?

- Dave

Modified script:

(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 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.exe"
Set objShell = CreateObject("WScript.Shell")

If strServer = "" Then
strServer = cboServerSelection.Value
End if

strVersionFile = "\\" & strServer & "\D$\Program Files\Adss\Patch
Level\Version.ini"
Set objWshScriptExec = objShell.Exec (strCMD & " " & strVersionFile)

' Enable Change Database button when all criteria is selected
If strServer <> "" and strRelease <> "" and strDatabase <> "" Then
ChangeDatabase_button.Disabled = False

' Display server name in HTA window
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

' Display release in HTA window
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

' Display database in HTA window
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"

Set FSO = CreateObject("Scripting.FileSystemObject")

' If file exists, remove the Read Only attribute if necessary
If CreateObject("Scripting.FileSystemObject").FileExists(strTargetPATH
& strNewREGFile) Then

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
End If

FSO.CopyFile strREGFileSourcePATH, strTargetPATH, True
FSO.CopyFile strEXEFile, strTargetPATH, True

' 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)

Else
strConfigDatabase = strDatabase

End If

strNewConfigFile = "Configuration-" & strConfigDatabase & ".xml"

strConfigFileSourcePATH = strConfigPath & strReleasePath &
strNewConfigFile
strTargetConfigFile = "\\" & strServer &
"\D$\Inetpub\wwwroot\WebView\Configuration.xml"

' Remove the Read Only attribute if necessary
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

FSO.CopyFile strConfigFileSourcePATH, strTargetConfigFile, True

Set objShell = CreateObject("WScript.Shell")

' Change Registry
strREGImportCMD = "Psexec" & " \\" & strServer & " " & "cmd /c
C:\TEMP\REG IMPORT"
Set objWshScriptExec = objShell.Exec(strREGImportCMD & " " &
strNewREGFile)

' Restart IIS
strIISResetCMD = "Psexec" & " \\" & strServer & " " & "cmd /c
C:\WINNT\system32\IISRESET /RESTART"
Set objWshScriptExec = objShell.Exec(strIISResetCMD)

' Display the output of strIISResetCMD
Set objStdOut = objWshScriptExec.StdOut
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 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:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<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 ====================

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация