patternhtmlMinor
PowerShell within HTA
Viewed 0 times
withinpowershellhta
Problem
I've put some code together that helps me search an internal ticket system using an HTA application with some TextBoxes and some embedded VBScript that runs PowerShell scripts. The PowerShell scripts are hard coded to a certain directory and I was wondering if there's a better way to accomplish this task/goal. I really do appreciate any assistance! I'm trying to provide this to co-workers who want things as simple as can be!
Here's the HTA code, as it can be seen, the VBScript has hard coded references to the various PowerShell scripts. (Huge thanks to this for the basic code I adapted to my specific needs).
```
Search for IR
Sub Resize()
window.resizeTo 500,450
TextBox1.Focus
End Sub
Sub ExecutePowerShell()
Dim oShell, appCmd, sSvr, sLast, sDesc, sDate
'Collect value from input form
sSvr = document.getElementByID("TextBox1").Value
sLast = document.getElementByID("TextBox2").Value
sDesc = document.getElementByID("TextBox3").Value
sDate = document.getElementByID("choose").Value
'Check for empty server name input box.
'If sSvr = "" Then
'MsgBox "Please enter something in the input form"
'Exit Sub
'End If
Set oShell = CreateObject("WScript.Shell")
appCmd = "powershell.exe C:\Temp\SearchIR.ps1 " & Chr(39) & sSvr & Chr(39) & " " & Chr(39) & sLast & Chr(39) & " " & Chr(39) & sDesc & Chr(39) & " " & Chr(39) & sDate & Chr(39)
oShell.Run appCmd, 0, true
End Sub
Sub Clearer()
TextBox1.Value = ""
TextBox2.Value = ""
TextBox3.Value = ""
TextBox4.Value = ""
TextBox5.Value = ""
TextBox6.Value = ""
TextBox7.Value = ""
TextBox8.Value = ""
choose.Value = ""
ChooseStatus.Value = ""
TextBox1.Focus
End Sub
Sub SearchSR()
Dim sSr, sRequest, oSRSearch
sRequest = document.getElementByID("TextBox4").Value
If sRequest = "" Then
MsgBox "Please enter something in the input form"
Exit Sub
End If
Set oSRSearch = CreateObject("WScript.Shell")
sSr = "powershell.exe C:\Temp\SearchSR.ps1 " & Chr(39) & sRequest & Chr(39)
oSRSearch.Run sSr, 0, true
End Sub
Sub SearchCR()
Here's the HTA code, as it can be seen, the VBScript has hard coded references to the various PowerShell scripts. (Huge thanks to this for the basic code I adapted to my specific needs).
```
Search for IR
Sub Resize()
window.resizeTo 500,450
TextBox1.Focus
End Sub
Sub ExecutePowerShell()
Dim oShell, appCmd, sSvr, sLast, sDesc, sDate
'Collect value from input form
sSvr = document.getElementByID("TextBox1").Value
sLast = document.getElementByID("TextBox2").Value
sDesc = document.getElementByID("TextBox3").Value
sDate = document.getElementByID("choose").Value
'Check for empty server name input box.
'If sSvr = "" Then
'MsgBox "Please enter something in the input form"
'Exit Sub
'End If
Set oShell = CreateObject("WScript.Shell")
appCmd = "powershell.exe C:\Temp\SearchIR.ps1 " & Chr(39) & sSvr & Chr(39) & " " & Chr(39) & sLast & Chr(39) & " " & Chr(39) & sDesc & Chr(39) & " " & Chr(39) & sDate & Chr(39)
oShell.Run appCmd, 0, true
End Sub
Sub Clearer()
TextBox1.Value = ""
TextBox2.Value = ""
TextBox3.Value = ""
TextBox4.Value = ""
TextBox5.Value = ""
TextBox6.Value = ""
TextBox7.Value = ""
TextBox8.Value = ""
choose.Value = ""
ChooseStatus.Value = ""
TextBox1.Focus
End Sub
Sub SearchSR()
Dim sSr, sRequest, oSRSearch
sRequest = document.getElementByID("TextBox4").Value
If sRequest = "" Then
MsgBox "Please enter something in the input form"
Exit Sub
End If
Set oSRSearch = CreateObject("WScript.Shell")
sSr = "powershell.exe C:\Temp\SearchSR.ps1 " & Chr(39) & sRequest & Chr(39)
oSRSearch.Run sSr, 0, true
End Sub
Sub SearchCR()
Solution
The lack of indentation may make slightly smaller HTML files, but this isn't 1997 anymore, a few spaces isn't going to make a significant difference in download times. Readability wins over micro-optimization - assuming the non-indentation is not merely the result of a paste glitch.
Isn't it much easier to read? If it's easier to read, it's easier to maintain... and to review.
Why is there commented-out code there? If the [lack of] indentation was meant to save bytes, then that's defeating the purpose!
Other than that, I see 6 places where you declare a
Sub Resize()
window.resizeTo 500,450
TextBox1.Focus
End Sub
Sub ExecutePowerShell()
'Collect value from input form
Dim oShell, appCmd, sSvr, sLast, sDesc, sDate
sSvr = document.getElementByID("TextBox1").Value
sLast = document.getElementByID("TextBox2").Value
sDesc = document.getElementByID("TextBox3").Value
sDate = document.getElementByID("choose").Value
'Check for empty server name input box.
'If sSvr = "" Then
' MsgBox "Please enter something in the input form"
' Exit Sub
'End If
Set oShell = CreateObject("WScript.Shell")
appCmd = "powershell.exe C:\Temp\SearchIR.ps1 " & Chr(39) & sSvr & Chr(39) & " " & Chr(39) & sLast & Chr(39) & " " & Chr(39) & sDesc & Chr(39) & " " & Chr(39) & sDate & Chr(39)
oShell.Run appCmd, 0, true
End Sub
...Isn't it much easier to read? If it's easier to read, it's easier to maintain... and to review.
Why is there commented-out code there? If the [lack of] indentation was meant to save bytes, then that's defeating the purpose!
Clearer is a class name - method/procedure names should start with a verb. A better name might be ClearFields or something similar.Other than that, I see 6 places where you declare a
powershell.exe command string. You need to have a dedicated procedure for this!Code Snippets
<script language="vbscript">
Sub Resize()
window.resizeTo 500,450
TextBox1.Focus
End Sub
Sub ExecutePowerShell()
'Collect value from input form
Dim oShell, appCmd, sSvr, sLast, sDesc, sDate
sSvr = document.getElementByID("TextBox1").Value
sLast = document.getElementByID("TextBox2").Value
sDesc = document.getElementByID("TextBox3").Value
sDate = document.getElementByID("choose").Value
'Check for empty server name input box.
'If sSvr = "" Then
' MsgBox "Please enter something in the input form"
' Exit Sub
'End If
Set oShell = CreateObject("WScript.Shell")
appCmd = "powershell.exe C:\Temp\SearchIR.ps1 " & Chr(39) & sSvr & Chr(39) & " " & Chr(39) & sLast & Chr(39) & " " & Chr(39) & sDesc & Chr(39) & " " & Chr(39) & sDate & Chr(39)
oShell.Run appCmd, 0, true
End Sub
...Context
StackExchange Code Review Q#80324, answer score: 2
Revisions (0)
No revisions yet.