Technical Anshu
Tutorials Blog
为什么要转换

为什么在共享之前将 PowerShell 脚本转换为 EXE

该脚本有效。问题在于其他人。双击时会在记事本中打开 .ps1 文件,如果运行,则会引发红色策略警告,并且对于普通用户来说看起来像是恶意软件。将其转换为 EXE 根本与脚本无关——而是与接收它的人有关。

By Technical AnshuWindows 10 / 114 minute read
观看视频,了解脚本成为任何人都可以运行的程序的那一刻

管理员根据代码的功能来判断工具。用户通过点击时发生的情况来判断它们。 Between those two views sits the entire reason script-to-EXE tools exist.如果您曾经通过右键单击,使用 PowerShell 运行,输入 Y,按 Enter 与同事交谈过,而他们则向您读回红色错误文本,那么您已经知道此页面所涉及的问题。

The same tool, two ways of handing it over
DiskCleanup.ps1 - a text fileOpens in Notepad
Steps to run it - policy, right-click, Run with PowerShell5 chances to give up
DiskCleanup.exe - one program fileDouble-click and done
剧本根本没有改变——只是接收者的经历发生了变化。面板值是说明性的。
1

双击测试您的脚本失败

将 .ps1 文件发送给普通用户并观察会发生什么。 They double-click it - and it opens in Notepad, because Windows treats scripts as documents, not programs.什么也没跑。从用户的角度来看,你的工具坏了。

如果他们以某种方式找到“使用 PowerShell 运行”,他们就会遇到执行策略警告,即一堵红色的文本墙,读起来就像病毒警报一样。大多数人都会关闭它并给您打电话。剧本很完美。交付每一步都失败。

2

EXE 对接收器有何更改

  1. 一个文件,一个操作:双击,它就会运行 - 与他们使用的所有其他程序相同的心理模型
  2. 可识别的图标而不是空白脚本页面,因此它可以存在于桌面或共享文件夹上
  3. 没有政策提示供用户解读,也不会丢失任何说明文档
  4. 内置可选的 UAC 提示,因此需要管理员权限的工具会在开始时清楚地询问,而不是在中间死掉

That is the whole honest case for converting: it is a kindness to the person on the other end, and it cuts your support calls to near zero.

3

最有回报的情况

帮助台交接。您每周运行的修复 - 清除卡住的打印队列、重置配置文件、收集诊断信息 - 成为任何支持同事无需了解 PowerShell 即可运行的文件。

Branch offices and family PCs. People you cannot sit next to get a tool that cannot be run wrong: there is only one way to start an EXE.

Repeated small tools. Anything a user must run more than once deserves a desktop icon, not a path name read out over the phone.

4

转换不能给您带来什么

保持诚实的期望。 EXE 不会使脚本更快,不会添加功能,并且不会保护其中的代码 - 源代码可以被读回,这在本集中的其自己的页面中进行了解释。 It also does not remove the need for the script to be correct: a bad script in an EXE is just a bad program that is harder to debug.

And if your audience is other admins, skip the conversion entirely. They can read a script, they distrust black boxes, and for scheduled jobs and Group Policy the plain .ps1 is the right format anyway.

观看视频中的完整过程

Watch the video for the live conversion and the before-and-after difference for the end user.

Watch Full Video
常见问题解答

如果 EXE 只是打包,为什么不发送快捷方式或批处理文件呢?

批处理启动器仍然会闪烁控制台窗口,仍然依赖于留在同一位置的 .ps1 文件,并且仍然显示用户脚本机制。 The converted EXE is one self-contained file with its own icon and name, which is what makes non-technical users treat it like a normal program.

转换后的 EXE 是否可以在禁用脚本执行的 PC 上运行?

一般来说是的,因为用户正在启动一个程序,而不是要求 PowerShell 运行脚本文件 - 这是管理员转换的实际原因之一。您组织自己的应用程序控制策略仍然适用,并且它们应该:这是一个打包工具,而不是隐藏您自己的 IT 规则的策略旁路。

转换会让我的脚本运行得更快吗?

不会。相同的脚本在相同的 PowerShell 引擎上运行; EXE 只包装它。如果说有什么不同的话,那就是包装器准备脚本时的启动成本很小。转换是为了可用性和分发,而不是为了速度。

本指南基于技术安数 PowerShell 到 EXE 视频 - 遇到任何步骤?在视频评论中提问,你会得到回复。