gpt4 book ai didi

powershell - 将 Powershell 脚本和文件包装在一起?

转载 作者:行者123 更新时间:2023-12-03 00:02:14 26 4
gpt4 key购买 nike

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

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

这甚至可能吗?

谢谢你的帮助

最佳答案

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

# 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

完整示例代码 is avalable在博客上。

关于powershell - 将 Powershell 脚本和文件包装在一起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22757851/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com