Tuesday, May 23, 2017

PowerShell - Find VPN adapter and change DNS addresses

If we have to join VPN, the DNS will mostly be set during the joining time. If we want to change the DNS servers after connecting to VPN, we have to manually do it every time. It involves so many clicks to change. 
Below is PowerShell script which finds a Cisco VPN adapter and change its DNS address

$index = Get-NetAdapter | where InterfaceDescription -like 'Cisco*' | Select ifIndex

Set-DnsClientServerAddress -InterfaceIndex $index.ifIndex -ServerAddresses "1.1.1.1,1.1.1.2"

This script has to run in admin mode to get it applied. Better save the above 2 lines as .ps1 file and execute in admin mode.

The first line can be further optimized to check whether VPN connected or not, exact name of adapter etc...

The PowerShell module seems already installed in Win 10 machines. The API may differ for other versions.

Tuesday, May 16, 2017

Protractor - CI with AppVeyor issue on webdriver-manager update

Protractor is a nice tool to do testing of Angular applications. It really shines when used in continuous delivery scenarios because of its end to end testing capabilities. We can simply host the site and run our tests with Protractor. But when we use it with hosted CI solutions there is a chance for getting the below exception in the build process.

(node:1184) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Failed to make Github request, rate limit reached.

This happens when we issue the below command which we cannot avoid to get it working.

webdriver-manager update

One such failure can be seen here. What is really happening is that during the update, the webdriver-manager is trying to download something from GitHub for Gecko driver. Since most of the hosted CI solutions use same single IP to access outside internet, the GitHub API limits comes into play.

This is already known issue and the only way is to disable Gecko driver for now. Command to disable Gecko driver is as follows.

webdriver-manager update --gecko false

Thanks to the AppVeyor support team.

Tuesday, May 9, 2017

Functional JavaScript - Find hidden string in the input

This is continuation of my functional programming experiments in JavaScript. This problem is from HackerRank.

The problem

Input is a string and we have to find out whether the letters of word 'hackerrank' are present inside it in the right order. When we say order, there might be some other letters in between the letters of 'hackerrank' the the order should be maintained. More details can be found in the actual problem link

Traditional solution

If the guy is not from the computer science background, there is 99% chance that the solution will be based on loops where a lot of states might have stored temporarily. But it looks like a familiar situation of email validation where we are expecting chaaracters in specific order.

The RegExp way

Regular expressions are great. There is no doubt on that. Here goes the regular expression way.

function checkForhackerrank(input) {
  if(testForhackerrank(input)) {
    return 'YES';
  }
  else return 'NO';
}
function testForhackerrank(input){
  return /h(.)*a(.)*c(.)*k(.)*e(.)*r(.)*r(.)*a(.)*n(.)*k/g.test(input)
}

Works great for the word hackerrank. Lets generalize

Generalized version

Below goes the generalized version which can work for any pattern other than 'hackerrank'.

function testForhackerrank(input){
  return getFindPattern('hackerrank').test(input);
}
function getFindPattern(stringToSearch) {
  return getGlobalRegExp(stringToSearch.split('').map(a=> `(.)*${a}`).join(''));
}
function getGlobalRegExp(pattern){
  return new RegExp(pattern,'g');
}

This doesn't need anything special regarding functional paradigm as the magic is done by regular expression. Plnkr is here.

Enjoy coding.

Tuesday, May 2, 2017

Functional JavaScript - Finding unique characters in string


These are small code snippets created as part of learning functional JavaScript. As we know when we  say functional programming, we have to create pure functions, avoiding assignment etc...It really require practice to write the functional way especially, if we started in imperative languages. This is first in the series where some algorithms are written using functiona style.
  1. Functional JavaScript - Finding unique characters in string.

The problem

The problem is simple. Have to find out unique chars in string. eg: Hello has to return Helo. 

Functional ways

The easy way is to loop through the characters and push items into another array, if it is not already present. Finally create string by joining array.

Below is slightly different version where the characters are filtered, if the index found in original string is different than current index.

function uniq(str) {
    return str.split('').filter(function(item, index, self) {
      return self.indexOf(item) == index;
    }).join('');
}
Runnable plnkr below.