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.
Daniel ForresterDirectorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gabriel CliftonNet AdminCommented:
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
Daniel ForresterDirectorAuthor Commented:
Perfect!!
Gabriel CliftonNet AdminCommented:
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
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

Lee W, MVPTechnology and Business Process AdvisorCommented:
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.
Daniel ForresterDirectorAuthor Commented:
I wanted to run the script with admin rights, can you edit it to do that?
Lee W, MVPTechnology and Business Process AdvisorCommented:
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.
Daniel ForresterDirectorAuthor Commented:
But it needs to be login reactive.
Lee W, MVPTechnology and Business Process AdvisorCommented:
So?  Did you read my first comment?  Have you used Scheduled tasks before or looked at them now?
Lee W, MVPTechnology and Business Process AdvisorCommented:
> 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
Daniel ForresterDirectorAuthor Commented:
Apologies, that looks good thanks, I'll try tomorrow and then post the results.
Daniel ForresterDirectorAuthor Commented:
what if the user who logs in didn't have admin rights?
Gabriel CliftonNet AdminCommented:
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

Daniel ForresterDirectorAuthor 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 AdminCommented:
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

Daniel ForresterDirectorAuthor Commented:
That didnt work
Gabriel CliftonNet AdminCommented:
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.
Daniel ForresterDirectorAuthor 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!!
Gabriel CliftonNet AdminCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
DHCP

From novice to tech pro — start learning today.