We help IT Professionals succeed at work.

Run a script that fixes my local IP at administrator level

I want to be able to run a script with admin rights that will fix the ip address of my specific user and another one that will put it back to dhcp when I log off, i'm in a windows 7 environment, it's important it's 'user' specific and doesn't work when I log on as another user on the same computer.
Comment
Watch Question

Gabriel CliftonNet Admin
Top Expert 2012

Commented:
You could always use a login / log off batch script that will do that.

set DHCP
netsh interface ip set address "Local Area Connection" dhcp
netsh interface ip set dns "Local Area Connection" dhcp

Set Static
netsh interface ip set address "Local Area Connection" static IP Address Subnet mask Gateway 1
netsh interface ip set dns "local area connection" DNS Server 1
netsh interface ip add dns "local area connection" DNS Server 2 index=2

Author

Commented:
Perfect!!
Gabriel CliftonNet Admin
Top Expert 2012

Commented:
Let me clear that up a little on the static side, looked a little confusing to me.

netsh interface ip set address "Local Area Connection" 1.1.1.5 255.255.254.0 1.1.1.2 1
netsh interface ip set dns "local area connection" 1.1.1.3
netsh interface ip add dns "local area connection" 1.1.1.4 index=2

at the end of the first line, you must have that final 1
Lee W, MVPTechnology and Business Process Advisor
Most Valuable Expert 2013

Commented:
Problem is, such a script won't by itself run as administrator with the capability of actually changing the network settings.  You would have to take the script and configure it using a Scheduled Task that could be told to run with Administrative privilages at logon.  That script would probably have to have a check on the user name so it knows if it should set the address statically or via DHCP and you'd have to have it stored with an account that had administrative credentials (like a service account) otherwise it would stop working as soon as you changed your password.

Author

Commented:
I wanted to run the script with admin rights, can you edit it to do that?
Lee W, MVPTechnology and Business Process Advisor
Most Valuable Expert 2013

Commented:
There's no way to add-in to a script an automated elevation to admin.  That's why I said you need to use a scheduled task.  You can execute a scheduled task from a command line and so long as you configured the task with admin rights, you'd have them for whatever the task runs.

Author

Commented:
But it needs to be login reactive.
Lee W, MVPTechnology and Business Process Advisor
Most Valuable Expert 2013

Commented:
So?  Did you read my first comment?  Have you used Scheduled tasks before or looked at them now?
Lee W, MVPTechnology and Business Process Advisor
Most Valuable Expert 2013

Commented:
> But it needs to be login reactive.

Quoting my first comment:
configure it using a Scheduled Task that could be told to run with Administrative privilages at logon

I further stated you could run a scheduled task from the command line - which was to imply that you can execute it in a script - https://technet.microsoft.com/en-us/library/cc772785%28v=ws.10%29.aspx#BKMK_run

How the task runs

Author

Commented:
Apologies, that looks good thanks, I'll try tomorrow and then post the results.

Author

Commented:
what if the user who logs in didn't have admin rights?
Gabriel CliftonNet Admin
Top Expert 2012

Commented:
If you simply want to elevate for UAC you can add this to the beginning of the script
:::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights
:::::::::::::::::::::::::::::::::::::::::
@echo off
CLS 
ECHO.
ECHO =============================
ECHO Running Admin shell
ECHO =============================

:checkPrivileges 
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges ) 

:getPrivileges 
if '%1'=='ELEV' (shift & goto gotPrivileges)  
ECHO. 
ECHO **************************************
ECHO Invoking UAC for Privilege Escalation 
ECHO **************************************

setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs" 
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs" 
"%temp%\OEgetPrivileges.vbs" 
exit /B 

:gotPrivileges 
::::::::::::::::::::::::::::
:START
::::::::::::::::::::::::::::
setlocal & pushd .

REM Run shell as admin (example) - put here code as you like

Open in new window

Author

Commented:
can you show me an example script if the network setting are as follows?

192.168.42.140
255.255.255.0
192.168.42.1
192.168.42.3
Gabriel CliftonNet Admin
Top Expert 2012

Commented:
I am assuming that
192.168.42.140 is you IP
255.255.255.0 is you net mask
192.168.42.1 is your gateway
192.168.42.3 is your only DNS Server
:::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights
:::::::::::::::::::::::::::::::::::::::::
@echo off
CLS 
ECHO.
ECHO =============================
ECHO Running Admin shell
ECHO =============================

:checkPrivileges 
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges ) 

:getPrivileges 
if '%1'=='ELEV' (shift & goto gotPrivileges)  
ECHO. 
ECHO **************************************
ECHO Invoking UAC for Privilege Escalation 
ECHO **************************************

setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs" 
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs" 
"%temp%\OEgetPrivileges.vbs" 
exit /B 

:gotPrivileges 
::::::::::::::::::::::::::::
:START
::::::::::::::::::::::::::::
setlocal & pushd .

REM Run shell as admin (example) - put here code as you like


netsh interface ip set address "Local Area Connection" 192.168.42.140 255.255.255.0 192.168.42.1 1
netsh interface ip set dns "local area connection" 192.168.42.3

Open in new window

Author

Commented:
That didnt work
Gabriel CliftonNet Admin
Top Expert 2012

Commented:
have you tried just the
netsh interface ip set address "Local Area Connection" 192.168.42.140 255.255.255.0 192.168.42.1 1
netsh interface ip set dns "local area connection" 192.168.42.3
from elevated command prompt

Also, make sure the "Local Area Connection" is the name of your connection, nic.

Author

Commented:
It is my name, I just need a script that i can turn into a batch file and when i run it it changes my ip to the following details

ip: 192.168.42.140
sub: 255.255.255.0
gw: 192.168.42.1
dns: 192.168.42.3

Please help!!
Net Admin
Top Expert 2012
Commented:
I am sorry, that is how I used to do it in Win XP, I did not realize it had changed in Windows 7

netsh interface ip set address name = "local area connection" static 192.168.42.140 255.255.255.0 192.168.42.1
netsh interface ip set dnsservers name = "local area connection" static 192.168.42.3

:::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights
:::::::::::::::::::::::::::::::::::::::::
@echo off
CLS 
ECHO.
ECHO =============================
ECHO Running Admin shell
ECHO =============================

:checkPrivileges 
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges ) 

:getPrivileges 
if '%1'=='ELEV' (shift & goto gotPrivileges)  
ECHO. 
ECHO **************************************
ECHO Invoking UAC for Privilege Escalation 
ECHO **************************************

setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs" 
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs" 
"%temp%\OEgetPrivileges.vbs" 
exit /B 

:gotPrivileges 
::::::::::::::::::::::::::::
:START
::::::::::::::::::::::::::::
setlocal & pushd .

REM Run shell as admin (example) - put here code as you like

netsh interface ip set address name = "local area connection" static 192.168.42.140 255.255.255.0 192.168.42.1
netsh interface ip set dnsservers name = "local area connection" static 192.168.42.3

Open in new window