将PowerShell脚本转换为不可读的格式

问题描述 投票:4回答:2

我有一个PowerShell脚本,它在客户机器上安装补丁(包含要添加的文件集)。为此,我创建了一个执行此PowerShell脚本的批处理文件。 要使客户运行此批处理文件,PowerShell脚本文件也必须放在客户机器上。

PowerShell脚本采用文本格式,客户可以轻松阅读和理解。

我们可以将此脚本文件转换为某种不可读的格式(例如bin或exe),以便客户无法读取吗?

batch-file powershell powershell-v2.0
2个回答
6
投票

您可以将脚本转换为Base64编码,以便它不会立即可读。要将PowerShell脚本文件转换为Base64字符串,请运行以下命令:

$Base64 = [System.Convert]::ToBase64String([System.IO.File]::ReadAllBytes('c:\path\to\script file.ps1'));

要启动Base64编码的脚本,可以按如下方式调用PowerShell.exe:

powershell.exe -EncodedCommand <Base64String>

例如,以下命令:

powershell.exe -EncodedCommand VwByAGkAdABlAC0ASABvAHMAdAAgAC0ATwBiAGoAZQBjAHQAIAAiAEgAZQBsAGwAbwAsACAAdwBvAHIAbABkACEAIgA7AA==

将返回以下结果:

Hello, world!

3
投票

我尝试了@TrevorSullivan提出的解决方案,但它给了我错误

The term '????' is not recognized as the name of a cmdlet, function,
script file or operable program...

正如我后来发现编码错误一样。我找到了另一种方法,当我将这两种方法结合起来时,我得到了PS命令:

$Base64 = [System.Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes([System.IO.File]::ReadAllText("script.ps1")))

然后我可以将结果重定向到文件:

$Base64 > base64Script.txt

从那里我只是复制编码的命令并粘贴在这里而不是<Base64String>

powershell.exe -EncodedCommand <Base64String>

它没有任何问题。

© www.soinside.com 2019 - 2024. All rights reserved.