1

I'm trying to create a Powershell script in which it will automate the installation of applications and printers needed for work. Most of the script works great, the only thing that doesn't is a Condition that I've placed to skip a step.

The script uses WinGet to install most of the applications. WinGet requires the Microsoft Desktop App Installer version 1.12.11692.0 or higher for Winget to work. If that version is not installed, then install it and then continue with the script. If it is installed, then continue with the script without prompting the user to install Microsoft Desktop App Installer.

$AppInstallerVersion = Get-AppxPackage Microsoft.DesktopAppInstaller | Select Version

Write-Host "Microsoft Desktop App Installer Version:" $AppInstallerVersion


if ($AppInstallerVersion -eq "1.12.11692.0")
{
 Write-Host "Microsoft Desktop App Installer is equal or higher to the version needed for this script to work, continuing the script."

 .\(pathtoInstallationscript).ps1
}
else
{
Write-Host "Microsoft Desktop App Installer does not meet the minimum version to run this script, a window will now appear to install the version required for this script the run. Click on Update to install it.
Once installed, press the Enter key in the script to continue."

.\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle
pause
.\(PathtoInstallationscript).ps1
}

So far I've tried the following after the -eq part of the condition:

  • Create a 2nd variable that has a string with the version number in it.

  • Type in the version number directly in it (as shown in the example above.)

  • Type in @{Version=1.12.11692.0} instead of a string.

  • Type in the version number without the quotation marks ("")

The odd thing is that it would oddly sometimes work, but when testing out the script a 2nd time, it would just break.

NickLab
  • 21
  • 4
  • 1
    Did you know that there is a type you could use for version numbers like this `[VERSION]'1.12.11692.0'` to be properly compared? ... or did I get something wrong? – Olaf Jul 05 '21 at 19:25
  • I was actually unaware of this! I'll definitely try this out and see if it works! – NickLab Jul 06 '21 at 13:39
  • It didn't seem to work, got an error saying it cannot convert the value. `Cannot convert value "@{Version=1.0.30251.0}" to type "System.Version". Error: "Cannot convert the "@{Version=1.0.30251.0}" value of type "Selected.Microsoft.Windows.Appx.PackageManager.Commands.AppxPackage" to type "System.Version"."` – NickLab Jul 06 '21 at 14:06

2 Answers2

1

You are using an equality but you seem to assume it means equal or greater, and to @Olaf's point you may want to cast the version strings to the [Version] type.

So I would suggest trying something like this:

if ([Version]$AppInstallerVersion -ge [Version]"1.12.11692.0") {
  Write-Host "Microsoft Desktop App Installer is equal or higher to the version needed for this script to work, continuing the script."

 .\(pathtoInstallationscript).ps1
}
mklement0
  • 382,024
  • 64
  • 607
  • 775
Justin
  • 1,303
  • 15
  • 30
  • Hey, yes I wanted to do an equal or greater than but I just wanted to test out with equal first. I've tried using the [Version] but I received an error saying that it cannot convert to type System.Version. `Cannot convert value "@{Version=1.0.30251.0}" to type "System.Version". Error: "Cannot convert the "@{Version=1.0.30251.0}" value of type "Selected.Microsoft.Windows.Appx.PackageManager.Commands.AppxPackage" to type "System.Version"."` – NickLab Jul 06 '21 at 13:54
  • yes if it is a hashtable you need to access the property of the hashtable before you convert. – Justin Jul 07 '21 at 02:46
1

I've solved it, by changing the variable $AppInstallerVersion and the Condition itself. It took a lot of trial and error but it's now working! The hint was that because [Version] couldn't be converted. It wasn't an actually string or System.Version. Rather it was a PSObject so it had to be changed to .Version which only prints out the version number. Which can then be compared to a string.

$AppInstallerVersion = Get-AppxPackage Microsoft.DesktopAppInstaller

if ($AppInstallerVersion.Version -ge "1.12.11692.0")
NickLab
  • 21
  • 4