# index.js

const { exec } = require('child_process');
const util = require('node:util');

const execPromise = util.promisify(exec);

const runBothPath = 'runBoth.bat';

const execFiles = 'file1.bat,file2.bat'
const command = `powershell.exe -Command "Start-Process -FilePath '${runBothPath}' -ArgumentList '${execFiles}' -Verb RunAs -Wait"`;

const main = async () => {
  try {
    await execPromise(command)
    console.log('done')
  } catch (error) {
    console.log('error')
  }
}

main()
# file1.bat

@echo off
timeout /t 3
echo "Hello, file1!"
# file2.bat

@echo off
timeout /t 3
echo "Hello, file2!"
# runBoth.bat

chcp 65001
@echo off

for %%f in (%*) do (
  call %~dp0%%f
)
@pause

在批处理执行中索取管理员权限

chcp 65001 > nul

NET SESSION >nul 2>&1
if %errorlevel% neq 0 (
    echo 请求管理员权限...
    powershell -Command "Start-Process '%~dpnx0' -Verb RunAs" >nul
    exit /b
)

@echo 已经获取管理员权限。

@pause

@echo和echo的区别

不加@:显示命令和结果
echo "hello" 输出为:echo "hello" hello

加@:只显示结果,隐藏命令
@echo "hello" 输出为:hello


热饭班长
3.7k 声望434 粉丝

先去做,做出一坨狗屎,再改进。