【问题标题】:Wrapping Powershell script and files together?将Powershell脚本和文件包装在一起?
【发布时间】:2014-03-31 09:19:41
【问题描述】:

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

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

这可能吗?

感谢您的帮助

【问题讨论】:

    标签: powershell powershell-2.0


    【解决方案1】:

    需要外部文件的 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

    【讨论】:

      猜你喜欢
      • 2018-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多