- 為什麼?

- 為什麼?

我已在中輸入建議的解決方案記錄整個批次檔輸出

但只有當我們的批次腳本不要求管理員存取權限或接受任何使用者輸入時才有效。

在我的腳本中,我獲得了管理存取權限,並且還有使用者輸入:

@echo off
:: BatchGotAdmin
::-------------------------------------
REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params = %*:"="
    echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    pushd "%CD%"
    CD /D "%~dp0"
::--------------------------------------
set LOGFILE=batch.log
call :LOG > %LOGFILE%
exit /B
:LOG
REM Set IP address to USB Ethernet
echo "Set IP address to USB Ethernet :"
set /p SystemIP="Enter the System IP address[127.*.*.*]:" 
set /p  SystemSubnetMask="Enter the System Subnetmark[Default:255.255.255.0]:" 
IF [%system_subnetmask%]==[] set /p system_subnetmask=255.255.255.0
set /p SystemGateway="Enter the System Default Gateway[127.*.*.*]:" 
echo "Aviliable Ethernet Interface :"
netsh interface show interface
set /p EthernetInterfaceName="Type the Ethernet Interface Name:" 
netsh interface ip set address %EthernetInterfaceName% static %SystemIP% %SystemSubnetMask% %SystemGateway%
netsh interface ip set dns %EthernetInterfaceName% static 8.8.8.8
netsh interface ip add dnsserver %EthernetInterfaceName% address=4.2.2.2 index=1

答案1

“如何在批次腳本中獲取准入訪問權限後進行日誌記錄?”

  • 關聯在你的問題中以多種方式回答了這個問題。

「我已在下面的連結中輸入了建議的解決方案,但僅當我們的批次腳本不要求管理員存取權限時才有效...

  • 程式碼中的一些更改/編輯是必要的,包括那些使用,並且這會幹擾請求管理員權限的機制。

“...或接受任何用戶輸入。”

  • 您的程式碼中將接收輸入的這些操作是在(透過VBS)取得管理員權限的操作之後進行的,因此如果前一個沒有發生,那麼這一個也不會發生。

無需詳細說明,您的目標重點是獲取批量列出的命令執行的日誌,但在嘗試此操作時,您忽略了屏幕上的輸出,因為您使用了到日誌文件的重定向,但是沒有意識到屏幕(stdout)不會有任何命令輸出,它甚至會癱瘓並等待set /p命令輸入。

這並不會完全破壞您正在運行的程式碼,但它肯定會“掛起”,而不在螢幕上顯示任何訊息,並且它會停留在那裡等待用戶/管理員互動繼續,而這種情況永遠不會發生。


我的建議是進行更改,將註冊所需的操作與接收互動/輸入所需的操作分開:

@echo off

>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system" && goto :gotAdmin

echo/Requesting administrative privileges... & goto :UACPrompt
 
:UACPrompt 
set "_params=" & if not "%*" == "" =;(
     set "_params=%*" 
     call set "_params=%%_params:"=%%"
    );=
    
> "%temp%\getadmin.vbs" =;(
     echo/Set UAC = CreateObject("Shell.Application"^)
     echo/UAC.ShellExecute "%ComSpec%", "/c %~s0 %_params%", "", "runas", 1
    );= && "C:\Windows\System32\cscript.exe" //Nologo "%temp%\getadmin.vbs" 

>nul 2>&1 del /q "%temp%\getadmin.vbs" & exit /b || goto :eOf

:gotAdmin
cd /d "%~dp0"
set "_LogFile=%temp%\batch.log"

echo/Set IP address to USB Ethernet : 
set /p "_SystemIP=Enter the System IP address [127.*.*.*]: " 
set /p "_SystemSubnetMask=Enter the System Subnet Mask [Default: 255.255.255.0]: "

if "%_SystemSubnetMask%" == "" set "_System_SubnetMask=255.255.255.0"
set /p "_SystemGateway=Enter the System Default Gateway [127.*.*.*]: " 

echo/Aviliable Ethernet Interface:
"C:\Windows\System32\netsh.exe" interface show interface | findstr .

set /p "_EthernetInterfaceName=Type the Ethernet Interface Name: " 

:: logging commands after defined in/with admin inputs
:: ----------------------------------------------------
for %%i in (EthernetInterfaceName,SystemIP, SystemSubnetMask, SystemGateway
    )do "%ComSpec%" /v:on /e:on /c "echo/%%~i: !_%%~i!"

2>&1 =;(
      netsh interface show interface | findstr .
      for %%i in (EthernetInterfaceName,SystemIP, SystemSubnetMask, SystemGateway)do "%ComSpec%" /v:on /e:on /c "echo/%%~i: !_%%~i!"
      echo\ "C:\Windows\System32\netsh.exe" interface ip set address %_EthernetInterfaceName% static %_SystemIP% %_SystemSubnetMask% %_SystemGateway%
      echo\ "C:\Windows\System32\netsh.exe" interface ip set dns %_EthernetInterfaceName% static 8.8.8.8
      echo\ "C:\Windows\System32\netsh.exe" interface ip add dnsserver %_EthernetInterfaceName% address=4.2.2.2 index=1
      echo\ "C:\Windows\System32\ipconfig.exe" /flushdns
     );= >"%_LogFile%"

type "%_LogFile%" | clip 
"C:\Windows\System32\timeout.exe" -1

- 為什麼?

雖然可以重定向stderrstdout,也可以重定向stderrstdout同時,不可能的是重定向stdinset /Pstdout到一個文件並同時有stdout螢幕上接收stdin。你的問題指向了這個目標。


指針流

stdin、stdout 和 stderr 全域常數指標是輸入、輸出和錯誤輸出的標準流

stdin  Standard input     
stderr Standard error 
stdout Standard output

Redirection:
stderr  <to>  Null      <use>  2>Nul
stderr  <to>  stdout    <use>  2>&1 
stderr  <to>  File.Log  <use>  2>File.Log

stdout  <to>  Null      <use>  >Nul
stdout  <to>  Null      <use> 1>Nul
stdout  <to>  File.Log  <use>  >File.Log
stdout  <to>  File.Log  <use> 1>File.Log

Redirection stdout and stderr to File.Log:
2>&1  YourCommandHere   >File.Log
2>&1  YourCommandHere  1>File.Log

Set IP address to USB Ethernet : 
Enter the System IP address [127.*.*.*]:               //user input [stdin]
Enter the System Subnet Mask [Default: 255.255.255.0]: //user input [stdin]
Enter the System Default Gateway [127.*.*.*]:          //user input [stdin]
Avaliable Ethernet Interface:
Admin State.    State         Type            Interface Name
-------------------------------------------------------------------------
Enable          Conected      Dedicaed        Wi-Fi
    
Type the Ethernet Interface Name:                      //user input [stdin]

其他資源:

相關內容