【发布时间】:2021-10-06 16:01:45
【问题描述】:
我希望你一切都好。 我目前面临一些难以理解的行为,您可以在下面找到描述。
这段代码有什么作用?
- 检查 Ins_ProductVersion 的 2 个注册表位置(32 位和 64 位)
- 如果没有找到,则显示消息为 Product Version: Not 找到了!
- 如果找到值,则显示来自的产品版本 Ins_ProductVersion
== 行为描述 ==
如果我在 Powershell ISE 中使用下面的代码,它会按预期工作:
输出提供:
- 产品版本: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
# assuming you want to exit the loop at the first successfull 'hit'
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!"}
如果我用ps2exe把这段代码转换成exe,输出结果就不同了。
ps2exe samples.ps1 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!
希望你能帮我理解问题出在哪里:)
提前致谢。
LEFBE
==[解决方案] == 我发现问题出在哪里。该代码按预期工作,但我的防病毒软件已配置为阻止读取注册表项的未签名应用程序。基于此,无法获取 $hotfix 的值
【问题讨论】:
-
确保在生成可执行文件之前使用 UTF8 编码保存
samples.ps1,否则ps2exe会像现在一样跳闸。 -
您好@MathiasR.Jessen,感谢您的快速答复。您能否指导我确保 samples.ps1 使用 UTF8 编码?
-
您使用什么文本/代码编辑器来编写脚本?
-
已使用 Powershell ISE 编写此脚本
-
你知道比powershell ISE更好的代码编辑器吗?他可以将我的脚本转换为UTF8?
标签: powershell ps2exe-gui