仅在没有错误的情况下显示一条消息

时间:2019-03-08 17:55:56

标签: powershell batch-file

我目前有一个批处理脚本,该脚本使用powershell解压缩文件。

powershell Expand-Archive C:\File1\File22.zip -DestinationPath C:\File1\File2\

如果解压缩zip文件没有错误,我想运行以下命令:

echo set WshShell = WScript.CreateObject("WScript.Shell") > %tmp%\tmp.vbs
echo WScript.Quit (WshShell.Popup( "Updating! Please wait... " ,10 ,"Update", 0)) >> %tmp%\tmp.vbs
cscript /nologo %tmp%\tmp.vbs
if %errorlevel%==1 (
  echo You Clicked OK
) else (
  echo The Message timed out.
)
del %tmp%\tmp.vbs

如何在代码中添加if else语句?

1 个答案:

答案 0 :(得分:1)

您需要先执行powershell命令,然后检查并确保它以错误代码0(成功执行)退出。

powershell Expand-Archive C:\File1\File22.zip -DestinationPath C:\File1\File2\
If %ERRORLEVEL% == 0 (
    echo set WshShell = WScript.CreateObject("WScript.Shell") > %tmp%\tmp.vbs
    echo WScript.Quit (WshShell.Popup( "Updating! Please wait... " ,10 ,"Update", 0)) 
        >> %tmp%\tmp.vbs
    cscript /nologo %tmp%\tmp.vbs
    if ERRORLEVEL 1 (
        echo You Clicked OK
    ) else (
        echo The Message timed out.
    )
del %tmp%\tmp.vbs
)