Solved

VSB script or Powershell to make into an HTA

Posted on 2014-09-27
6
194 Views
Last Modified: 2015-01-25
So I have this script that install patches to remote systems but I was wondering if there was a way to make into an HTA format, can it be done with powershell by creating a type of console and to see if the process installed successfully. I believe I have this but I want to make it more modular such we can install it on one system or on multiple systems and if possible to check if the system is on line...here is the script

' ******Usage "Cscript InstallPatch.vbs"**********
' Script is used to installs msi patch on remote computers.
' Update "Server.txt" with the list of computers to receive the patch.
' Update variable strFilename below with the filename of the patch.
' Copy the patch to "D:\Scripts\InstallPatch\"
' Save this file and run using command line "Cscript InstallPatch.vbs"
' --------------------------------------------------------------------
On Error Resume next
Dim Cnt, strFilename, strPath

'**** Update strFilename below with name of patch to install ******
'    strFilename = "IE11-Windows6.1-KB2977629-x64.msu"
'    strFilename = "install_flash_player_15_active_x.exe"
    strFilename = "install_flash_player_15_plugin.exe"


strFileType = right(strFilename,4)
select case strFileType
    case ".msu", ".msi"
	setup = "wusa.exe ""C:\CCM\" & strFilename & """ /quiet"
    case ".exe"
	if instr(1,strFilename,"flash",1) > 0 then
	    setup = """C:\CCM\" & strFilename & """ -install"
	end if
end select


'wscript.echo "Install string: " & strFileType

  
strPath = "D:\Scripts\InstallPatch\" 
Cnt = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objReadFile = objFSO.OpenTextFile("D:\Scripts\InstallPatch\server.txt", 1)
Set objLogFile = objFSO.OpenTextFile("D:\Scripts\InstallPatch\status.txt", 2)
Do Until objReadFile.AtEndOfStream
    
    strContents = objReadFile.Readline
    LogOutput cnt & ". Accessing " & strContents
    MakeDirectory(strContents)
    CopyFile1(strContents)
    InstallPatch(strContents)
    LogOutput "               "
    Cnt = Cnt + 1

loop
objReadFile.Close

function CopyFile1(Server)
  Const OverwriteExisting = TRUE

  Set objFSO = CreateObject("Scripting.FileSystemObject")
  strRemoteFile = "\\" & Server & "\C$\CCM\" & strFilename
  objFSO.CopyFile strPath & strFilename, strRemoteFile, OverwriteExisting
  LogOutput "    " & strFilename & " copied to server."
end function

function InstallPatch(Server)
  'Example:  wusa.exe d:\934307\Windows6.0-KB934307-x86.msu /quiet
 ' setup = "wusa.exe ""C:\CCM\" & strFilename & """ /quiet"
LogOutput "    " & setup
  Set wmi = GetObject("winmgmts://" & Server & "/root/cimv2")
  rc = wmi.Get("Win32_Process").Create(setup, , , pid)
  If rc = 0 Then
    LogOutput "    Setup started with PID " & pid & ". ********"
  Else
    LogOutput "    Starting setup failed. (" & rc & ")"
  End If

end function

Function MakeDirectory(Server)
  If objFSO.FolderExists("\\" & Server & "\C$\CCM") Then
     LogOutput "    CCM folder exists."
  Else
     LogOutput "    CCM folder created."
     Set objWMIService = GetObject("winmgmts:\\" & Server & "\root\cimv2:Win32_Process")
     errReturn = objWMIService.Create("cmd.exe /c md c:\CCM", Null, Null, intProcessID)
  End If
end function

Function LogOutput(strMessage)
  Wscript.Echo strMessage
  objLogFile.writeline(strMessage)
end function

Open in new window

0
Comment
Question by:erwin_miranda
  • 3
  • 3
