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
相关产品推荐
相关产品推荐

