-1

I am trying to pass Netsh some variables and it refuses to accept them. Can you advise how I can call Netsh within a PowerShell script to set IP address, Subnet, DNS etc by using preset variables?

Environment: Windows 2008 r2 with PS 4 - so I don't have NetAdapter modules available. I need to use Netsh

This does not work:

$DNSServer = 10.10.10.1
netsh interface ip set dns name="Local Area Connection" static $DNSServer

This does work:

netsh interface ip set dns name="Local Area Connection" static 10.10.10.1

I want to do this really....

$IP = 10.10.10.3
$SubNet = 255.255.255.0
$GateWay = 10.10.10.100

netsh interface ip set address name="Local Area Connection" static $IP $SubNet $GateWay

many thanks!

henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
blop001
  • 15
  • 1
  • 3
  • Your question needs improvement because you did not say specifically _how_ it didn't work. As Vincent K pointed out in his answer, you need to quote the values of strings when assigning them to variables. – Bill_Stewart Dec 13 '17 at 17:10

1 Answers1

3

Put single quotes around IP address.

$DNSServer = '10.10.10.1'
netsh interface ip set dns name="Local Area Connection" static $DNSServer
wp78de
  • 18,207
  • 7
  • 43
  • 71
Vincent K
  • 1,326
  • 12
  • 19
  • I'd also recommend the OP reading up on use of quotes in PS, there's a good heyscriptingguy article on [understanding-quotation-marks-in-powershell](https://blogs.technet.microsoft.com/heyscriptingguy/2015/06/20/weekend-scripter-understanding-quotation-marks-in-powershell/) which will help – henrycarteruk Dec 13 '17 at 17:40
  • **Thanks very much**, i think i tried "" but it did not work. Your version was perfect thanks! – blop001 Dec 14 '17 at 19:45