PowerShell新手求助:为软件自动下载安装脚本添加已安装检测逻辑
嘿,作为PowerShell新手能写出自动批量安装脚本已经超棒了!要实现「检测软件已安装则跳过并提示,未安装则下载安装」的逻辑,核心是针对每个软件的特性(比如注册表卸载项、安装目录、可执行文件路径)来做存在性校验——因为不同软件的安装记录存储位置不一样。
下面是给你优化后的完整脚本,我已经加入了每个软件的检测逻辑,还有详细注释帮你理解:
# 定义所有需要安装的软件信息,新增DetectionScript字段用于检测是否已安装 [Hashtable[]]$Installers = @( # Firefox 检测:通过注册表卸载项查找 @{ SoftwareName = "Firefox" Url = "https://ftp.mozilla.org/pub/firefox/releases/11.0/win32/enUS/Firefox%20Setup%2011.0.exe" Destination = "C:\Users\cdac\Desktop\Shravan\Softwares\firefox.exe" Arguments = '/s' DetectionScript = { # 检查32位和64位注册表卸载项 $uninstallPaths = @( "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*", "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" ) (Get-ItemProperty -Path $uninstallPaths -ErrorAction SilentlyContinue).DisplayName -match "Mozilla Firefox" } }, # CodeBlocks 检测:通过注册表卸载项查找 @{ SoftwareName = "CodeBlocks" Url = "https://sourceforge.net/projects/codeblocks/files/Binaries/20.03/Windows/codeblocks-20.03-setup.exe" Destination = "C:\Users\cdac\Desktop\Shravan\Softwares\codeblocks.exe" Arguments = '/s' DetectionScript = { $uninstallPaths = @( "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*", "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" ) (Get-ItemProperty -Path $uninstallPaths -ErrorAction SilentlyContinue).DisplayName -eq "Code::Blocks" } }, # x64dbg 检测:检查解压目标目录是否存在(绿色版) @{ SoftwareName = "x64dbg" Url = "https://github.com/x64dbg/x64dbg/releases/download/snapshot/snapshot_2021-07-01_23-17.zip" Destination = "C:\Users\cdac\Desktop\Shravan\Softwares\xdbg.zip" ExtractPath = "C:\Program Files\x64dbg" # 自定义解压路径,可修改 DetectionScript = { Test-Path -Path $this.ExtractPath } }, # Python 检测:检查注册表中的安装路径或PATH中的可执行文件 @{ SoftwareName = "Python 3.9.6" Url = "https://www.python.org/ftp/python/3.9.6/python-3.9.6-amd64.exe" Destination = "C:\Users\cdac\Desktop\Shravan\Softwares\python.exe" Arguments = '/quiet InstallAllUsers=1 PrependPath=1' # 静默安装并添加到PATH DetectionScript = { # 方法1:检查PATH中是否有python.exe $pythonPath = Get-Command "python.exe" -ErrorAction SilentlyContinue # 方法2:检查注册表中的Python安装项 $regPath = "HKLM:\Software\Python\PythonCore\3.9\InstallPath" $regExists = Test-Path -Path $regPath -ErrorAction SilentlyContinue $pythonPath -ne $null -or $regExists } }, # NASM 检测:检查PATH中的可执行文件或注册表卸载项 @{ SoftwareName = "Netwide Assembler (NASM)" Url = "https://www.nasm.us/pub/nasm/releasebuilds/2.15.05/win64/nasm-2.15.05-installer-x64.exe" Destination = "C:\Users\cdac\Desktop\Shravan\Softwares\nasm.exe" Arguments = '/s' DetectionScript = { # 检查PATH中的nasm.exe $nasmPath = Get-Command "nasm.exe" -ErrorAction SilentlyContinue # 同时检查注册表卸载项 $uninstallPaths = @( "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*", "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" ) $regExists = (Get-ItemProperty -Path $uninstallPaths -ErrorAction SilentlyContinue).DisplayName -match "NASM" $nasmPath -ne $null -or $regExists } } ) function Install-Software([Hashtable]$installer) { # 执行检测脚本,判断软件是否已安装 $isInstalled = & $installer.DetectionScript if ($isInstalled) { Write-Host "✅ 该软件已安装:$($installer.SoftwareName)" -ForegroundColor Green return } # 未安装则执行下载 Write-Host "📥 正在下载:$($installer.SoftwareName)" -ForegroundColor Cyan Invoke-WebRequest -Uri $installer.Url -OutFile $installer.Destination -ErrorAction Stop # 根据软件类型执行安装或解压 Write-Host "🔧 正在安装/部署:$($installer.SoftwareName)" -ForegroundColor Cyan if ($installer.SoftwareName -eq "x64dbg") { # 处理x64dbg的zip解压 Expand-Archive -Path $installer.Destination -DestinationPath $installer.ExtractPath -Force -ErrorAction Stop Write-Host "✅ $($installer.SoftwareName) 已解压到:$($installer.ExtractPath)" -ForegroundColor Green } else { # 执行exe安装程序 Start-Process -FilePath $installer.Destination -ArgumentList $installer.Arguments -Wait -NoNewWindow -ErrorAction Stop Write-Host "✅ $($installer.SoftwareName) 安装完成!" -ForegroundColor Green } } # 遍历所有软件,执行安装逻辑 foreach ($installer in $Installers) { try { Install-Software -installer $installer } catch { Write-Host "❌ 处理$($installer.SoftwareName)时出错:$($_.Exception.Message)" -ForegroundColor Red } }
关键修改点说明:
- 新增检测逻辑:每个软件项添加了
DetectionScript脚本块,针对不同软件的特性做针对性检测(注册表、目录、PATH) - x64dbg特殊处理:因为是绿色压缩包,新增了
ExtractPath字段定义解压路径,用Expand-Archive命令自动解压 - 错误处理:添加了
try/catch块捕获下载或安装过程中的错误,避免一个软件失败导致整个脚本终止 - 优化Python安装参数:把原来的
/s改成/quiet InstallAllUsers=1 PrependPath=1,确保静默安装并将Python添加到系统PATH中 - 友好输出:用不同颜色区分提示信息,更直观
可自定义的地方:
- 所有软件的
Destination(下载路径)和ExtractPath(x64dbg解压路径)可以根据你的实际需求修改 - 如果某个软件的检测逻辑不准确,可以调整
DetectionScript里的代码(比如修改注册表项名称、路径等)
内容的提问来源于stack exchange,提问作者Shravan Meghavath




