We help IT Professionals succeed at work.

VB Script Issue with Quotes - End of Statement Error

The script below runs a standard netsh command to assign the IP and DNS settings for a network card named "Local" to dhcp.

..............

WScript.Echo "Assigning Address Range"

Set objShell = WScript.CreateObject("Wscript.Shell")
objShell.Run "netsh interface ip set address Local source=dhcp"
objShell.Run "netsh interface ip set dns Local source=dhcp"
Set objShell = Nothing

WScript.Echo "Completed.  Please reboot your laptop."
WScript.Quit

My problem is the network connection in windows is called AHDB LAN but when I change the script to reflect that name, putting AHDB LAN in quotes due to the space, it appears to break the script.

..........

Set objShell = WScript.CreateObject("Wscript.Shell")
objShell.Run "netsh interface ip set address "AHDB LAN" source=dhcp"
objShell.Run "netsh interface ip set dns "AHDB LAN" source=dhcp"

When I run it I get "Expected end of statement".

How can I amend the script to query the correct NIC adapter name.
Comment
Watch Question

Social distance - Don't touch your face - Wash your hands for 20 seconds
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
objShell.Run "netsh interface ip set address " & Asc(34) & "AHDB LAN" & Asc(34) & " source=dhcp"


and similar for the next one.
CERTIFIED EXPERT

Commented:
Here you go:
Dim strInterface
strInterface = "AHDB LAN"

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "netsh interface ip set address " &  Chr(34) & strInterface  & Chr(34)  & " source=dhcp"
objShell.Run "netsh interface ip set dns " & Chr(34) & strInterface & Chr(34) & " source=dhcp"

Open in new window

I also added a variable called "strInterface" for the connection name to make things easier to modify for different connection names.

Or, if you prefer, you can also do it this way:
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "netsh interface ip set address " & """AHDB LAN""" & " source=dhcp"
objShell.Run "netsh interface ip set dns " & """AHDB LAN""" & " source=dhcp"

Open in new window

Explore More ContentExplore courses, solutions, and other research materials related to this topic.