Calling a bat file with a parameter

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Docfxit
Posts: 30
Joined: 12 Nov 2015 12:42

Calling a bat file with a parameter

#1 Post by Docfxit » 11 Jul 2019 10:57

I'm trying to call a bat file with a parameter.
The bat file does get called. The parameter doesn't.

Code: Select all

 Set "RunFile=IPAddressSet.bat 1"
REG ADD "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce" /V "Set IP Address" /t REG_SZ /F /D "%~dp0%RunFile%"
This is the bat file I am calling:

Code: Select all

@ECHO off
    call :getAdminrights

::set DNS1=8.8.8.8
::set DNS2=8.8.4.4
set DNS1=66.51.205.100
set DNS2=66.51.206.100

cls
:start
ECHO.
ECHO 1. Change Local Area Connection Static IP 192.168.168.9
ECHO 2. Change Wireless Network Connection Static IP 192.168.168.7
ECHO 3. Show Network Name 
ECHO 4. Obtain an IP address automatically
ECHO 5. Exit
set choice=
set /p choice=Enter choice:
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='1' goto con1
if '%choice%'=='2' goto con2
if '%choice%'=='3' goto con3
if '%choice%'=='4' goto autosearch
if '%choice%'=='5' goto end
ECHO "%choice%" is not valid, try again
ECHO.
goto start
:con1
ECHO Connecting Connection 1
netsh interface ip set address "Local Area Connection" static 192.168.168.9 255.255.255.0 192.168.168.168 1
::netsh int ip set dns "local area connection" static {66.51.205.100} primary
::netsh int ip set dns "local area connection" static {66.51.206.100} secondary
    call :SetDNS 
::    call :SetDNSWin10

goto end

:con2
ECHO Connecting Connection 2
netsh interface ip set address "Wireless Network Connection" static 192.168.168.7 255.255.255.0 192.168.168.168 1
goto end

:con3
ECHO Show Network Name 3
netsh interface show interface
goto end

:autosearch
ECHO obtaining auto IP
ipconfig /renew "Local Area Connection"
goto end

:bye
ECHO BYE
goto end

:SetDNS
for /f "tokens=1,2,3*" %%i in ('netsh int show interface') do (
    if %%i equ Enabled (
        echo Changing "%%l" : %DNS1% + %DNS2%
        netsh int ipv4 set dns name="%%l" static %DNS1% primary validate=no
        netsh int ipv4 add dns name="%%l" %DNS2% index=2 validate=no
    )
)

ipconfig /flushdns
goto :eof

:SetDNSWin10
set INTERFACE=Ethernet

netsh int ipv4 set dns name="%INTERFACE%" static %DNS1% primary validate=no
netsh int ipv4 add dns name="%INTERFACE%" %DNS2% index=2

ipconfig /flushdns
goto :eof

:getAdminrights
REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\icacls.exe" "%SYSTEMROOT%\system32\config\system"

set ADMINSCRIPT="%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"
REM --> If error flag set, we do not have admin.
if '%ERRORLEVEL%' NEQ '0' (
    echo Set UAC = CreateObject^("Shell.Application"^) > %ADMINSCRIPT%
    set params = %*:"=""
    echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> %ADMINSCRIPT%

    cscript /nologo %ADMINSCRIPT%
    del %ADMINSCRIPT%
)
goto :eof
:EOF
:end

kwsiebert
Posts: 31
Joined: 20 Jan 2016 15:46

Re: Calling a bat file with a parameter

#2 Post by kwsiebert » 12 Jul 2019 08:54

That batch file doesn't appear to be making use of using any command line parameters. If I'm reading this right, you seem to be expecting the command line parameter to get piped into the set /p, but that's not how it works.

Docfxit
Posts: 30
Joined: 12 Nov 2015 12:42

Re: Calling a bat file with a parameter

#3 Post by Docfxit » 12 Jul 2019 11:40

Ok. So it's not working. I would like to get it working.
Yes I would like to automatically choose option one.
Do you know how to get it to accept option one on the next re-boot?

Thanks,
Docfxit

pieh-ejdsch
Posts: 198
Joined: 04 Mar 2014 11:14
Location: germany

Re: Calling a bat file with a parameter

#4 Post by pieh-ejdsch » 13 Jul 2019 06:43

Your adminscript gets not the variable params but 'params '.
But neither 'params ' nor 'params' are so usable within a command line.

Post Reply