Home > Software design >  Is it possible for a batch script to 'catch' a powershell error?
Is it possible for a batch script to 'catch' a powershell error?

Time:01-25

This is my batch code and it's works find as long as the url being checked is a valid url, but gets the following error if you put in an invalid url URL Status= ( was unexpected at this time.

@Echo Off
Set CheckUrl=`Powershell.exe -nologo -NoProfile -command "(Invoke-WebRequest -Uri http://exceedtstab.infarmbureau.com/Exceed).StatusCode"`
For /f "usebackqdelims=" %%A in (
    %CheckUrl%
) Do Set UrlStatus=%%A
Echo URL Status=%UrlStatus%
IF %UrlStatus% == 200 ( 
    Echo URL Connected to internet
    goto :eof
) else (
    Echo URL Not connected to internet
)

CodePudding user response:

What are you trying to do? Can you not just curl?

curl -I http://exceedtstab.infarmbureau.com/Exceed

CodePudding user response:

Or you can test the "UrlStatus" variable with double quotes around it, just in case it's blank; then you won't get that error message, "( was unexpected at this time."

IF "%UrlStatus%" == "200" ( 
  •  Tags:  
  • Related