Script error - Missing opening '(' after keyword 'for'

Pete 1 Reputation point
2021-01-21T11:03:57.597+00:00

Hi,

I have a script that i can`t get to work, it is as follows with the error:

Script:

:: Get IP of Domain name
setlocal EnableDelayedExpansion
set myServer=eu-api.mimecast.com
for /f "tokens=1,2 delims=[]" %%a IN ('ping -n 1 !myServer!') DO (if "%%b" NEQ "" set myServerIP=%%b)
echo ip is %myServerIP%
route add %myServerIP% mask 255.255.255.255 <NEXT HOP>
EXIT

Error:

PS H:\> :: Get IP of Domain name
setlocal EnableDelayedExpansion
set myServer=eu-api.mimecast.com
for /f "tokens=1,2 delims=[]" %%a IN ('ping -n 1 !myServer!') DO (if "%%b" NEQ "" set myServerIP=%%b)
echo ip is %myServerIP%
route add %myServerIP% mask 255.255.255.255 <NEXT HOP>
EXIT
At line:4 char:4

  • for /f "tokens=1,2 delims=[]" %%a IN ('ping -n 1 !myServer!') DO (if ...
  • ~
    Missing opening '(' after keyword 'for'.
  • CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
  • FullyQualifiedErrorId : MissingOpenParenthesisAfterKeyword

I hope someone can help :)

regards,

Windows for business Windows Server User experience PowerShell
{count} votes

1 answer

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2021-01-21T14:50:37.287+00:00

    You are running Powershell and trying to execute .bat file commands. That won't work. You have to execute Powershell statements.

    $myServer = 'eu-api.mimecast.com'
    $dns = (Resolve-DnsName -Name $myServer -ErrorAction SilentlyContinue).IPAddress
    if ($dns.count -eq 0 ) { 
        "Unable to resolve DNS name: $myserver"
        return
    }
    if ($dns.count -eq 1 ) {
        $ip = $dns                # $dns is just a single IP address
    } else {
        $ip = $dns[0]            # multiple IP's, $dns is an array         
    }
    
    
    "$myserver IP address is $ip"
    route.exe add $ip mask 255.255.255.255 <NEXT HOP>
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.