Installing via powershell

Alex
Alex used Ask the Experts™
on
@ECHO OFF

setlocal enabledelayedexpansion

::: Find 8x8 v5.1.0.18959
REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{DDF96E66-E39B-4A69-B776-2DE49EBCAFBD}\" /s >NUL 2>&1 | FIND "DisplayVersionVersion=5.1.0.18959"
	IF %ErrorLevel% EQU 0 (
Goto :5.1.0.18959Uninstall
)

::: Uninstall
:5.1.0.18959Uninstall 
MsiExec.exe /X{DDF96E66-E39B-4A69-B776-2DE49EBCAFBD} /qn 2> NUL

::: Find 8x8 v5.2.0.19289
REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{D7D3F00A-638B-41DE-A2A3-FFC6EF034783}\" /s >NUL 2>&1 | FIND "DisplayVersion=5.2.0.19289" > NUL 2> NUL
	IF %ErrorLevel% EQU 0 (
Goto :5.2.0.19289Uninstall
)

::: Uninstall
:5.2.0.19289Uninstall 
MsiExec.exe /X{D7D3F00A-638B-41DE-A2A3-FFC6EF034783} /qn 2> NUL

::: Find 8x8 v5.3.1.19599
REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{B1A6CB20-C032-4228-940F-AC3BC9BF6B3E}\" /s >NUL 2>&1 | FIND "DisplayVersion=5.3.1.19599" > NUL 2> NUL
	IF %ErrorLevel% EQU 0 (
Goto :5.3.1.19599Uninstall
)

::: Uninstall 
:5.3.1.19599Uninstall 
MsiExec.exe /X{B1A6CB20-C032-4228-940F-AC3BC9BF6B3E} /qn 2> NUL

::: Find 8x8 v5.4.0.19820
REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{3EB41C0B-00EF-4C8B-9FF7-FF252E2F4E33}\" /s >NUL 2>&1 | FIND "DisplayVersion=5.4.0.19820" > NUL 2> NUL
	IF %ErrorLevel% EQU 0 (
Goto :5.4.0.19820Uninstall
)

::: Uninstall 
:5.4.0.19820Uninstall 
MsiExec.exe /X{3EB41C0B-00EF-4C8B-9FF7-FF252E2F4E33} /qn 2> NUL

::: Find 8x8 v5.5.0.19923
REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{D8A08493-2F06-4EB2-A636-4392991981AB}\" /s >NUL 2>&1 | FIND "DisplayVersion=5.5.0.19923" > NUL 2> NUL
	IF %ErrorLevel% EQU 0 (
Goto :5.5.0.19923Uninstall
)

::: Uninstall 
:5.5.0.19923Uninstall 
MsiExec.exe /X{D8A08493-2F06-4EB2-A636-4392991981AB} /qn 2> NUL

:::8x8 VOD Latest Version Installation
:Install8x8
::: Copy the latest version of the VOD to the path below and change the msi filename
msiexec /i \\Domain\SYSVOL\DOmain\scripts\8x8\VOD_5_5_0.msi /qn

Open in new window



I'd be interested to know if there is an easier or better way to do this in powershell.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Shaun VermaakTechnical Specialist
Awarded 2017
Distinguished Expert 2018

Commented:
For one, you could refactor the repeating code blocks with a function and a loop
AlexSenior Infrastructure Analyst

Author

Commented:
That's not a bad shout actually, so would it work doing something like a Hash table and then have a foreach for that?
Shaun VermaakTechnical Specialist
Awarded 2017
Distinguished Expert 2018

Commented:
Correct. Especially the Uninstall/Find part. If there is other logic behind which applications those GUIDs belong to, you might even drop the GUID from the script and do a lookup by text (display name) and get the GUID at runtime.

This way it can support multiple versions
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

AlexSenior Infrastructure Analyst

Author

Commented:
Excellent, let me bash something together and I'll report back, thank you for the ideas, I knew it could be done, I just needed to know the best way to achieve it.
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
That batch script will fail all over the place, sorry.
The "DisplayVersionVersion" in line 6 aside:
- You're suppressing the standard output of reg.exe, so find.exe will never find anything.
- You have a "goto" in the "if", but no "else" section, so the script will always process all uninstall commands.
- msiexec.exe is a GUI application, not a command line tool, so it needs to be called using "start /wait", otherwise control will be returned to the script as soon as msiexec is started, without waiting for msiexec to finish.
That can certainly be done in PowerShell, but it's actually just a few basic commands in batch:
@echo off
setlocal enabledelayedexpansion

