如何在PowerShell中带参数执行注册表获取的卸载字符串?
在PowerShell中带参数执行MSI卸载字符串的解决方法
问题原因
直接拼接参数后,字符串的引号格式或命令解析逻辑出错,导致cmd无法正确识别完整的卸载命令和参数。
解决方案
方法1:直接调用msiexec传递参数(推荐)
跳过cmd /c,直接在PowerShell中用Start-Process调用msiexec,参数控制更清晰:
# 从卸载字符串中提取产品代码 $productCode = "{123-12323-123213-A6123-123123}" # 执行静默卸载并等待完成 Start-Process msiexec.exe -ArgumentList "/X $productCode /quiet /norestart" -Wait
方法2:修复字符串后通过cmd执行
如果要保留cmd /c方式,需先清理原始字符串的多余引号,再用引号包裹完整命令:
# 移除卸载字符串首尾的多余引号 $uninstallstring = $uninstallstring.Trim('"') # 追加静默参数 $uninstallstring += " /quiet /norestart" # 用三重引号包裹命令,确保cmd正确解析 cmd /c """$uninstallstring"""
方法3:拆分命令与参数执行
将卸载命令拆分为可执行文件和参数列表,避免字符串拼接的格式问题:
# 清理原始字符串并拆分命令与初始参数 $cleanedString = $uninstallstring.Trim('"') $parts = $cleanedString -split ' ', 2 $exe = $parts[0] $originalArgs = $parts[1] # 组合新参数 $newArgs = "$originalArgs /quiet /norestart" # 执行命令 & $exe $newArgs.Split(' ')
内容的提问来源于stack exchange,提问作者user18209625
相关产品推荐
相关产品推荐

