You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell的.ps1脚本与ps2exe转换后exe运行行为差异问询

PowerShell脚本转exe后运行异常问题排查

脚本功能逻辑

  • 分别在32位、64位两个注册表路径下读取Ins_ProductVersion值
  • 若两个路径均未读取到对应值,则输出「Product Version: Not found!」
  • 若成功读取到值,则输出获取到的Ins_ProductVersion版本号

直接运行表现

将脚本在PowerShell ISE中运行时,功能完全符合预期,输出结果为:

  • Product Version: 11.7.0.669

原始脚本代码如下:

foreach ($path in 'HKLM:\SOFTWARE\KasperskyLab\protected\KES\environment\', 'HKLM:\SOFTWARE\WOW6432Node\KasperskyLab\protected\KES\environment\') {
try {
$hotfix = Get-ItemPropertyValue -Path $path  -Name 'Ins_ProductVersion' -ErrorAction SilentlyContinue
# 匹配到第一个有效结果即退出循环
if ($hotfix) { break }
}
catch { 
    #Write-Warning $_.Exception.Message
    #Write-Host "Unable to find $hotfix"
}} 

if ($hotfix) {write-host "- Product Version: $hotfix"}
else {write-host "- Product Version: Not found!"}

转exe后异常表现

使用ps2exe工具将该脚本转换为exe可执行文件,转换命令为:ps2exe samples.ps1 test.exe。运行生成的test.exe结果异常,报错信息如下:

.\test.exe
.\test.exe : ERROR: Impossible de trouver une variable nomm‚e ®ÿhotfixÿ¯.
Au caractère Ligne:1 : 1
+ .\test.exe
+ ~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (ERROR: Impossib...m‚e ®ÿhotfixÿ¯.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError

- Product Version: Not found!

注:报错中法语内容含义为「无法找到名为hotfix的变量」

问题根因

代码本身运行逻辑无异常,异常是由于杀毒软件配置了拦截未签名应用读取注册表的规则,导致$hotfix变量无法被正常赋值。


内容的提问来源于stack exchange,提问作者LEFBE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 08:45:04