6 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 40348436
Hi, this can be done, in a HTA with VBScript, or with Powershell using Windows Forms, but it would require a complete conversion to Powershell for that.  Using VBScript in a HTA, you could paste this code straight in, and make only a few modifications to form elements in the HTA.  What do you want on the form?  Right now, you would have an input box for the patch file to install, the file to read, and the file to log to.  Then in addition to logging to the file, you can output it to a multiline text box in the HTA.

Have you attempted to build a HTA with those elements on it?

Regards,

Rob.
0
 

Author Comment

by:erwin_miranda
ID: 40354291
If we were to do this in a Powershelll would it be able to be backwards compatible with Powershell 1.0 2.0 3.0?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 40355945
More than likely.  I don't think you'd be using any functions that would have been introduced in later versions.

I am not familiar enough with Powershell to write that quickly, so if you need to post a request for attention to get more Powershell gurus onto this, go ahead and do that.

Rob.
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

Author Comment

by:erwin_miranda
ID: 40356287
So let us try this with VBScript sir...I believe it would best because of the legacy systems that we have...
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 40367515
Hi, sorry for my delay.  I had forgotten about this question.

Please try this HTA.

Regards,

Rob.

<HTA:APPLICATION
	APPLICATIONNAME="PatchInstaller"
	BORDER="dialog"
	SCROLL="no" 
	MAXIMIZEBUTTON="no"
	SINGLEINSTANCE="no"
	CONTEXTMENU="no"
	WINDOWSTATE="normal"
	SINGLEINSTANCE="yes"
