如何在批处理脚本中创建弹出消息?

Gut*_*bon 34 batch-file popup

我需要知道如何在使用VBScript或KiXtart或任何其他外部脚本/编程语言的情况下在批处理脚本中生成弹出消息.我对此毫无头绪......甚至没有起点.我知道NET SEND但在我当前的环境中禁用了Messenger服务.

小智 49

msg * "Enter Your Message"
Run Code Online (Sandbox Code Playgroud)

这有帮助吗?


dbe*_*ham 36

关于LittleBobbyTable的答案 - NET SEND在Vista或Windows 7上不起作用.它已被MSG.EXE取代

有一个适用于所有Windows版本的原始解决方案 - 可以通过启动一个新的cmd.exe窗口来发送原始弹出消息,该窗口在按下一个键后关闭.

start "" cmd /c "echo Hello world!&echo(&pause"
Run Code Online (Sandbox Code Playgroud)

如果希望脚本暂停,直到消息框被解除,则可以添加/ WAIT选项.

start "" /wait cmd /c "echo Hello world!&echo(&pause"
Run Code Online (Sandbox Code Playgroud)


Jam*_*s K 21

您可以利用CSCRIPT.EXEWSCRIPT.EXE(这些已经存在于Windows的每个版本中,我相信,Windows 95),如下所示:

echo msgbox "Hey! Here is a message!" > %tmp%\tmp.vbs
cscript /nologo %tmp%\tmp.vbs
del %tmp%\tmp.vbs
Run Code Online (Sandbox Code Playgroud)

要么

echo msgbox "Hey! Here is a message!" > %tmp%\tmp.vbs
wscript %tmp%\tmp.vbs
del %tmp%\tmp.vbs
Run Code Online (Sandbox Code Playgroud)

您还可以选择更可自定义的PopUp命令.在超时之前,此示例为您提供10秒窗口以单击"确定":

echo set WshShell = WScript.CreateObject("WScript.Shell") > %tmp%\tmp.vbs
echo WScript.Quit (WshShell.Popup( "You have 10 seconds to Click 'OK'." ,10 ,"Click OK", 0)) >> %tmp%\tmp.vbs
cscript /nologo %tmp%\tmp.vbs
if %errorlevel%==1 (
  echo You Clicked OK
) else (
  echo The Message timed out.
)
del %tmp%\tmp.vbs
Run Code Online (Sandbox Code Playgroud)

在他们的上述背景下,无论是cscriptwscript将采取行动一样.当从一个批处理文件,名为BOT cscriptwscript,直到他们完成他们的脚本将暂停该批处理文件,则允许该文件以继续.

从命令提示符手动调用时,cscript不会将控制权返回到命令提示符,直到完成为止,同时wscript将创建一个单独的线程来执行它的脚本,甚至在脚本完成之前将控制权返回给命令提示符.

此线程中讨论的其他方法不会导致批处理文件的执行在等待单击消息时暂停.您的选择将取决于您的需求.

注意:使用此方法,可以使用多个按钮和图标配置来覆盖用户的各种是/否/取消/中止/重试查询:https://technet.microsoft.com/en-us/library/ee156593.aspx

在此输入图像描述


npo*_*aka 16

几种方式(在所有这些方法中,脚本等待按钮按下不同msg.exe).

1)最怪异和最讨厌的 - 它使用IEXPRESS创建小exe,它将创建一个带有单个按钮的弹出窗口(它可以创建两种类型的弹出消息).来自XP及以上版本的每个窗口上的工作:

;@echo off
;setlocal

;set ppopup_executable=popupe.exe
;set "message2=click OK to continue"
;
;del /q /f %tmp%\yes >nul 2>&1
;
;copy /y "%~f0" "%temp%\popup.sed" >nul 2>&1

