将 Powershell 脚本和文件包装在一起? [英] Wrapping Powershell script and files together?

查看:25
本文介绍了将 Powershell 脚本和文件包装在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 PS2EXE 将我的 powershell 脚本编译成可执行文件,确实效果很好!

I'm currently using PS2EXE to compile my powershell script into an executable, works very well indeed!

我的问题是这个脚本依赖于其他文件/文件夹.因此,与其将这些文件与 exe 一起使用,我还希望将这些文件与 PS 脚本一起包装"到 exe 中.运行 exe 将运行 PS 脚本,然后提取这些文件/文件夹并将它们移出 exe...

My problem is that this script relies on other files/folders. So instead of having these out with the exe I want to also have these files 'wrapped' up into the exe along with the PS script. Running the exe will run the PS script then extract these files/folders and move them out of the exe...

这甚至可能吗?

感谢您的帮助

推荐答案

需要外部文件的 Powershell 脚本可以通过将数据嵌入其中来自我维持.通常的方法是将数据转换为 Base64 格式,并在 Powershell 脚本中将其保存为字符串.在运行时,通过解码 Base64 数据创建新文件.

A Powershell script that requires external files can be self-sustained by embedding the data within. The usual way is to convert data into Base64 form and save it as strings within the Powershell script. At runtime, create new files by decoding the Base64 data.

# First, let's encode the external file as Base64. Do this once.
$Content = Get-Content -Path c:\some.file -Encoding Byte
$Base64 = [Convert]::ToBase64String($Content)
$Base64 | Out-File c:\encoded.txt


# Create a new variable into your script that contains the c:\encoded.txt contents like so,
$Base64 = "ABC..."


# Finally, decode the data and create a temp file with original contents. Delete the file on exit too.
$Content = [Convert]::FromBase64String($Base64)
Set-Content -Path $env:temp\some.file -Value $Content -Encoding Byte

完整的示例代码 可用 在博客上.

The full sample code is avalable on a blog.

这篇关于将 Powershell 脚本和文件包装在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