<?xml version="1.0" encoding="utf-8"?><rss version="2.0"
    xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:wfw="http://wellformedweb.org/CommentAPI/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:atom="http://www.w3.org/2005/Atom"
    xmlns:media="http://search.yahoo.com/mrss/"
    xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
    xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
>
<channel>
  <title>Srijan Choudhary, all posts tagged: windows</title>
  <link>https://srijan.ch/feed/all/tag:windows</link>
  <lastBuildDate>Wed, 05 May 2021 16:25:00 +0000</lastBuildDate>
  <image>
    <url>https://srijan.ch/assets/favicon/favicon-32x32.png</url>
    <title>Srijan Choudhary, all posts tagged: windows</title>
    <link>https://srijan.ch/feed/all/tag:windows</link>
  </image>
  <sy:updatePeriod>daily</sy:updatePeriod>
  <sy:updateFrequency>1</sy:updateFrequency>
  <generator>Kirby</generator>
  <atom:link href="https://srijan.ch/feed/all.xml/tag:windows" rel="self" type="application/rss+xml" />
  <description>Srijan Choudhary&#039;s Articles and Notes Feed for tag: windows</description>
  <item>
    <title>Automating custom routes and DNS setup on Windows</title>
    <description><![CDATA[How I automated setting up custom routes and DNS for FortiClient SSL VPN on Windows 10]]></description>
    <link>https://srijan.ch/automating-custom-routes-dns-windows</link>
    <guid isPermaLink="false">6092a2ba2a944a000154e7ba</guid>
    <category><![CDATA[windows]]></category>
    <dc:creator>Srijan Choudhary</dc:creator>
    <pubDate>Wed, 05 May 2021 16:25:00 +0000</pubDate>
    <media:content url="https://srijan.ch/media/pages/blog/automating-custom-routes-dns-windows/f4072ebf2b-1699621096/photo-1593642632823-8f785ba67e45.jpeg" medium="image" />
    <content:encoded><![CDATA[<figure data-ratio="auto">
    <img src="https://srijan.ch/media/pages/blog/automating-custom-routes-dns-windows/f4072ebf2b-1699621096/photo-1593642632823-8f785ba67e45.jpeg" alt="Automating custom routes and DNS setup on Windows">
  
  </figure>
<p>One of the problems I've faced working from home for the last
 one year is the rigidity of the VPN software used at my work. If we 
were using something like <a href="https://openvpn.net/" rel="noreferrer">OpenVPN</a>, then I could modify the client config to setup any overrides I wanted to the network routing table or DNS, but we use <a href="https://www.fortinet.com/resources/cyberglossary/ssl-vpn" rel="noreferrer">FortiClient SSL VPN</a>, which does not have such a functionality. Also, I've been using Windows 10 on my work setup for some time now because <a href="https://docs.microsoft.com/en-us/windows/wsl/" rel="noreferrer">WSL</a> works very well for me.</p> <p>But first, why did I even need to modify the routing table or DNS at all?</p><ol><li>I
 use a slightly non-standard network setup for my home, and one of my 
home subnets actually clashes with one of the routed work subnets (which
 I don't need). So, the easy solution for me is to change this routing 
table entry to what works for me.</li><li>I use <a href="https://diversion.ch/diversion/diversion.html" rel="noreferrer">diversion</a> on my home router to do central ad-blocking, and wanted to leverage that even when connected to the work VPN.</li></ol><hr />
<p>Here is the <a href="https://docs.microsoft.com/en-us/powershell/" rel="noreferrer">PowerShell</a> script that does the changes I want:</p><figure>
  <pre><code class="language-powershell">Start-Transcript -Append -Path &quot;C:\Users\srijan\Apps\network-post-connect.log&quot;

if( (Get-NetConnectionProfile -InterfaceAlias Wi-Fi).Name -eq &quot;Home Wifi&quot; ) {
    echo &quot;Home Wifi is connected&quot;
    $FortinetAdapter = Get-NetAdapter -InterfaceDescription &quot;Fortinet SSL*&quot;
    if($FortinetAdapter.Status -eq &quot;Up&quot;) {
        echo &quot;Work VPN is connected&quot;
        $FortinetAdapter | Set-DnsClientServerAddress -ServerAddresses (&quot;192.168.2.1&quot;, &quot;8.8.8.8&quot;)
        echo &quot;[OK] DNS server set to 192.168.2.1,8.8.8.8&quot;
        Get-NetRoute -DestinationPrefix 192.168.2.0/24 -RouteMetric 0 | Set-NetRoute -RouteMetric 500
        echo &quot;[OK] 192.168.2.0/24 routed locally&quot;
    }
    else {
        echo &quot;Work VPN is not connected. Doing nothing.&quot;
    }
}
else {
    &quot;Home Wifi is not connected. Doing nothing.&quot;
}

Stop-Transcript</code></pre>
    <figcaption class="text-center">network-post-connect.ps</figcaption>
  </figure>
<p>Explanation of what it does:</p><ol><li>Uses Start-Transcript and Stop-Transcript to log the output to a file.</li><li>Checks if the system is connected to SSID "Home Wifi".</li><li>If so, checks if the adapter with description with the pattern "Fortinet SSL*" is up</li><li>If so, changes the DNS server address</li><li>In the routing table, it increases the route metric of <code>192.168.2.0/24</code> with metric <code>0</code> - sets it to <code>500</code>. This route is added by FortiClient, which I wanted to de-prioritize.</li></ol><p>From the <a href="https://docs.microsoft.com/en-us/powershell/module/nettcpip/set-netroute?view=windowsserver2019-ps#parameters" rel="noreferrer">Set-NetRoute docs</a>:</p><blockquote>
  The computer selects the route with the lowest combined value.  </blockquote>
<hr />
<p>Now, we just need to setup some automation to run this whenever the VPN is connected. For this, I used <a href="https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-task-scheduler" rel="noreferrer">Windows Task Scheduler</a>. Whenever windows activates any network profile, a <code>Microsoft-Windows-NetworkProfile/Operational</code> log is generated with Event Id <code>10000</code>. Windows Task Scheduler has the capability to schedule a task to be run whenever any event is triggered.</p> <p>Here are some screenshots of the task configuration:</p><figure data-ratio="auto">
  <ul>
        <li>
      <img alt="" src="https://srijan.ch/media/pages/blog/automating-custom-routes-dns-windows/cd90aa1b3c-1699621096/task-general-1.png">    </li>
        <li>
      <img alt="" src="https://srijan.ch/media/pages/blog/automating-custom-routes-dns-windows/cd494ad4f5-1699621096/task-trigger.png">    </li>
        <li>
      <img alt="" src="https://srijan.ch/media/pages/blog/automating-custom-routes-dns-windows/ce2aa5df17-1699621096/task-action.png">    </li>
        <li>
      <img alt="" src="https://srijan.ch/media/pages/blog/automating-custom-routes-dns-windows/00e4e2a59e-1699621096/task-settings.png">    </li>
      </ul>
  </figure>
<p>Here's the exported XML of the task:</p><figure>
  <pre><code class="language-xml">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-16&quot;?&gt;
&lt;Task version=&quot;1.2&quot; xmlns=&quot;http://schemas.microsoft.com/windows/2004/02/mit/task&quot;&gt;
  &lt;RegistrationInfo&gt;
    &lt;Date&gt;2021-01-19T23:24:57.7398228&lt;/Date&gt;
    &lt;Author&gt;srijan&lt;/Author&gt;
    &lt;Description&gt;Currently:
1. Set local DNS
2. Route 192.168.2.0/24 locally&lt;/Description&gt;
    &lt;URI&gt;\Network post connect automation&lt;/URI&gt;
  &lt;/RegistrationInfo&gt;
  &lt;Triggers&gt;
    &lt;EventTrigger&gt;
      &lt;Enabled&gt;true&lt;/Enabled&gt;
      &lt;Subscription&gt;&amp;lt;QueryList&amp;gt;&amp;lt;Query Id=&quot;0&quot; Path=&quot;Microsoft-Windows-NetworkProfile/Operational&quot;&amp;gt;&amp;lt;Select Path=&quot;Microsoft-Windows-NetworkProfile/Operational&quot;&amp;gt;*[System[Provider[@Name=&#039;Microsoft-Windows-NetworkProfile&#039;] and EventID=10000]]&amp;lt;/Select&amp;gt;&amp;lt;/Query&amp;gt;&amp;lt;/QueryList&amp;gt;&lt;/Subscription&gt;
      &lt;Delay&gt;PT10S&lt;/Delay&gt;
    &lt;/EventTrigger&gt;
  &lt;/Triggers&gt;
  &lt;Settings&gt;
    &lt;MultipleInstancesPolicy&gt;IgnoreNew&lt;/MultipleInstancesPolicy&gt;
    &lt;DisallowStartIfOnBatteries&gt;false&lt;/DisallowStartIfOnBatteries&gt;
    &lt;StopIfGoingOnBatteries&gt;true&lt;/StopIfGoingOnBatteries&gt;
    &lt;AllowHardTerminate&gt;true&lt;/AllowHardTerminate&gt;
    &lt;StartWhenAvailable&gt;false&lt;/StartWhenAvailable&gt;
    &lt;RunOnlyIfNetworkAvailable&gt;false&lt;/RunOnlyIfNetworkAvailable&gt;
    &lt;IdleSettings&gt;
      &lt;StopOnIdleEnd&gt;true&lt;/StopOnIdleEnd&gt;
      &lt;RestartOnIdle&gt;false&lt;/RestartOnIdle&gt;
    &lt;/IdleSettings&gt;
    &lt;AllowStartOnDemand&gt;true&lt;/AllowStartOnDemand&gt;
    &lt;Enabled&gt;true&lt;/Enabled&gt;
    &lt;Hidden&gt;false&lt;/Hidden&gt;
    &lt;RunOnlyIfIdle&gt;false&lt;/RunOnlyIfIdle&gt;
    &lt;WakeToRun&gt;false&lt;/WakeToRun&gt;
    &lt;ExecutionTimeLimit&gt;PT1H&lt;/ExecutionTimeLimit&gt;
    &lt;Priority&gt;7&lt;/Priority&gt;
  &lt;/Settings&gt;
  &lt;Actions Context=&quot;Author&quot;&gt;
    &lt;Exec&gt;
      &lt;Command&gt;powershell&lt;/Command&gt;
      &lt;Arguments&gt;-File C:\Users\srijan\Apps\network-post-connect.ps1 -WindowStyle Hidden&lt;/Arguments&gt;
    &lt;/Exec&gt;
  &lt;/Actions&gt;
&lt;/Task&gt;</code></pre>
  </figure>
<hr />
<p>I
 have gotten used to the ease of setting up things like this for Linux, 
but was pleasantly surprised that it's easy enough for Windows as well. 
Windows Task Scheduler actually supports a lot of different conditionals
 for tasks as well. For example, only starting the task if the computer 
has been idle for some time, or only starting if connected to AC power, 
etc..</p> <p>Let me know in the comments if you think there is an easier way, or if you have any improvement suggestions for the above.</p>]]></content:encoded>
    <comments>https://srijan.ch/automating-custom-routes-dns-windows#comments</comments>
    <slash:comments>1</slash:comments>
  </item></channel>
</rss>
