在 msgbox 中执行 cmd 命令

Do a cmd command in a msgbox

因此,我的目标是在不使用命令提示符的情况下获得一个消息框,告诉我我正在使用的电源计划。

我试过制作一个批处理文件,但它只能执行命令。

start cmd /k powercfg/getactivescheme

当我尝试从命令提示符执行此操作时,它只是在消息框中输出命令文本,而不是实际输出。

msg %username% powerconfg/getactivescheme

提前致谢

像这样尝试(使用 for 在变量中获取命令输出):

@echo off
for /f "tokens=* delims= " %%a in ('powercfg /getactivescheme') do msg * %%a

使用 VBScript MsgBox 的更好示例,它可以具有自定义标题和图标:

@echo off
for /f "tokens=* delims= " %%a in ('powercfg /getactivescheme') do set "body=%%a"
echo MsgBox "%body%",64,"YOUR TITLE" >temp.vbs
cscript //nologo temp.vbs
del temp.vbs

Learn more on VBScript MsgBox

您也可以试试这个批处理文件:


@echo off
Set Title="command in a msgbox"
Title %Title%
@for /f "delims=" %%a in ('powercfg /getactivescheme') do (Set "MSG=%%a")
>"%temp%\%~n0.vbs" ( echo MsgBox "%MSG%",vbInformation,%Title% )
 wscript "%temp%\%~n0.vbs" & Del "%temp%\%~n0.vbs"

编辑:如果您想在两个括号之间提取数据 ( Data Info....) 您可以使用像这样处理 Regex 的 vbscript:

例如我在法国机器上的测试( Utilisation Normale )


@echo off
Set Title="command in a msgbox"
Title %Title%
@for /f "delims=" %%a in ('powercfg /getactivescheme') do (Set "MSG=%%a")
Call :ExtractData "%MSG%" MSG
>"%temp%\%~n0.vbs" ( echo MsgBox "%MSG%",vbInformation,%Title% )
Wscript.exe "%temp%\%~n0.vbs" & Del "%temp%\%~n0.vbs"
Exit
REM------------------------------------------------------------------------
:ExtractData
(
    echo wscript.echo Extract("%~1"^)
    echo Function Extract(Data^)
    echo Dim strPattern,oRegExp,Matches
    echo strPattern = "(?:\()(.*)(?:\))"
    echo Set oRegExp = New RegExp
    echo oRegExp.IgnoreCase = True 
    echo oRegExp.Pattern = strPattern
    echo set Matches = oRegExp.Execute(Data^) 
    echo If Matches.Count ^> 0 Then Extract = Matches(0^).SubMatches(0^)
    echo End Function
)>"%tmp%\%~n0.vbs"
@for /f "delims=" %%a in ('cscript //nologo "%tmp%\%~n0.vbs"') do ( set "%2=%%a")
If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
Exit /B
REM------------------------------------------------------------------------