Link to home
Start Free TrialLog in
Avatar of compdigit44
compdigit44

asked on

Urgent Help need creating a NETSH login script

Here is my problem. I need to create a login script that will update the static DNS and WINS informatino on client workstation ONLY if these settings are already staticalyy assigned.

In a nutshell this script needs to check to see if the DNS settings are DHCP or Static basised. If they are DHCP the script should not run

Please help
Avatar of Britt Thompson
Britt Thompson
Flag of United States of America image

Looks like someone already needed that here:https://www.experts-exchange.com/questions/21326998/Netsh-script-to-check-DHCP-enabled.html

@leew
for /f "tokens=3" %%a in ('netsh interface ip show config ^| find /i "DHCP Enabled"') do set DHCP=%%a
If /i "%dhcp%" == "Yes" (
    netsh interface ip set dns "Local Area Connection" dhcp
) Else (
    netsh interface ip set dns "Local Area Connection" static 10.0.0.3
)

You could actually avoid the environment variable entirely with this:

for /f "tokens=3" %%a in ('netsh interface ip show config ^| find /i "DHCP Enabled"') do (
If /i "%%a" == "Yes" (
    netsh interface ip set dns "Local Area Connection" dhcp
) Else (
    netsh interface ip set dns "Local Area Connection" static 10.0.0.3
)
)
Avatar of Qlemo
Or, more comprehensive,

netsh int ip dump | findstr "dns wins" | findstr source=static && (
  rem your netsh commands here
)

Avatar of compdigit44
compdigit44

ASKER

This is not quite what I was looking for...

I need this script to run ONLY on clients who have STATIC DNS & WINS enteries. please note some clients are set to DHCP for there TCP/IP settings but DNS is startic. I need this script to look for all static DNS & WINS and update these enteries only!!!!!!!!!!!!!!!!!!!!!!!
My script should work for exactly this situation. It's checking the DNS and WINS entries only for being static.
OK this is what my scritp looks like. Right now I just paused on screen and never changes my static DNS / WINS settings

netsh int ip dump | findstr "dns wins" | findstr source=static && (netsh interface ip set dns "Local Area Connection" static 1.2.3.4
netsh interface ip add dns "Local Area Connection" 1.2.3.4 index=2
netsh interface ip set wins "Local Area Connection" static 1.2.3.4
netsh interface ip add wins "Local Area Connection" 1.2.3.4index=2)
Just tried, and it works fine for me. Does it output the config lines found?
Whn I just run the first part of the script:

netsh int ip dump | findstr "dns wins" | findstr source=static
It works and list the connection information "IF" it has static DNS & WINS enteries which is what  want. The problem is this inforamtion is not beening replacement with the correct DNS & WINS information from my scrupt. Don't I need an if statement somewhere??????
No, the && is like an IF. If the previous command "succeeds", i.e. returns a errolevel of 0, the command after && is executed. You can try by using

netsh int ip dump | findstr "dns wins" | findstr source=static && echo --- It is STATIC ---


The complete command as you posted above should give the same output, plus the results of your netsh commands, which should be 4 times "OK!".

Do I need the ( ) ??? in this script?
Right now the script RUN and updates the DNS but it RUNS on my client who have DNS set by DHCP which is what I do not want
You need the () to build a single block executed only if the condition applies (findstr finding "source=static"). If you omit the parentheses, only the first command is executed conditional, the remainder always.
Still now working... the script is working but it is running no matter if the current DNS settings are DHCP or static... :-(
Your are doing something wrong. You confirmed that
netsh int ip dump | findstr "dns wins" | findstr source=static
is displaying the correct lines.
netsh int ip dump | findstr "dns wins" | findstr source=static && echo --- It is STATIC
Should display the same lines, and the text "--- It is STATIC" if any of the entries contain source=static.
netsh int ip dump | findstr "dns wins" | findstr source=static && ^
(
netsh interface ip set dns "Local Area Connection" static 1.2.3.4
netsh interface ip add dns "Local Area Connection" 1.2.3.4 index=2
netsh interface ip set wins "Local Area Connection" static 1.2.3.4
netsh interface ip add wins "Local Area Connection" 1.2.3.4index=2
)
Is applied if at least one of DNS or WINS setting is static.

Do I need the ^ after the &&?
If you write the ( into the next line - yes. Instead you can use

netsh int ip dump | findstr "dns wins" | findstr source=static && (
netsh interface ip set dns "Local Area Connection" static 1.2.3.4
netsh interface ip add dns "Local Area Connection" 1.2.3.4 index=2
netsh interface ip set wins "Local Area Connection" static 1.2.3.4
netsh interface ip add wins "Local Area Connection" 1.2.3.4index=2
)

if you like that more.
I noticed that when this script runs it updates both my Local Area Connection and Network Adapter Local but I do not see Network Adapter Connection listed any where
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This worked perfectly....

Thanks Again