1

我正在尝试创建一个 Powershell 脚本,它将自动安装工作所需的应用程序和打印机。大多数脚本都很好,唯一没有的是我放置的一个条件来跳过一个步骤。

该脚本使用 WinGet 安装大部分应用程序。WinGet 需要 Microsoft 桌面应用程序安装程序版本 1.12.11692.0 或更高版本才能使 Winget 工作。如果未安装该版本,请安装它,然后继续执行该脚本。如果已安装,则继续执行脚本而不提示用户安装 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
}

到目前为止,我在条件的 -eq 部分之后尝试了以下操作:

  • 创建第二个变量,其中包含一个带有版本号的字符串。

  • 直接在其中输入版本号(如上例所示。)

  • 输入 @{Version=1.12.11692.0} 而不是字符串。

  • 键入不带引号 ("") 的版本号

奇怪的是,它有时会奇怪地工作,但是当第二次测试脚本时,它就会中断。

4

2 回答 2

1

我通过更改变量 $AppInstallerVersion 和 Condition 本身解决了这个问题。它需要大量的试验和错误,但它现在正在工作!提示是因为 [Version] 无法转换。它实际上不是字符串或 System.Version。相反,它是一个 PSObject,因此必须将其更改为仅打印出版本号的 .Version。然后可以将其与字符串进行比较。

$AppInstallerVersion = Get-AppxPackage Microsoft.DesktopAppInstaller

if ($AppInstallerVersion.Version -ge "1.12.11692.0")
于 2021-07-06T16:58:45.073 回答
1

您正在使用相等,但您似乎认为它意味着相等或更大,并且对于@Olaf 的观点,您可能希望将版本字符串转换为[Version]类型。

所以我建议尝试这样的事情:

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
}
于 2021-07-05T19:28:36.083 回答