2

I want to call a simple Powershell script file that should return either 0 or 1 upon the ps script failure or pass respectively, from a batch file . And based upon the return value, I want to continue further or quit.

The following illustrates what I have tried out:

startup.bat:-

@ECHO OFF
SET x = powershell C:\Users\saravind\Desktop\pingping.ps1
if "%x%" == 1 then (
    doSomething1
    ECHO Ran project 1
)

pingping.ps1:-

$someIP = '192.168.173.299'
$result = $true
try { Test-Connection $someIP -Source localhost -ErrorAction Stop }
catch { $result = $false }

What I'm trying to achieve is that on the execution of pingping.ps1 if it can successfully ping the '192.168.173.299', it should return some value, say 1 to the batch file from which it was being called. And if the ping fails, it should return 0 so that the batch file will not proceed with 'doSomething' Eventhough the pingping fails, it proceeds with doSomething with my code. What's wrong with my code.

Thanks in advance.

4

2 に答える 2

3

以下は私にとってはうまくいきます。powershellスクリプトが成功したことを示すために0を使用し、エラーが発生したことを示すために1を使用しました。

startup.bat:

@echo off
powershell C:\Users\saravind\Desktop\pingping.ps1

if %errorlevel% equ 0 (
    doSomething1
    ECHO Ran project 1
)

pingping.ps1:

$someIP = '192.168.173.299'
try {
    Test-Connection $someIP -Source localhost -ErrorAction Stop
}
catch {
    exit 1
}
exit 0
于 2013-10-24T08:55:14.677 に答える