Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell条件_Powershell_Batch File - Fatal编程技术网

Powershell条件

Powershell条件,powershell,batch-file,Powershell,Batch File,我想在powershell中写这篇文章,但无论我如何尝试,似乎都无法让它工作。我用过“试着抓住”这个词,但它似乎不是我想要的方式 Try { write-host Attempting Reset CD C:\MKC1_ECU_Reuse ./power_on.bat CD C:\UTIL\XDL\XMIT\BIN .\xpc32_nodriver conm2.xpc -con .\xpc32_

我想在powershell中写这篇文章,但无论我如何尝试,似乎都无法让它工作。我用过“试着抓住”这个词,但它似乎不是我想要的方式

Try
   {
        write-host Attempting Reset
        CD C:\MKC1_ECU_Reuse
        ./power_on.bat
        CD C:\UTIL\XDL\XMIT\BIN
        .\xpc32_nodriver conm2.xpc -con
        .\xpc32_nodriver $global:path\$global:customer\$global:customer_reset.xpc
    }
Catch
    {
         Write-warning "Something went wrong!: $_"
         return # script failed
    }
在批处理中,它将如下所示:

Do some task || goto :error
goto :finished

Powershell将尝试执行该任务,然后转到下一行,无论任务是否成功完成。我试图做的任务是连接到EEPROM并写入。如果写入失败,我不想继续我的脚本。我希望能够显示一个错误,告诉操作员有问题。我正在使用我的公司允许我使用的工具,或者我将使用最新的Powershell,允许| |和&&。任何帮助都将不胜感激。

Try/Catch仅适用于提示powershell命令错误。你可以测试

if($LASTEXITCODE-ne 0)
if(-not$?)
以获取错误信息

猜一猜答案:

$result = .\xpc32_nodriver $global:path\$global:customer\$global:customer_reset.xpc
if ($result -like '*error*') { 
   write-warning 'Something went wrong!'
   exit 1
}

在批处理文件中,您可能需要以下内容:

IF %ERRORLEVEL% NEQ 0 ( 
   exit %ERRORLEVEL%
)
这将在
$LASTEXITCODE
变量中将提升的退出代码中继回PowerShell。然后,您可以使用上一个答案中的逻辑


我想他们是在强迫你使用.bat,但最好是你从PowerShell内部启动底层程序。

我也尝试过使用它。无论程序xpc32_nodriver是否成功连接,它始终返回0或True。另外,我没有提到xpc32_nodriver是我用来连接EEPROM的程序。我不确定这是否重要。我想你必须测试该命令的输出。我实际上正在使用Powershell启动程序。问题是,我无法从要启动回Powershell以在IF语句中使用的程序中读取错误级别。