/>
<HTML>
  <head>
    <script language="VBScript">
    	Dim strHTAPath, objLogFile, objFSO, strFilename
    	
    	Sub Window_OnLoad
			intWidth = 550
			intHeight = 350
			Me.ResizeTo intWidth, intHeight
		    Me.MoveTo ((Screen.Width / 2) - (intWidth / 2)),((Screen.Height / 2) - (intHeight / 2))

		    If Mid(document.location, 6, 3) = "///" Then
		    	strHTAPath = Mid(Replace(Replace(document.location, "%20", " "), "/", "\"), 9)
		    Else
		    	strHTAPath = Mid(Replace(Replace(document.location, "%20", " "), "/", "\"), 6)
		    End If
		    txt_patchfile.value = Left(strHTAPath, InStrRev(strHTAPath, "\")) & "YourPatch.msi"
		    txt_inputfile.value = Left(strHTAPath, InStrRev(strHTAPath, "\")) & "server.txt"
		    txt_logfile.value = Left(strHTAPath, InStrRev(strHTAPath, "\")) & "InstallLog.txt"
    	End Sub

		Sub HTASleep(intSeconds)
			Set objShell = CreateObject("WScript.Shell")
			objShell.Run "ping 127.0.0.1 -n " & intSeconds + 1, 0, True
		End Sub
		
		Sub BeginInstall
			' ********************************************************************
			' Script is used to installs msi patch on remote computers.
			' Update "Server.txt" with the list of computers to receive the patch.
			' Update variable strFilename below with the filename of the patch.
			' Copy the patch to "D:\Scripts\InstallPatch\"
			' Save this file and run using command line "Cscript InstallPatch.vbs"
			' --------------------------------------------------------------------
			'On Error Resume next
			Dim Cnt
			Set objFSO = CreateObject("Scripting.FileSystemObject")
			
			'**** Update strFilename below with name of patch to install ******
			'    strFilename = "IE11-Windows6.1-KB2977629-x64.msu"
			'    strFilename = "install_flash_player_15_active_x.exe"
			strFilename = txt_patchfile.value
			strReadFile = txt_inputfile.value
			strLogFile = txt_logfile.value
			
			strFileType = Right(strFilename,4)
			Select case strFileType
				Case ".msu", ".msi"
					setup = "wusa.exe ""C:\CCM\" & strFilename & """ /quiet"
				Case ".exe"
					If instr(1,strFilename,"flash",1) > 0 then
						setup = """C:\CCM\" & strFilename & """ -install"
					End if
			End Select

			'MsgBox "Install string: " & strFileType
			
			If objFSO.FileExists(strFilename) = False Then
				MsgBox strFilename & " does not exist."
				Exit Sub
			ElseIf objFSO.FileExists(strReadFile) = False Then
				MsgBox strReadFile & " does not exist."
				Exit Sub
			End If
			
			Cnt = 1
			Set objReadFile = objFSO.OpenTextFile(strReadFile, 1, False)
			Set objLogFile = objFSO.OpenTextFile(strLogFile, 2, True)
			Do Until objReadFile.AtEndOfStream				
				strContents = objReadFile.ReadLine
				If Ping(strContents) = True Then
					LogOutput cnt & ". Accessing " & strContents
					MakeDirectory(strContents)
					CopyFile1(strContents)
					InstallPatch(strContents)
					LogOutput "               "
					Cnt = Cnt + 1
				Else
					LogOutput strContents & " is offline"
				End If
			Loop
			objReadFile.Close
		End Sub

function CopyFile1(Server)
  Const OverwriteExisting = TRUE
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  strRemoteFile = "\\" & Server & "\C$\CCM\" & Mid(strFilename, InStrRev(strFilename, "\") + 1)
  objFSO.CopyFile strFilename, strRemoteFile, OverwriteExisting
  LogOutput "    " & strFilename & " copied to server."
end function

function InstallPatch(Server)
  'Example:  wusa.exe d:\934307\Windows6.0-KB934307-x86.msu /quiet
 ' setup = "wusa.exe ""C:\CCM\" & strFilename & """ /quiet"
LogOutput "    " & setup
  Set wmi = GetObject("winmgmts://" & Server & "/root/cimv2")
  rc = wmi.Get("Win32_Process").Create(setup, , , pid)
  If rc = 0 Then
    LogOutput "    Setup started with PID " & pid & ". ********"
  Else
    LogOutput "    Starting setup failed. (" & rc & ")"
  End If

end function

Function MakeDirectory(Server)
  If objFSO.FolderExists("\\" & Server & "\C$\CCM") Then
     LogOutput "    CCM folder exists."
  Else
     LogOutput "    CCM folder created."
     'Set objWMIService = GetObject("winmgmts:\\" & Server & "\root\cimv2:Win32_Process")
     'errReturn = objWMIService.Create("cmd.exe /c md c:\CCM", Null, Null, intProcessID)
     objFSO.CreateFolder "\\" & Server & "\C$\CCM"
  End If
end function

Function LogOutput(strMessage)
  txt_results.value = txt_results.value & vbCrLf & strMessage
  objLogFile.writeline(strMessage)
end Function

Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

    </script>
  </head>
  <body>
      <table border="0">
		<tr><th colspan='2' align="center"><h2><u>Remote PC Patch Installer</u></h2></th></tr>
		<tr>
			<td>
				<b>Patch File: </b>
			</td>
			<td>
				<input type='text' name='txt_patchfile' size='30'>
			</td>
		</tr>
		<tr>
			<td>
				<b>Input File: </b>
			</td>
			<td>
				<input type='text' name='txt_inputfile' size='30'>
			</td>
		</tr>
		<tr>
			<td>
				<b>Log File: </b>
			</td>
			<td>
				<input type='text' name='txt_logfile' size='30'>
			</td>
		</tr>
		<tr>
			<td colspan='2' align='center'>
				<input type="button" value="Install Patch" name='btn_submit' onClick="vbscript:BeginInstall"/>
			</td>
		</tr>
		<tr>
			<td colspan='2' align='center'>
				<textarea name='txt_results' rows='6' cols='60'></textarea>
			</td>
		</tr>
      </table>
  </body>
</HTML>

Open in new window

0
 

Author Closing Comment

by:erwin_miranda
ID: 40570035
As usual Mr. Sampson...top notch....
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A procedure for exporting installed hotfix details of remote computers using powershell
A brief introduction to what I consider to be the best editor for PowerShell.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question