Wrapping Powershell script and files together?

谁说胖子不能爱 提交于 2019-12-04 05:07:12

问题


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

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...

Is this even possible?

Thanks for your help


回答1:


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.



来源:https://stackoverflow.com/questions/22757851/wrapping-powershell-script-and-files-together

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!