在第二个getCmdPID.bat文件

时间:2018-08-24 13:02:14

标签: batch-file vbscript batch-processing

当我从ADMINTASKS行执行命令时,我想隐藏admin.bat控制台窗口。是否可以在vbscript中提供pid或其他隐藏控制台的方式?

run.vbs

Set oShell = CreateObject ("Wscript.Shell")
Dim strArgs
strArgs = "cmd /c admin.bat"
oShell.Run strArgs, 0, false

admin.bat

@echo off
title=mycmd
echo test
NET SESSION
IF %ERRORLEVEL% NEQ 0 GOTO ELEVATE
GOTO ADMINTASKS

:ELEVATE
CD /d %~dp0
MSHTA "javascript: var shell = new ActiveXObject('shell.application'); shell.ShellExecute('%~nx0', '', '', 'runas', 1);close();"
EXIT

:ADMINTASKS
::(Do whatever you need to do here)
pause
EXIT

getCmdPID.bat

@echo off
for /f "tokens=2 delims=," %%a in (
  'tasklist /v /fo csv ^| findstr /i "mycmd"'
) do (
  set "mypid=%%~a"
)
echo %mypid%
::here should be modifications
start admin.bat
pause

1 个答案:

答案 0 :(得分:0)

您似乎要结束两种可能的情况。

场景1.将两个脚本都隐藏起来,在这里,您将启动run.vbs,它将隐藏两个脚本:

run.vbs

Set oShell = CreateObject ("Wscript.Shell")
oShell.Run "D:\getCmdPID.bat", 0, false

admin.bat

@echo off
title=mycmd
echo test
NET SESSION
IF %ERRORLEVEL% NEQ 0 GOTO ELEVATE
GOTO ADMINTASKS

:ELEVATE
CD /d %~dp0
MSHTA "javascript: var shell = new ActiveXObject('shell.application'); shell.ShellExecute('%~nx0', '', '', 'runas', 1);close();"
EXIT

:ADMINTASKS
::(Do whatever you need to do here)
pause
EXIT

getCmdPID.bat

@echo off
for /f "tokens=2 delims=," %%a in (
  'tasklist /v /fo csv ^| findstr /i "mycmd"'
) do (
  set "mypid=%%~a"
)
echo %mypid%
::here should be modifications
start admin.bat
pause

或者,在方案2中,您希望用户看到getCmDPID.bat文件,但隐藏admin.bat,但是在这里,您将以隐藏模式启动getCmdPID.bat file, which will call run.vbs and in turn will call admin.bat。

run.vbs

Set oShell = CreateObject ("Wscript.Shell")
oShell.Run "D:\admin.bat", 0, false

admin.bat

@echo off
title=mycmd
echo test
NET SESSION
IF %ERRORLEVEL% NEQ 0 GOTO ELEVATE
GOTO ADMINTASKS

:ELEVATE
CD /d %~dp0
MSHTA "javascript: var shell = new ActiveXObject('shell.application'); shell.ShellExecute('%~nx0', '', '', 'runas', 1);close();"
EXIT

:ADMINTASKS
::(Do whatever you need to do here)
pause
EXIT

getCmdPID.bat

@echo off
for /f "tokens=2 delims=," %%a in (
  'tasklist /v /fo csv ^| findstr /i "mycmd"'
) do (
  set "mypid=%%~a"
)
echo %mypid%
::here should be modifications
"D:\run.vbs"
pause