在 PowerShell 脚本中嵌入 VBScript - 一个文件 [英] Embed VBScript in PowerShell Script - One File

查看:26
本文介绍了在 PowerShell 脚本中嵌入 VBScript - 一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在批处理 + VBScript 混合脚本方面学到了很多东西,虽然学习效果很好,但现在是更彻底地学习 PowerShell 的时候了.但是,我最喜欢 Batch/VBScript 解决方案的部分是我可以创建一个 script.cmd 文件来分发.

I recently learned a lot messing with batch + VBScript hybrid scripts and while it was great learning and works, it's time to learn PowerShell more thoroughly. But, my favorite part of Batch/VBScript solution is that I can create a single script.cmd file to distribute.

PowerShell/VBScript 有什么解决方案吗?理想情况下,我想我更喜欢带有嵌入式 VBScript 的 .ps1 脚本,但有兴趣了解我的选择.

Is there any sort of solution with PowerShell/VBScript? Ideally I think I'd prefer a .ps1 script with embedded VBScript, but interested in knowing my options.

目标似乎有些混乱.

  • 单个文件(这是最重要的部分)
  • 扩展名 .ps1.vbs
  • POWERSHELLVBScript 都在单个文件中
  • 奖励:
    • 不写入外部文件
    • 每行开头
    • 必须转义代码中的特殊字符
    • 编码整个脚本部分(开销 CPU 密集型操作)
    • One Single File (This is the most important part)
    • Extension either .ps1 or .vbs
    • Both POWERSHELL and VBScript inside single file
    • Bonus:
      • No writing to external file
      • Prefacing each line
      • Having to escape special characters in code
      • Encoding entire sections of script (overhead CPU intensive operations)

      这是一个理论例子:

      script.{ps1/vbs}:

      <!-- : Begin PS1 script
      $strString = "Hello PowerShell"
      write-host $strString
      
      cscript //nologo "%~f0?.wsf" //job:HELLOWORLD
      exit /b
      PAUSE
      ----- Begin wsf script --->
      <package>
        <job id="HELLOWORLD">
          <script language="VBScript">
            MsgBox "Hello World VBS"
          </script>
        </job>
        <job id="VBS">
          <script language="VBScript">
              'Second Script!
          </script>
        </job>
      </package>
      

      类似的东西-->

      Something like this -->

      https://stackoverflow.com/a/9074483/5079799

      <!-- : Begin batch script
      @ECHO OFF
      CLS
      
      cscript //nologo "%~f0?.wsf" //job:HELLOWORLD
      exit /b
      PAUSE
      ----- Begin wsf script --->
      <package>
        <job id="HELLOWORLD">
          <script language="VBScript">
            MsgBox "Hello World"
          </script>
        </job>
        <job id="VBS">
          <script language="VBScript">
              'Second Script!
          </script>
        </job>
      </package>
      

      推荐答案

      照常创建 VBS 脚本.保存在某个位置,然后将其转换为 Base64.使用字节编码,因此这也适用于二进制文件,并克服了字符编码问题.像这样,

      Create the VBS script as usual. Save in some location and then convert it into Base64. Byte encoding is used so that this will work on binary files too, and overcomes character encoding issues. Like so,

      $Content = Get-Content -Path C:\temp\myScript.vbs -Encoding Byte
      $Base64 = [System.Convert]::ToBase64String($Content)
      $Base64 | Out-File c:\temp\myScript.b64
      

      然后,在您的 Powershell 脚本中,包含 VBS 脚本的编码版本.将 Base64 转换回字符串并将其写入文件.最后,调用cscript运行.vbs.

      Then, in your Powershell script, include the encoded version of the VBS script. Convert Base64 back into string and write it into a file. Finally, call cscript to run the .vbs.

      $Base64 = "ZgB1AG4AYwB0AGkAbwBuACAAR..."
      $Content = [System.Convert]::FromBase64String($Base64)
      Set-Content -Path $env:temp\myScript.vbs -Value $Content -Encoding Byte
      
      & cscript /nologo $env:temp\myScript.vbs 
      

      另一个选项是像这样将 VBScript 嵌入到 here-string 中,

      Another an option is to embed the VBScript in a here-string like so,

      # Paste the VBS in a here string
      $Content = @'
      dim foo
      ...
      '@
      Set-Content -Path $env:temp\myScript.vbs -Value $Content
      & cscript /nologo $env:temp\myScript.vbs 
      

      这篇关于在 PowerShell 脚本中嵌入 VBScript - 一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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