set GUIDs=DDF96E66-E39B-4A69-B776-2DE49EBCAFBD D7D3F00A-638B-41DE-A2A3-FFC6EF034783 B1A6CB20-C032-4228-940F-AC3BC9BF6B3E 3EB41C0B-00EF-4C8B-9FF7-FF252E2F4E33 D8A08493-2F06-4EB2-A636-4392991981AB
for %%a in (%GUIDs%) do (
	reg.exe query "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{%%a}" >NUL 2>&1
	if not errorlevel 1 (
		echo Uninstalling %%a
		start "Uninstalling %%a" /wait msiexec.exe /X{%%a} /qn
	)
)

:::8x8 VOD Latest Version Installation
::: Copy the latest version of the VOD to the path below and change the msi filename
start "Installing VOD" /wait msiexec.exe /i \\Domain\SYSVOL\DOmain\scripts\8x8\VOD_5_5_0.msi /qn

Open in new window

AlexSenior Infrastructure Analyst

Author

Commented:
oBdA

This isn't my code, I've been asked to fix it :D, I'm shocking with Batch and they asked to fit it into a powershell script.

But i'll give that a shot now.

Thanks
Alex
ste5anSenior Developer

Commented:
Reading the registry: Working with Registry Keys. Then you can call MsiExec using Start-Process:

$result = (Start-Process -FilePath "msiexec.exe" -ArgumentList "/X$Guid" -Wait -Passthru).ExitCode

Open in new window

Just complete the skeleton:

Function Uninstall () {
    Param ( [string] $Guid, [string] $DisplayVersion )
    # TODO: Read registry into $RegistryVersion.
    If ($DisplayVersion -eq $RegistryVersion) {
      # TODO: Call MsiExec.
    }
}

Uninstall Guid="{DDF96E66-E39B-4A69-B776-2DE49EBCAFBD}" DisplayVersion="5.1.0.18959"
Uninstall Guid="{D7D3F00A-638B-41DE-A2A3-FFC6EF034783}" DisplayVersion="5.2.0.19289" 
Uninstall Guid="{B1A6CB20-C032-4228-940F-AC3BC9BF6B3E}" DisplayVersion="5.3.1.19599" 
Uninstall Guid="{3EB41C0B-00EF-4C8B-9FF7-FF252E2F4E33}" DisplayVersion="5.4.0.19820" 
Uninstall Guid="{D8A08493-2F06-4EB2-A636-4392991981AB}" DisplayVersion="5.5.0.19923"  

Open in new window

Normal programs are removed by querying WMI like

$program = Get-WmiObject -Class Win32_Product | 
    Where-Object {  $_.Name -match "Software Name" }
$program.Uninstall()

Open in new window

So maybe you can query your applications also.
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
The batch from above translated to PowerShell:
$guids = @(
	'DDF96E66-E39B-4A69-B776-2DE49EBCAFBD'
	'D7D3F00A-638B-41DE-A2A3-FFC6EF034783'
	'B1A6CB20-C032-4228-940F-AC3BC9BF6B3E'
	'3EB41C0B-00EF-4C8B-9FF7-FF252E2F4E33'
	'D8A08493-2F06-4EB2-A636-4392991981AB'
)
ForEach ($guid In $guids) {
	If (Get-Item -Path "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{$guid}" -ErrorAction SilentlyContinue) {
		Write-Host "Uninstalling $($guid)"
	}
	& msiexec.exe /X{$guid} /qn | Out-Null
}
Write-Host "Installing VOD"
& msiexec.exe /i \\Domain\SYSVOL\DOmain\scripts\8x8\VOD_5_5_0.msi /qn | Out-Null

Open in new window


ste5an,
Win32_Product should be avoided. This is not a simple query, it initiates a consistency check of packages installed, verifying and repairing the install!
Event log message indicates that the Windows Installer reconfigured all installed applications
https://support.microsoft.com/en-us/help/974524/event-log-message-indicates-that-the-windows-installer-reconfigured-al
AlexSenior Infrastructure Analyst

Author

Commented:
Thanks all :D
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Just noticed an error, sorry; line 11 and 12 need to be swapped, in case you haven't noticed it yet (that is, the msiexec call needs to be inside the "If" statement):
$guids = @(
	'DDF96E66-E39B-4A69-B776-2DE49EBCAFBD'
	'D7D3F00A-638B-41DE-A2A3-FFC6EF034783'
	'B1A6CB20-C032-4228-940F-AC3BC9BF6B3E'
	'3EB41C0B-00EF-4C8B-9FF7-FF252E2F4E33'
	'D8A08493-2F06-4EB2-A636-4392991981AB'
)
ForEach ($guid In $guids) {
	If (Get-Item -Path "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{$guid}" -ErrorAction SilentlyContinue) {
		Write-Host "Uninstalling $($guid)"
		& msiexec.exe /X{$guid} /qn | Out-Null
	}
}
Write-Host "Installing VOD"
& msiexec.exe /i \\Domain\SYSVOL\DOmain\scripts\8x8\VOD_5_5_0.msi /qn | Out-Null 

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial