PowerShell触发.msi文件直接运行而非将路径存入变量的问题排查及脚本优化咨询
Fixing Your SciTE Installation PowerShell Script
Let's walk through what's causing the unexpected behavior and get your script working correctly:
Key Issues in Your Original Script
- Accidental MSI Execution: When you write
$msiSciteInstallerby itself in the script, PowerShell interprets that as a command to run/launch the file at that path. That's why your MSI is opening immediately instead of just storing the path. You need to usemsiexec.exe(Windows' official MSI installer tool) to trigger the installation programmatically. - Incorrect Variable Increment:
$flag+1 > $nulldoesn't actually increment the$flagvariable—it just calculates the value but doesn't assign it back. Use$flag++instead to properly count detected installations. - Unreliable Version Comparison: Using
-cle(case-sensitive less-than-or-equal) on string version numbers can lead to errors (e.g., string comparison thinks "6.10" < "6.9"). Convert the version string to a[version]type for accurate numeric comparison.
Corrected Script
# Define the path to your SciTE MSI installer $msiSciteInstaller = "path_to.msi" $flag = 0 # Get installed software entries matching SciTE $installedSciTE = Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object { $_.Displayname -match "Scite" } foreach ($entry in $installedSciTE) { $flag++ # Properly increment the detection flag Write-Host " * SciTE Installation Detected " Write-Host " * SciTE version : $($entry.DisplayVersion)" # Convert version string to [version] type for accurate comparison $currentVersion = [version]$entry.DisplayVersion $minimumSafeVersion = [version]"6.0" if ($currentVersion -le $minimumSafeVersion) { Write-Host " * Warning: Vulnerability Detected - Updating SciTE..." # Use msiexec to run the installer silently (adjust switches as needed) Start-Process msiexec.exe -ArgumentList "/i `"$msiSciteInstaller`" /qn" -Wait } else { Write-Host " * SciTE is up to date - No action needed" exit } } if ($flag -eq 0) { Write-Host "SciTE is not installed - Starting installation..." # Silent install command (remove /qn if you want interactive setup) Start-Process msiexec.exe -ArgumentList "/i `"$msiSciteInstaller`" /qn" -Wait }
What Changed?
- Replaced direct variable reference with
msiexec: TheStart-Processcommand runsmsiexec.exewith standard MSI switches:/i: Install mode"$msiSciteInstaller": Quoted path to handle spaces in the installer path/qn: Silent install (no user interface; remove this if you want the setup wizard to appear)-Wait: Makes PowerShell wait for the installation to finish before continuing
- Fixed variable increment:
$flag++correctly counts how many SciTE installations were detected - Improved version comparison: Converting both current version and target version to
[version]ensures proper numeric comparison (e.g., "6.10" is correctly recognized as newer than "6.9") - Cleaned up syntax: Used PowerShell's pipeline formatting for readability, and added meaningful comments
内容的提问来源于stack exchange,提问作者user17033631
相关产品推荐
相关产品推荐

