{"id":2787,"date":"2018-09-29T18:33:41","date_gmt":"2018-09-29T23:33:41","guid":{"rendered":"http:\/\/osric.com\/chris\/accidental-developer\/?p=2787"},"modified":"2018-09-29T18:33:41","modified_gmt":"2018-09-29T23:33:41","slug":"setting-a-static-ip-default-gateway-and-nameservers-via-powershell","status":"publish","type":"post","link":"https:\/\/osric.com\/chris\/accidental-developer\/2018\/09\/setting-a-static-ip-default-gateway-and-nameservers-via-powershell\/","title":{"rendered":"Setting a static IP, default gateway, and nameservers via PowerShell"},"content":{"rendered":"<p>I needed to set up a number of Windows server VMs (Windows 2012R2) as a test bed for a vulnerability scanning suite. This would have been fast &#038; easy using AWS EC2 instances (or Azure!), but I decided to use my internal VMWare infrastructure instead.<\/p>\n<p>For CentOS VMs I would typically use one of three things to configure the static IP, gateway, and default nameservers:<\/p>\n<ul>\n<li><code>nmtui<\/code> (a text user interface to the network manager)<\/li>\n<li>the interactive installer<\/li>\n<li>a custom kickstart file<\/li>\n<\/ul>\n<p>How to accomplish the same thing on Windows 2012R2? In particular, I was looking for Powershell commands, since I would be connecting over a web-based console.<br \/>\n<!--more--><\/p>\n<p>The first command I found was <code>Set-NetIPAddress<\/code>. I combined that with a blog post on Microsoft&#8217;s TechNet site with a promising title: <a href=\"https:\/\/blogs.technet.microsoft.com\/ken_brumfield\/2014\/06\/23\/one-liner-powershell-to-set-ip-address-dns-servers-and-default-gateway\/\">One-liner PowerShell to set IP Address, DNS Servers, and Default Gateway<\/a>:<\/p>\n<pre><code>PS C:\\Users\\Administrator&gt; Set-NetIPAddress -InterfaceAlias Ethernet0 -AddressFamily IPv4 -IPAddress 192.168.100.2 -PrefixLength 24 -DefaultGateway 192.168.100.1\r\nSet-NetIPAddress : A parameter cannot be found that matches parameter name 'DefaultGateway'.\r\nAt line:1 char:104<\/code><\/pre>\n<p>Counter-intuitive, but you can&#8217;t use <code>Set-NetIPAddress<\/code> for setting an IP address. As the <a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/nettcpip\/set-netipaddress\">Set-NetIPAddress command documentation<\/a> states: <\/p>\n<blockquote><p>The Set-NetIPAddress cmdlet modifies IP address configuration properties of an existing IP address.<\/p>\n<p>To create an IPv4 address or IPv6 address, use the New-NetIPAddress cmdlet.<\/p><\/blockquote>\n<p>Unfortunately, that led me to believe that <code>DefaultGateway<\/code> was a bad parameter in general. That&#8217;s not the case, and it works just fine with the <code>New-NetIPAddress<\/code> cmdlet as demonstrated in the TechNet article (and below):<\/p>\n<pre><code>PS C:\\Users\\Administrator&gt; New-NetIPAddress -InterfaceAlias Ethernet0 -AddressFamily IPv4 -IPAddress 192.168.100.2 -PrefixLength 24 -DefaultGateway 192.168.100.1<\/code><\/pre>\n<p>The <a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/nettcpip\/new-netipaddress?view=winserver2012r2-ps\">Microsoft documentation for the New-NetIPAddress cmdlet<\/a> further describes the <code>-DefaultGateway<\/code> option and other options.<\/p>\n<p>Of course, I hadn&#8217;t figured that out at the time, and so I had to find a different way to set the default gateway. <a href=\"https:\/\/www.virtualizationhowto.com\/2016\/09\/change-default-gateway-powershell\/\">Change Default Gateway with Powershell<\/a> was a helpful article, although overly convoluted for my needs. I didn&#8217;t need a function to wrap around a couple cmdlets. Here&#8217;s what I ended up doing:<\/p>\n<pre><code>Get-NetIPAddress -InterfaceIndex 12\r\nGet-NetRoute -InterfaceIndex 12\r\nRemove-NetIPAddress -InterfaceIndex 12\r\nNew-NetIPAddress -InterfaceIndex 12 -IPAddress 192.168.100.2 -PrefixLength 24\r\nRemote-NetRoute -InterfaceIndex 12\r\nNew-NetRoute -InterfaceIndex 12 -NextHop 192.168.100.1 -DestinationPrefix 0.0.0.0\/0\r\nping 192.168.100.1\r\nping 216.154.220.53\r\nGet-DnsClientServerAddress -Interface 12\r\nSet-DnsClientServerAddress -Interface 12 -ServerAddresses @(\"8.8.8.8\",\"8.8.4.4\")\r\nping osric.com<\/code><\/pre>\n<p>All of the pings succeeded. It worked!<\/p>\n<p>As I learned later, I could have accomplished the same with just the following 2 commands:<\/p>\n<pre><code>New-NetIPAddress -InterfaceAlias Ethernet0 -IPAddress 192.168.100.2 -AddressFamily IPv4 -PrefixLength 24 -DefaultGateway 192.168.100.1\r\nSet-DnsClientServerAddress -Interface 12 -ServerAddresses @(\"8.8.8.8\",\"8.8.4.4\")<\/code><\/pre>\n<p>You might wonder why I used <code>InterfaceIndex 12<\/code> in the prior example. I used Powershell&#8217;s tab completion and for some reason it used <code>-InterfaceIndex<\/code> instead of <code>-InterfaceAlias<\/code>. Honestly, I&#8217;m not sure why and I&#8217;m not sure if <em>InterfaceIndex 12<\/em> always corresponds to <em>InterfaceAlias Ethernet0<\/em> or not. <code>InterfaceAlias Ethernet0<\/code> is certainly more human-readable and is what I used when updating the IP address on subsequent cloned VMs:<\/p>\n<ul>\n<li>Add new IP address on same interface<\/li>\n<li>Remove old IP address on same interface<\/li>\n<li>(Reconnect to RDP [Remote Desktop Protocol] on the new address)<\/li>\n<li>Default gateway\/routes unchanged, since both IP addresses were in the same subnet<\/li>\n<\/ul>\n<p>Speaking of RDP, one additional task remained: Could I remote into the host? No, at least, not yet. The error message on Microsoft Remote Desktop (on Mac OSX):<\/p>\n<pre><code>Unable to connect to remote PC. Please provide the fully-qualified name or the IP address of the remote PC, and then try again.<\/code><\/pre>\n<p>Another online tutorial to the rescue: <a href=\"https:\/\/www.techtutsonline.com\/enable-remote-desktop-on-windows-server-2012-r2-via-powershell\/\">Enable Remote Desktop on Windows Server 2012 R2 via PowerShell<\/a>. I was glad to find it, since the registry key step below was very foreign to my Linux mindset:<\/p>\n<pre><code>Set-ItemProperty -Path 'HKLM:\\System\\CurrentControlSet\\Control\\Terminal Server' -Name 'fDenyTSConnections' -Value 0\r\nEnable-NetFirewallRule -DisplayGroup 'Remote Desktop'<\/code><\/pre>\n<p>One last thing I had to do in my Microsoft Remote Desktop client: specify the port:<\/p>\n<pre><code>192.168.100.2:3389<\/code><\/pre>\n<p>(Even though 3389 is the default RDP port, for some reason it needed that.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of my first steps after installing Linux is to set up networking (static IP address, default gateway, and nameservers). I recently needed to install a number of Windows servers and needed to find a way to do the same using Powershell. This post describes the steps (and missteps) I took to accomplish this.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[393],"tags":[228],"class_list":["post-2787","post","type-post","status-publish","format-standard","hentry","category-powershell","tag-powershell"],"_links":{"self":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/2787","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/comments?post=2787"}],"version-history":[{"count":13,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/2787\/revisions"}],"predecessor-version":[{"id":2800,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/2787\/revisions\/2800"}],"wp:attachment":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/media?parent=2787"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/categories?post=2787"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/tags?post=2787"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}