Nip*_*pun 4 powershell batch-file powershell-2.0
我有一个PowerShell脚本,它在客户机器上安装补丁(包含要添加的文件集).为此,我创建了一个执行此PowerShell脚本的批处理文件.
要使客户运行此批处理文件,PowerShell脚本文件也必须放在客户机器上.
PowerShell脚本采用文本格式,客户可以轻松阅读和理解.
我们可以将此脚本文件转换为某种不可读的格式(例如bin或exe),以便客户无法读取吗?
您可以将脚本转换为Base64编码,以便它不会立即可读.要将PowerShell脚本文件转换为Base64字符串,请运行以下命令:
$Base64 = [System.Convert]::ToBase64String([System.IO.File]::ReadAllBytes('c:\path\to\script file.ps1'));
Run Code Online (Sandbox Code Playgroud)
要启动Base64编码的脚本,可以按如下方式调用PowerShell.exe:
powershell.exe -EncodedCommand <Base64String>
Run Code Online (Sandbox Code Playgroud)
例如,以下命令:
powershell.exe -EncodedCommand VwByAGkAdABlAC0ASABvAHMAdAAgAC0ATwBiAGoAZQBjAHQAIAAiAEgAZQBsAGwAbwAsACAAdwBvAHIAbABkACEAIgA7AA==
Run Code Online (Sandbox Code Playgroud)
将返回以下结果:
Hello, world!
Run Code Online (Sandbox Code Playgroud)
我尝试了@TrevorSullivan 提出的解决方案,但它给了我错误
The term '????' is not recognized as the name of a cmdlet, function,
script file or operable program...
Run Code Online (Sandbox Code Playgroud)
后来我发现编码有问题。我找到了另一种方法,当我将这两者结合起来时,我得到了有效的 PS 命令:
$Base64 = [System.Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes([System.IO.File]::ReadAllText("script.ps1")))
Run Code Online (Sandbox Code Playgroud)
然后我可以将结果重定向到文件:
$Base64 > base64Script.txt
Run Code Online (Sandbox Code Playgroud)
我只是从那里复制编码的命令并将其粘贴到此处而不是<Base64String>:
powershell.exe -EncodedCommand <Base64String>
Run Code Online (Sandbox Code Playgroud)
它可以毫无问题地工作。