Link to home
Start Free TrialLog in
Avatar of Alex
AlexFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Installing via powershell

@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.
Avatar of Shaun Vermaak
Shaun Vermaak
Flag of Australia image

For one, you could refactor the repeating code blocks with a function and a loop
Avatar of Alex

ASKER

That's not a bad shout actually, so would it work doing something like a Hash table and then have a foreach for that?
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
Avatar of Alex

ASKER

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.
Avatar of oBdA
oBdA

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

Avatar of Alex

ASKER

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
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.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Alex

ASKER

Thanks all :D
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