【问题标题】:How to run in vbscript an .sh file, or how to execute shell script in vbscript?如何在 vbscript 中运行 .sh 文件,或者如何在 vbscript 中执行 shell 脚本?
【发布时间】:2019-11-15 14:49:27
【问题描述】:

我正在尝试使用 shell 脚本创建 Windows 10 通知,我想用 vbscript 执行它,但它似乎只是提示我在 Microsoft Store 中找到处理此问题的程序。

我尝试使用 vbscript 中的代码执行 .sh 文件,例如 WshShell.Run

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "Notification.sh"

我有一个 Microsoft Store 对话框,询问我如何运行 .sh 文件,但我想运行它,但没有系统程序可以处理它,除了 Windows Shell 控制台。

【问题讨论】:

  • .sh 脚本作为参数传递给bash.exe
  • @DavidRakProgrammingTutorial - 你的帖子没有提到任何这样的事情。我应该读懂你的心思吗? [grin] 另外,文章中列出的流程比您发布的流程更重要。所以……你真的把它变红并试过了吗?
  • @DavidRakProgrammingTutorial 你知道我觉得什么是粗鲁的,当人们懒得去形成一个连贯的问题时。 The answer you have posted 似乎没有回答您提出的问题。你是怎么从shell script到powershell的,是什么?

标签: bash vbscript


【解决方案1】:
WshShell.Run "powershell.exe -nologo -command C:\ShellFile.ps1"

【讨论】:

  • 这不是 shell 脚本。
【解决方案2】:

来的有点晚,但是 - 可以使用 WScript.Shell 的 run 方法 ...

With CreateObject("WScript.Shell")
    .run "cmd.exe /C ""c:\Program Files\Git\bin\sh.exe"" > c:\out.txt --login -i -c d:\yourfolder\bash.sh -R", 0, True
End With

' Read the output and remove the file when done...
Dim strOutput
With CreateObject("Scripting.FileSystemObject")
    strOutput = .OpenTextFile("c:\out.txt").ReadAll()
    .DeleteFile "c:\out.txt"
    WScript.Echo strOutput
End With

所以,这里先通过cmd.exe运行sh.exe,然后sh.exe运行bash.sh命令文件。我们还将控制台输出读入文件(out.txt),然后将其读入变量以捕获输出。另请注意,我们可以通过使用 0 作为 run-method 的第二个参数来隐藏窗口框架。

也可以使用 exec-method 并将输出直接接收到变量中,但是我们无法隐藏窗口...

Dim objShell
Set objShell = WScript.CreateObject ("WScript.shell")

outVar = objShell.exec("cmd.exe /C ""c:\Program Files\Git\bin\sh.exe"" --login -i -c d:/yourfolder/bash.sh -R").stdout.readall
WScript.Echo outVar

Set objShell = Nothing

【讨论】:

    猜你喜欢
    • 2018-08-10
    • 2019-03-28
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多