VBScript - 使用 "choice" 命令的条件执行传递 shell 命令
VBScript - Pass shell command using conditional execution with "choice" command
不确定这是否可行,但我正在尝试在一行中编写以下批处理脚本:
@echo off
echo Shutdown initiated...
echo.
choice /c xy /n /t 10 /d y /m "To cancel shutdown press "X""
if errorlevel 2 goto EXEC
if errorlevel 1 goto ABORT
:EXEC
echo.
echo Computer shutting down
timeout /t 10
exit
:ABORT
echo.
echo Shutdown cancelled
timeout /t 10
exit
以上脚本需要通过vbs 运行命令传入cmd。以下是我能得到的最接近的:
option explicit
dim wshell, strcmd
set wshell = createobject("wscript.shell")
if hour(now()) >= 0 and hour(now()) < 6 then
strcmd = "cmd /c @echo off & echo ""Pre-Dawn Shutdown initiated"" & echo. & choice /c xy /n /t 10 /d y /m ""To cancel shutdown press ""X"""" & if errorlevel 2 goto exec & if errorlevel 1 goto abort & :exec & echo. & echo ""Computer shutting down"" & timeout /t 10 & exit & :abort & echo. & echo ""Shutdown cancelled"" & timeout /t 10 & exit"
wshell.run strcmd
end if
上面的工作按预期进行,直到到达选择命令,然后脚本无法正确执行其余部分。非常感谢任何解决此问题的帮助。
考虑到GOTO
command directs a batch script to jump to a labelled line. Hence, you need to rewrite your script using IF… ELSE…
command syntax,您可以在一行中编写批处理脚本,如下所示:
@echo off
echo Shutdown initiated...
echo.
choice /c xy /n /t 10 /d y /m "To cancel shutdown press "X""
if errorlevel 2 (
rem goto EXEC
rem :EXEC
echo.
echo Computer shutting down
timeout /t 10
exit
) else if errorlevel 1 (
rem goto ABORT
rem :ABORT
echo.
echo Shutdown cancelled
timeout /t 10
exit
)
出于调试目的(即查看输出),在下一行中每个 exit
替换为 pause
:
@echo Shutdown initiated...&echo.&choice /c xy /n /t 10 /d y /m "To cancel shutdown press "X""&if errorlevel 2 (echo. &echo Computer shutting down&timeout /t 10&pause) else if errorlevel 1 (echo.&echo Shutdown cancelled&timeout /t 10& pause )
输出:
==> @echo Shutdown initiated...&echo.&choice /c xy /n /t 10 /d y /m "To cancel shutdown pres
s "X""&if errorlevel 2 (echo. &echo Computer shutting down&timeout /t 10&pause) else if erro
rlevel 1 (echo.&echo Shutdown cancelled&timeout /t 10& pause )
Shutdown initiated...
To cancel shutdown press X X
Shutdown cancelled
Waiting for 0 seconds, press a key to continue ...
Press any key to continue . . .
==> @echo Shutdown initiated...&echo.&choice /c xy /n /t 10 /d y /m "To cancel shutdown pres
s "X""&if errorlevel 2 (echo. &echo Computer shutting down&timeout /t 10&pause) else if erro
rlevel 1 (echo.&echo Shutdown cancelled&timeout /t 10& pause )
Shutdown initiated...
To cancel shutdown press X Y
Computer shutting down
Waiting for 0 seconds, press a key to continue ...
Press any key to continue . . .
下一个 VBScript
代码片段稍作修改,再次用于调试目的。
option explicit
On Error GoTo 0
Dim strResult: strResult = Wscript.ScriptName
dim wshell, strcmd, Return
set wshell = createobject("wscript.shell")
if True or (hour(now()) >= 0 and hour(now()) < 6) then
strcmd = "cmd /c @echo Shutdown initiated...&echo." & _
"&choice /c xy /n /t 10 /d y /m ""To cancel shutdown press ""X""""" & _
"&if errorlevel 2 (echo. &echo Computer shutting down&timeout /t 10" & _
"&exit /B 2) else if errorlevel 1 (echo." & _
"&echo Shutdown cancelled&timeout /t 10&exit /B 1)"
strResult = strResult & vbNewLine & strcmd
Return = wshell.run( strcmd, 1, True)
strResult = strResult & vbNewLine & CStr( Return)
end if
'strResult = strResult & vbNewLine &
Wscript.Echo strResult
输出:在第一个 运行 上按下 X
,在后面按下 Y
(或什么都不按下)。
==> cscript //nologo D:\VB_scripts\SO313751.vbs
39313751.vbs
cmd /c @echo Shutdown initiated...&echo.&choice /c xy /n /t 10 /d y /m "To cancel shutdown p
ress "X""&if errorlevel 2 (echo. &echo Computer shutting down&timeout /t 10&exit /B 2) else
if errorlevel 1 (echo.&echo Shutdown cancelled&timeout /t 10&exit /B 1)
1
==> cscript //nologo D:\VB_scripts\SO313751.vbs
39313751.vbs
cmd /c @echo Shutdown initiated...&echo.&choice /c xy /n /t 10 /d y /m "To cancel shutdown p
ress "X""&if errorlevel 2 (echo. &echo Computer shutting down&timeout /t 10&exit /B 2) else
if errorlevel 1 (echo.&echo Shutdown cancelled&timeout /t 10&exit /B 1)
2
==>
这来自帮助中 Win32_OperatingSystem class 主题的 Win32Shutdown 方法 - https://msdn.microsoft.com/en-us/library/aa394058(v=vs.85).aspx.
Dim testResult
Dim WMIServiceObject, ComputerObject
'Now get some privileges
WMIServiceObject = GetObject(
"Winmgmts:{impersonationLevel=impersonate,(Debug,Shutdown)}")
ForEach ComputerObject In WMIServiceObject.InstancesOf("Win32_OperatingSystem")
testResult = ComputerObject.Win32Shutdown(0, 0) 'logoff
If testResult <> 0 Then
MsgBox("Sorry, an error has occurred while trying to perform selected operation")
EndIf
Next
这是 table 表达您想要发生的事情。 (第一个参数)
0 (0x0) Log Off - Logs the user off the computer. Logging off stops all processes associated with the security context of the process that called the exit function, logs the current user off the system, and displays the logon dialog box.
4 (0x4) Forced Log Off (0 + 4) - Logs the user off the computer immediately and does not notify applications that the logon session is ending. This can result in a loss of data.
1 (0x1) Shutdown - Shuts down the computer to a point where it is safe to turn off the power. (All file buffers are flushed to disk, and all running processes are stopped.) Users see the message, It is now safe to turn off your computer. During shutdown the system sends a message to each running application. The applications perform any cleanup while processing the message and return True to indicate that they can be terminated.
5 (0x5) Forced Shutdown (1 + 4) - Shuts down the computer to a point where it is safe to turn off the power. (All file buffers are flushed to disk, and all running processes are stopped.) Users see the message, It is now safe to turn off your computer. When the forced shutdown approach is used, all services, including WMI, are shut down immediately. Because of this, you will not be able to receive a return value if you are running the script against a remote computer.
2 (0x2) Reboot - Shuts down and then restarts the computer.
6 (0x6) Forced Reboot (2 + 4) - Shuts down and then restarts the computer. When the forced reboot approach is used, all services, including WMI, are shut down immediately. Because of this, you will not be able to receive a return value if you are running the script against a remote computer.
8 (0x8) Power Off - Shuts down the computer and turns off the power (if supported by the computer in question).
12 (0xC) Forced Power Off (8 + 4) - Shuts down the computer and turns off the power (if supported by the computer in question). When the forced power off approach is used, all services, including WMI, are shut down immediately. Because of this, you will not be able to receive a return value if you are running the script against a remote computer.
不确定这是否可行,但我正在尝试在一行中编写以下批处理脚本:
@echo off
echo Shutdown initiated...
echo.
choice /c xy /n /t 10 /d y /m "To cancel shutdown press "X""
if errorlevel 2 goto EXEC
if errorlevel 1 goto ABORT
:EXEC
echo.
echo Computer shutting down
timeout /t 10
exit
:ABORT
echo.
echo Shutdown cancelled
timeout /t 10
exit
以上脚本需要通过vbs 运行命令传入cmd。以下是我能得到的最接近的:
option explicit
dim wshell, strcmd
set wshell = createobject("wscript.shell")
if hour(now()) >= 0 and hour(now()) < 6 then
strcmd = "cmd /c @echo off & echo ""Pre-Dawn Shutdown initiated"" & echo. & choice /c xy /n /t 10 /d y /m ""To cancel shutdown press ""X"""" & if errorlevel 2 goto exec & if errorlevel 1 goto abort & :exec & echo. & echo ""Computer shutting down"" & timeout /t 10 & exit & :abort & echo. & echo ""Shutdown cancelled"" & timeout /t 10 & exit"
wshell.run strcmd
end if
上面的工作按预期进行,直到到达选择命令,然后脚本无法正确执行其余部分。非常感谢任何解决此问题的帮助。
考虑到GOTO
command directs a batch script to jump to a labelled line. Hence, you need to rewrite your script using IF… ELSE…
command syntax,您可以在一行中编写批处理脚本,如下所示:
@echo off
echo Shutdown initiated...
echo.
choice /c xy /n /t 10 /d y /m "To cancel shutdown press "X""
if errorlevel 2 (
rem goto EXEC
rem :EXEC
echo.
echo Computer shutting down
timeout /t 10
exit
) else if errorlevel 1 (
rem goto ABORT
rem :ABORT
echo.
echo Shutdown cancelled
timeout /t 10
exit
)
出于调试目的(即查看输出),在下一行中每个 exit
替换为 pause
:
@echo Shutdown initiated...&echo.&choice /c xy /n /t 10 /d y /m "To cancel shutdown press "X""&if errorlevel 2 (echo. &echo Computer shutting down&timeout /t 10&pause) else if errorlevel 1 (echo.&echo Shutdown cancelled&timeout /t 10& pause )
输出:
==> @echo Shutdown initiated...&echo.&choice /c xy /n /t 10 /d y /m "To cancel shutdown pres
s "X""&if errorlevel 2 (echo. &echo Computer shutting down&timeout /t 10&pause) else if erro
rlevel 1 (echo.&echo Shutdown cancelled&timeout /t 10& pause )
Shutdown initiated...
To cancel shutdown press X X
Shutdown cancelled
Waiting for 0 seconds, press a key to continue ...
Press any key to continue . . .
==> @echo Shutdown initiated...&echo.&choice /c xy /n /t 10 /d y /m "To cancel shutdown pres
s "X""&if errorlevel 2 (echo. &echo Computer shutting down&timeout /t 10&pause) else if erro
rlevel 1 (echo.&echo Shutdown cancelled&timeout /t 10& pause )
Shutdown initiated...
To cancel shutdown press X Y
Computer shutting down
Waiting for 0 seconds, press a key to continue ...
Press any key to continue . . .
下一个 VBScript
代码片段稍作修改,再次用于调试目的。
option explicit
On Error GoTo 0
Dim strResult: strResult = Wscript.ScriptName
dim wshell, strcmd, Return
set wshell = createobject("wscript.shell")
if True or (hour(now()) >= 0 and hour(now()) < 6) then
strcmd = "cmd /c @echo Shutdown initiated...&echo." & _
"&choice /c xy /n /t 10 /d y /m ""To cancel shutdown press ""X""""" & _
"&if errorlevel 2 (echo. &echo Computer shutting down&timeout /t 10" & _
"&exit /B 2) else if errorlevel 1 (echo." & _
"&echo Shutdown cancelled&timeout /t 10&exit /B 1)"
strResult = strResult & vbNewLine & strcmd
Return = wshell.run( strcmd, 1, True)
strResult = strResult & vbNewLine & CStr( Return)
end if
'strResult = strResult & vbNewLine &
Wscript.Echo strResult
输出:在第一个 运行 上按下 X
,在后面按下 Y
(或什么都不按下)。
==> cscript //nologo D:\VB_scripts\SO313751.vbs
39313751.vbs
cmd /c @echo Shutdown initiated...&echo.&choice /c xy /n /t 10 /d y /m "To cancel shutdown p
ress "X""&if errorlevel 2 (echo. &echo Computer shutting down&timeout /t 10&exit /B 2) else
if errorlevel 1 (echo.&echo Shutdown cancelled&timeout /t 10&exit /B 1)
1
==> cscript //nologo D:\VB_scripts\SO313751.vbs
39313751.vbs
cmd /c @echo Shutdown initiated...&echo.&choice /c xy /n /t 10 /d y /m "To cancel shutdown p
ress "X""&if errorlevel 2 (echo. &echo Computer shutting down&timeout /t 10&exit /B 2) else
if errorlevel 1 (echo.&echo Shutdown cancelled&timeout /t 10&exit /B 1)
2
==>
这来自帮助中 Win32_OperatingSystem class 主题的 Win32Shutdown 方法 - https://msdn.microsoft.com/en-us/library/aa394058(v=vs.85).aspx.
Dim testResult
Dim WMIServiceObject, ComputerObject
'Now get some privileges
WMIServiceObject = GetObject(
"Winmgmts:{impersonationLevel=impersonate,(Debug,Shutdown)}")
ForEach ComputerObject In WMIServiceObject.InstancesOf("Win32_OperatingSystem")
testResult = ComputerObject.Win32Shutdown(0, 0) 'logoff
If testResult <> 0 Then
MsgBox("Sorry, an error has occurred while trying to perform selected operation")
EndIf
Next
这是 table 表达您想要发生的事情。 (第一个参数)
0 (0x0) Log Off - Logs the user off the computer. Logging off stops all processes associated with the security context of the process that called the exit function, logs the current user off the system, and displays the logon dialog box.
4 (0x4) Forced Log Off (0 + 4) - Logs the user off the computer immediately and does not notify applications that the logon session is ending. This can result in a loss of data.
1 (0x1) Shutdown - Shuts down the computer to a point where it is safe to turn off the power. (All file buffers are flushed to disk, and all running processes are stopped.) Users see the message, It is now safe to turn off your computer. During shutdown the system sends a message to each running application. The applications perform any cleanup while processing the message and return True to indicate that they can be terminated.
5 (0x5) Forced Shutdown (1 + 4) - Shuts down the computer to a point where it is safe to turn off the power. (All file buffers are flushed to disk, and all running processes are stopped.) Users see the message, It is now safe to turn off your computer. When the forced shutdown approach is used, all services, including WMI, are shut down immediately. Because of this, you will not be able to receive a return value if you are running the script against a remote computer.
2 (0x2) Reboot - Shuts down and then restarts the computer.
6 (0x6) Forced Reboot (2 + 4) - Shuts down and then restarts the computer. When the forced reboot approach is used, all services, including WMI, are shut down immediately. Because of this, you will not be able to receive a return value if you are running the script against a remote computer.
8 (0x8) Power Off - Shuts down the computer and turns off the power (if supported by the computer in question).
12 (0xC) Forced Power Off (8 + 4) - Shuts down the computer and turns off the power (if supported by the computer in question). When the forced power off approach is used, all services, including WMI, are shut down immediately. Because of this, you will not be able to receive a return value if you are running the script against a remote computer.