Saturday, July 29, 2006

Getting to Grips With HTA

Working with scripts is great as we all know, however have you started to convert some of your scripts over to HTA for a simple interface for service desk style activities. Here is an example of a simple service control script I wrote today for someone. Copy this into notepad and the save the file as a hta and then start to experiment with the script elements, enjoy.

<head>
<title>Restart Spooler Service</title>

<SCRIPT LANGUAGE="VBScript">

Sub Window_onLoad
window.resizeTo 600,200
End Sub

Sub RestartSpooler
On Error Resume Next
Server = ServerName.Value
ServiceName = "Spooler"
if Server = "Please Select Server From List" then
msgBox "Please Select Server Name from drop down list"
else
Set Locator = CreateObject("WbemScripting.SWbemLocator")
Set ServiceSet = Locator.ConnectServer(Server,"root\cimv2").ExecQuery("select * from _ Win32_Service
where Name='"& ServiceName &"'")
if Err.Number <> 0 then
MsgBox Err.Description
Else

For each Service in ServiceSet

RetVal = Service.StopService
if RetVal = 0 Then
msgbox "Service stopped successfully"
elseif RetVal = 3 Then
msgbox "There are other services that depend on this service that must be stopped first"
elseif RetVal = 5 Then
msgbox "Service was already stopped"
end If

RetVal = Service.StartService
if RetVal = 0 Then
msgbox "Service started successfully"
elseif RetVal = 3 Then
msgbox "There are other services that depend on this service that must be started first"
elseif RetVal = 5 Then
msgbox "Service was already started"
end If

Next
ServerName.Value = "Please Select Server From List"
end If
end if
End Sub

</SCRIPT>

<HTA:APPLICATION
APPLICATIONNAME="HTASpoolerControl"
SCROLL="no"
SINGLEINSTANCE="yes"
>

</head>
<body bgcolor="white"><center>

<body STYLE="font:14 pt arial; color:white; filter:progid:DXImageTransform.Microsoft.Gradient (GradientType=1, StartColorStr='#000000', EndColorStr='#0000FF')">

<h1>Restart Spooler Service On Server</h1>
<select size="1" name="ServerName">
<option value="Please Select Server From List">Please Select Server From List</option>
<option value="FILESVRA">FILESVRA</option>
<option value="FILESVRB">FILESVRB</option>
<option value="FILESVRC">FILESVRC</option>
<option value="FILESVRD">FILESVRD</option>
</select>
<input id=Go_Button type="button" value="RestartSpooler" name="Go_button" onClick="RestartSpooler">
<p>
</center>
</body>


Dave