;(echo(FinishMessage=%message2%)>>"%temp%\popup.sed";
;(echo(TargetName=%cd%\%ppopup_executable%)>>"%temp%\popup.sed";
;(echo(FriendlyName=%message1_title%)>>"%temp%\popup.sed"
;
;iexpress /n /q /m %temp%\popup.sed
;%ppopup_executable%
;rem del /q /f %ppopup_executable% >nul 2>&1

;pause

;endlocal
;exit /b 0


[Version]
Class=IEXPRESS
SEDVersion=3
[Options]
PackagePurpose=InstallApp
ShowInstallProgramWindow=1
HideExtractAnimation=1
UseLongFileName=0
InsideCompressed=0
CAB_FixedSize=0
CAB_ResvCodeSigning=0
RebootMode=N
InstallPrompt=%InstallPrompt%
DisplayLicense=%DisplayLicense%
FinishMessage=%FinishMessage%
TargetName=%TargetName%
FriendlyName=%FriendlyName%
AppLaunched=%AppLaunched%
PostInstallCmd=%PostInstallCmd%
AdminQuietInstCmd=%AdminQuietInstCmd%
UserQuietInstCmd=%UserQuietInstCmd%
SourceFiles=SourceFiles
[SourceFiles]
SourceFiles0=C:\Windows\System32\
[SourceFiles0]
%FILE0%=


[Strings]
AppLaunched=subst.exe
PostInstallCmd=<None>
AdminQuietInstCmd=
UserQuietInstCmd=
FILE0="subst.exe"
DisplayLicense=
InstallPrompt=
Run Code Online (Sandbox Code Playgroud)

2)使用MSHTA.也适用于XP及以上版本的每台Windows机器(尽管OP不需要"外部"语言,这里的jsvascript最小化).应该保存为.bat:

@if (true == false) @end /*!
@echo off
mshta "about:<script src='file://%~f0'></script><script>close()</script>" %*
goto :EOF */

alert("Hello, world!");
Run Code Online (Sandbox Code Playgroud)

或者在一行中:

mshta "about:<script>alert('Hello, world!');close()</script>"
Run Code Online (Sandbox Code Playgroud)

要么

mshta "javascript:alert('message');close()"
Run Code Online (Sandbox Code Playgroud)

要么

mshta.exe vbscript:Execute("msgbox ""message"",0,""title"":close")
Run Code Online (Sandbox Code Playgroud)

3)这里是参数化.bat/jscript混合(应该保存为bat).尽管有OP请求它再次使用jscript但是因为它是一个bat它可以被称为bat文件而不用担心.它使用POPUP允许比更多的更多控制人口MSGBOX.它使用WSH,但不是上面例子中的MSHTA.

 @if (@x)==(@y) @end /***** jscript comment ******
     @echo off

     cscript //E:JScript //nologo "%~f0" "%~nx0" %*
     exit /b 0

 @if (@x)==(@y) @end ******  end comment *********/


var wshShell = WScript.CreateObject("WScript.Shell");
var args=WScript.Arguments;
var title=args.Item(0);

var timeout=-1;
var pressed_message="button pressed";
var timeout_message="timedout";
var message="";

function printHelp() {
    WScript.Echo(title + "[-title Title] [-timeout m] [-tom \"Time-out message\"] [-pbm \"Pressed button message\"]  [-message \"pop-up message\"]");
}

if (WScript.Arguments.Length==1){
    runPopup();
    WScript.Quit(0);
}

if (args.Item(1).toLowerCase() == "-help" ||  args.Item(1).toLowerCase() == "-h" ) {
    printHelp();
    WScript.Quit(0);
}

if (WScript.Arguments.Length % 2 == 0 ) {
    WScript.Echo("Illegal arguments ");
    printHelp();
    WScript.Quit(1);
}

for (var arg = 1 ; arg<args.Length;arg=arg+2) {

    if (args.Item(arg).toLowerCase() == "-title") {
        title = args.Item(arg+1);
    }

    if (args.Item(arg).toLowerCase() == "-timeout") {
        timeout = parseInt(args.Item(arg+1));
        if (isNaN(timeout)) {
            timeout=-1;
        }
    }

    if (args.Item(arg).toLowerCase() == "-tom") {
        timeout_message = args.Item(arg+1);
    }

    if (args.Item(arg).toLowerCase() == "-pbm") {
        pressed_message = args.Item(arg+1);
    }

    if (args.Item(arg).toLowerCase() == "-message") {
        message = args.Item(arg+1);
    }
}

function runPopup(){
    var btn = wshShell.Popup(message, timeout, title, 0x0 + 0x10);

    switch(btn) {
        // button pressed.
        case 1:
            WScript.Echo(pressed_message);
            break;

        // Timed out.
        case -1:
           WScript.Echo(timeout_message);
           break;
    }
}

runPopup();
Run Code Online (Sandbox Code Playgroud)

4)和一个 jscript.net/.bat混合(应该保存为.bat).这次它使用.NET并编译一个.exe可以删除的小文件:

@if (@X)==(@Y) @end /****** silent jscript comment ******

@echo off
::::::::::::::::::::::::::::::::::::
:::       compile the script    ::::
::::::::::::::::::::::::::::::::::::
setlocal


::if exist "%~n0.exe" goto :skip_compilation

:: searching the latest installed .net framework
for /f "tokens=* delims=" %%v in ('dir /b /s /a:d /o:-n "%SystemRoot%\Microsoft.NET\Framework\v*"') do (
    if exist "%%v\jsc.exe" (
        rem :: the javascript.net compiler
        set "jsc=%%~dpsnfxv\jsc.exe"
        goto :break_loop
    )
)
echo jsc.exe not found && exit /b 0
:break_loop



call %jsc% /nologo /out:"%~n0.exe" "%~f0" 
::::::::::::::::::::::::::::::::::::
:::       end of compilation    ::::
::::::::::::::::::::::::::::::::::::
:skip_compilation

::
::::::::::
"%~n0.exe" %*
::::::::
::
endlocal
exit /b 0

****** end of jscript comment ******/

import System;
import System.WIndows;
import System.Windows.Forms

var arguments:String[] = Environment.GetCommandLineArgs();
MessageBox.Show(arguments[1],arguments[0]);
Run Code Online (Sandbox Code Playgroud)

5)最后一次调用powershell创建一个弹出窗口(如果安装了powershell,可以从命令行调用或从批处理调用):

powershell [Reflection.Assembly]::LoadWithPartialName("""System.Windows.Forms""");[Windows.Forms.MessageBox]::show("""Hello World""", """My PopUp Message Box""")
Run Code Online (Sandbox Code Playgroud)

6)虽然msg解决方案已经发布作为答案,但这是一个更好的使用方法:

msg * /self /w "hello world"
Run Code Online (Sandbox Code Playgroud)

/self 是一个未记录的开关,它将强制msg仅将消息发送给当前用户.


小智 5

msg *世界您好

为我工作。