View paste

User name: Thomas Baumann

Description: MspInstaller 1.0 Quelltext

Date/Time: 04.06.2016 08:46:51

Highlight: Vb

''' <summary>
'''     Application which installes all update files inside of a directory.
'''     Supported file types: .msu and .msp.
'''     Also install.bat will be executed, too.
''' </summary>
''' <remarks>
'''     Search starts in the directory where this application is stored.
'''     All supported files gets installed, first install.bat, followed by *.msu, followed by *.msp.
'''     After that the same for each sub directory.
''' </remarks>
Friend Module App

	Private Count As Integer
	Private Current As Integer
	Private ReadOnly System As String = Environment.GetFolderPath(Environment.SpecialFolder.System)
	Private ReadOnly Is64Bit As Boolean = (IntPtr.Size = 8)

	''' <summary>Entry point of this application.</summary>
	''' <param name="Args">Commandline arguments. Not used.</param>
	Friend Function Main(ByVal Args As String()) As Integer

		' Print header
		Console.ForegroundColor = ConsoleColor.White
		Console.WriteLine("MSP / MSU Installer - tightDev.Net")
		Console.WriteLine("===================================")
		Console.ResetColor()
		Console.WriteLine()

		' Get file count
		Console.WriteLine("Searching for update files...")
		Call CountFiles(Environment.CurrentDirectory, Environment.CurrentDirectory)
		Console.WriteLine()

		' Process all updates
		Call ProcessDirectory(Environment.CurrentDirectory, Environment.CurrentDirectory)

		' Print finished message and wait for key press
		Console.WriteLine()
		Console.ForegroundColor = ConsoleColor.Green
		Console.WriteLine("All updates installed. You should reboot now.")
		Console.WriteLine("Press any key to exit...")
		Console.ResetColor()
		Console.ReadKey()

		Return 0

	End Function

	''' <summary>Processes a directory and it's subdirectory and counts *.msp files.</summary>
	''' <param name="Root">The root directory of updates, used for the console output only.</param>
	''' <param name="Dir">The root directory to start with.</param>
	''' <remarks>This Sub is called by itself to recursively browse subdirectories.</remarks>
	Private Sub CountFiles(ByVal Root As String, ByVal Dir As String)

		Console.WriteLine("Scaning " & Dir.Substring(Root.Length) & "...")

		' Goto subdirectories, skip "x64" on x86 systems and "x86" on x64 systems
		For Each Item As String In IO.Directory.GetDirectories(Dir)
			Dim UpperName As String = Path.GetFileName(Item).ToUpperInvariant
			If _
			  (UpperName = "X64" AndAlso (Not Is64Bit)) OrElse _
			  (UpperName = "X86" AndAlso Is64Bit) Then
				Console.WriteLine("  Skipping " & Item.Substring(Root.Length) & " - wrong architecture (x64/x86)")
				Continue For
			End If
			Call CountFiles(Root, Item)
		Next

		' Enumerate files
		If File.Exists(Path.Combine(Dir, "install.bat")) Then
			Count += 1
		End If
		For Each Item As String In IO.Directory.GetFiles(Dir, "*.msu")
			Count += 1
		Next
		For Each Item As String In IO.Directory.GetFiles(Dir, "*.msp")
			Count += 1
		Next

	End Sub

	''' <summary>Processes a directory and it's subdirectory and installs *.msp files.</summary>
	''' <param name="Root">The root directory of updates, used for the console output only.</param>
	''' <param name="Dir">The root directory to start with.</param>
	''' <remarks>This Sub is called by itself to recursively browse subdirectories.</remarks>
	Private Sub ProcessDirectory(ByVal Root As String, ByVal Dir As String)

		Console.WriteLine("Processing " & Dir.Substring(Root.Length) & "...")

		' Process files
		If File.Exists(Path.Combine(Dir, "install.bat")) Then
			Console.WriteLine("  " & (Current + 1).ToString & "/" & Count.ToString & ": " & "install.bat")	' Print process and file name
			Using p As New Diagnostics.Process()
				With p.StartInfo
					.FileName = Path.Combine(Dir, "install.bat")
					.WorkingDirectory = Path.GetDirectoryName(.FileName)
				End With
				p.Start()
				p.WaitForExit()
			End Using
			Current += 1
		End If
		For Each Item As String In IO.Directory.GetFiles(Dir, "*.msu") ' Get all .msp files
			Console.WriteLine("  " & (Current + 1).ToString & "/" & Count.ToString & ": " & Path.GetFileName(Item))	' Print process and file name
			Using p As New Diagnostics.Process()
				With p.StartInfo
					.FileName = System & Path.DirectorySeparatorChar & "wusa.exe"
					.Arguments = """" & Item & """ /quiet /norestart"
				End With
				p.Start()
				p.WaitForExit()
			End Using
			Current += 1
		Next
		For Each Item As String In IO.Directory.GetFiles(Dir, "*.msp") ' Get all .msp files
			Console.WriteLine("  " & (Current + 1).ToString & "/" & Count.ToString & ": " & Path.GetFileName(Item))	' Print process and file name
			Using p As New Diagnostics.Process()
				With p.StartInfo
					.FileName = System & Path.DirectorySeparatorChar & "msiexec.exe"
					.Arguments = "/p """ & Item & """ /qb"
				End With
				p.Start()
				p.WaitForExit()
			End Using
			Current += 1
		Next

		' Goto subdirectories, skip "x64" on x86 systems and "x86" on x64 systems
		For Each Item As String In IO.Directory.GetDirectories(Dir)
			Dim UpperName As String = Path.GetFileName(Item).ToUpperInvariant
			If _
			  (UpperName = "X64" AndAlso (Not Is64Bit)) OrElse _
			  (UpperName = "X86" AndAlso Is64Bit) Then
				Console.WriteLine("  Skipping " & Item.Substring(Root.Length) & " - wrong architecture (x64/x86)")
				Continue For
			End If
			Call ProcessDirectory(Root, Item)
		Next

	End Sub

End Module

Create new paste