【问题标题】:How do I run a .ps1 file via a .vbs file in the same folder?如何通过同一文件夹中的 .vbs 文件运行 .ps1 文件?
【发布时间】:2019-11-15 01:52:54
【问题描述】:

以下文件位于同一文件夹中。
Testing.cmd
Toast notification.ps1
运行 [Toast notification] .vbs

当我运行 Toast notification.ps1 时,它可以工作。但是,当我运行 Run [Toast notification].vbs 时,.ps1 文件没有运行。
以下是 PowerShell 脚本:

[reflection.assembly]::loadwithpartialname("System.Windows.Forms")
[reflection.assembly]::loadwithpartialname("System.Drawing")
# notify.icon type: Information, Warning or Error.
$notify = new-object system.windows.forms.notifyicon
$notify.icon = [System.Drawing.SystemIcons]::Information
$notify.visible = $true
$notify.showballoontip(10,"", "The CPU is hot.", 
[system.windows.forms.tooltipicon]::None)
$notify.dispose()

以下是VBScript:

Path = split(wscript.scriptFullName, wscript.scriptname)(0)
Item1 = Path & "Toast notification.ps1" 
Item2 = Path & "Testing.cmd" 
Set Action = CreateObject("Wscript.shell")
Action.run ("powershell -executionpolicy bypass -file ""& Item1 &""")
Action.run ("""" & Item2 & ""), 0

当我双击 VBScript 文件时,.ps1 文件没有运行。 “Path”可用于运行Testing.cmd,但我无法使其与.ps1 文件一起使用。我该如何解决这个问题?
以下是Testing.cmd文件:

(ncpa.cpl)

【问题讨论】:

  • 编辑问题,并添加更多详细信息。您使用哪个 shell 和什么命令来调用 vbs 脚本?
  • @vonPryz,照你说的做。请再次阅读问题。

标签: powershell vbscript


【解决方案1】:

当您运行 PowerShell 脚本时,它应该看起来更像这样:

Option Explicit

Dim Path : Path = split(wscript.scriptFullName, wscript.scriptname)(0)
Dim Item1 : Item1 = Path & "Toast notification.ps1" 
Dim Item2 : Item2 = Path & "Testing.cmd" 
Dim Action : Set Action = CreateObject("Wscript.shell")
Action.run "powershell -executionpolicy bypass -file " & chr(34) & Item1 & chr(34), 0, true
Action.run "cmd /c " & chr(34) & Item2 & chr(34), 0, true
Set Action = Nothing

如果您的路径包含空格,则 chr(34) 表示引号。我还建议进行更多的错误检查,并检查文件是否存在等。上面的示例也隐藏了 PowerShell 和 CMD 窗口...

【讨论】:

    【解决方案2】:

    您不需要 3 个文件来执行这些命令,只需创建一个 vbscript 文件。 所以,参考this

    你可以这样做:

    Option Explicit
    Dim Ws,Ret,ByPassPSFile,PSFile
    Set Ws = CreateObject("wscript.Shell")
    ByPassPSFile = "cmd /c PowerShell.exe -ExecutionPolicy bypass -noprofile -file "
    Call WritePSFile("Warning","10","'The CPU is hot !'","'The CPU is hot '","'Warning'","10")
    Ret = Ws.run(ByPassPSFile & PSFile,0,True)
    Ret = Ws.run("cmd /c ncpa.cpl",0,True)
    '------------------------------------------------------------------------------------------------------------
    Sub WritePSFile(notifyicon,time,title,text,icon,Timeout) 
    Const ForWriting = 2
    Dim fso,ts,strText
    PSFile = Left(Wscript.ScriptFullName, InstrRev(Wscript.ScriptFullName, ".")) & "ps1"
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.OpenTextFile(PSFile,ForWriting,True)
    strText = strText & "[reflection.assembly]::loadwithpartialname('System.Windows.Forms') | Out-Null;" & VbCrlF
    strText = strText & "[reflection.assembly]::loadwithpartialname('System.Drawing') | Out-Null;" & VbCrlF 
    strText = strText & "$notify = new-object system.windows.forms.notifyicon;" & VbCrlF
    strText = strText & "$notify.icon = [System.Drawing.SystemIcons]::"& notifyicon &";" & VbCrlF 
    strText = strText & "$notify.visible = $true;" 
    strText = strText & "$notify.showballoontip("& time &","& title &","& text &","& icon &");" & VbCrlF 
    strText = strText & "Start-Sleep -s " & Timeout &";" & VbCrlF
    strText = strText & "$notify.Dispose()"
    ts.WriteLine strText
    End Sub
    '-----------------------------------------------------------------------------------------------------------
    

    【讨论】:

      【解决方案3】:

      还有一个绝妙的技巧可以使用通用批处理 (CMD) 启动任何 PowerShell 脚本。诀窍是像 PowerShell 脚本一样命名批处理,例如MyPS_Script.cmdMyPS_Script.ps1。它甚至允许通过批处理向脚本传递参数

      批次:

      @ECHO OFF
      
      REM *****
      REM * Wrapper CMD to start the PowerShell script of the same name
      REM * I.e. if the script is named ServiceRestart.ps1, then the 
      REM * CMD needs to be named ServiceRestart.cmd
      REM *****
      
      PowerShell.exe -Command "& '%~dpn0.ps1' %1 %2 %3 %4 %5 %6 %7"
      
      PAUSE
      

      PS脚本演示

      ForEach ($Arg In $Args) {
          Write-Host "Arguments from cmd" $Arg
      }
      

      【讨论】:

      • 当我运行“Warning--CPU is hot.cmd”时,我收到了这条消息:Warning--CPU is hot.ps1 cannot be loaded because running此系统已禁用脚本。
      • 如果您想绕过这个限制 - 您在 VBScript 示例中执行,请相应地编辑批处理文件:PowerShell.exe -ExecutionPolicy Bypass ...
      • 我的 VBScript 没有显示任何命令窗口,但您的 CMD 脚本会显示。有没有办法去掉窗户?
      • 我不知道。至少不是没有一些3rd party tools
      猜你喜欢
      • 1970-01-01
      • 2017-12-09
      • 2018-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-16
      • 1970-01-01
      相关资源
      最近更新 更多