Batch – Chrome and Self-built URLs

Batch – Chrome and Self-built URLs

A previous job of mine had a unique issue. Unstandardized google chromes and self-generated icons. Both of these issues caused a lot of heartaches. Today I will show you how I solved the issue of unstandardized google chromes with batch. At the time the company’s default browser was IE. The site that needed chrome was an in-house product. I was pushing towards standardization of Google chrome 64-bit enterprise. However, management did fully agree with my thinking. So a compromise was created. I created a shortcut link on the desktop that pointed to a batch file. This batch file determined which version of Chrome was installed and loaded that chrome to the special internal page. Here is the script for this icon:

The Script

if exist "C:\Program Files\Google\Chrome\Application\chrome.exe" (
	start "CMD" /D "C:\Windows\System32\" /max "C:\Program Files\Google\Chrome\Application\chrome.exe" "https://www.bolding.us"
) else (
	if exist "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" (
		start "CMD" /D "C:\Windows\System32\" /max "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "https://www.bolding.us"
	) else (
		msg "%username%" Chrome Not Installed
	)
)

It’s a simple batch script. We first ask if the 64-bit path exists. If it does, then we open chrome to a hyperlink. If it doesn’t, we ask if the x86 version exists. If it does, we open chrome to the hyperlink. If it doesn’t, we tell the user it doesn’t exist. Pretty simple? Yeah, it’s very simple. So, the desktop shortcut would point to the batch file. The batch file would execute and load up the chrome page.

Taking it another step

This process helped a lot when it came to the next issue. The next issue was each location had a custom-hosted site on that location’s Server. There were tablets that had to float between each location. This means, that the icon on the tablet must open the custom website link at center 1 and at center 101. Thankfully each center had it’s own subnet. Each server that hosted this site hosted it at 10.x.x.5. This means all I had to do is find out the Subnet and make a .5 address in the link. Believe it or not, that’s not as hard as you would think. We are taking the script from above and pretty much piping the new URL into it.

The Script

@echo off
rem All i need to do now is grab only the IP address with batch. 
rem ipconfig | find "IPv4"
for /f "usebackq tokens=2 delims=:" %%f in (`ipconfig ^| findstr /c:"IPv4 Address"`) do set "ip=%%f"

for /f "tokens=1-4 delims=. " %%a in ("%ip%") do (
set octetA=%%a
set octetB=%%b
set octetC=%%c
set octetD=%%d
)

if exist "C:\Program Files\Google\Chrome\Application\chrome.exe" (
	start "CMD" /D "C:\Windows\System32\" /max "C:\Program Files\Google\Chrome\Application\chrome.exe" "http://%octetA%.%octetB%.%octetC%.5/Login.aspx"
) else (
	if exist "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" (
		start "CMD" /D "C:\Windows\System32\" /max "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "http://%octetA%.%octetB%.%octetC%.5/Login.aspx"
	) else (
		msg "%username%" Chrome Not Installed
	)
)
exit

Inside the script you see that we are breaking down the IPv4 address. So, if you are running IPv6, this isn’t going to work. We place each Octect into it’s on veraible. Then we build the site at the link level. Bamn, done with the script.

The next part is to create the icon. Which i’ll leave that to you. I hope this little brain twister is helpful for what it is worth.

Local Website

Local Website

Here we are again, trying to make a device work from one center to another. Each center has the same server IP address but different subnets. All the subnets are /24. So, that makes life easier. All of them are .5 as well. So, that makes life much easier. So, how do we get a website started on .5 from every device but able to take it to another site? Well, the solution is simple. You find the IP address and tell the internet browser to start it for you.

First lets get the IP address and sort through it.

for /f "usebackq tokens=2 delims=:" %%f in (`ipconfig ^| findstr /c:"IPv4 Address"`) do set "ip=%%f"

The above is a loop for IP config to remove anything that is ipv6 to IPv four. Once we have our IPv4 Address we then loop through that and set each octet that we want to use.

for /f "tokens=1-4 delims=. " %%a in ("%ip%") do (
set octetA=%%a
set octetB=%%b
set octetC=%%c
set octetD=%%d
)

We sort through the IPv4 address and split each item via the . delimiter. Now we take that info and start our IE.

start "CMD" /D "C:\Windows\System32\" /max "C:\Program Files\Internet Explorer\iexplore.exe" "http://%octetA%.%octetB%.%octetC%.5/LocalWebsite/"

That’s it, a very simple process, but it can help with so much time.

The Script

@echo off
rem All i need to do now is grab only the IP address with batch. 
rem ipconfig | find "IPv4"
for /f "usebackq tokens=2 delims=:" %%f in (`ipconfig ^| findstr /c:"IPv4 Address"`) do set "ip=%%f"

for /f "tokens=1-4 delims=. " %%a in ("%ip%") do (
set octetA=%%a
set octetB=%%b
set octetC=%%c
set octetD=%%d
)

start "CMD" /D "C:\Windows\System32\" /max "C:\Program Files\Internet Explorer\iexplore.exe" "http://%octetA%.%octetB%.%octetC%.5/LocalWebsite/"
exit

Local Site Found by a Universal device

Local Site Found by a Universal device

We have multiple locations with backup hosted sites. These sites are designed to allow the end-user to access the information they need when the network is down. They are set up with a local database and some basic asp.net coding. However, the devices that communicate with the site must be able to go between multiple locations. This means no hard-coded links as the hosting device has a different name at each location. DNS might not be available as well. The chaos this brings! There is a solution though. Here are the requirements for the solution we created.

  1. The hosting server can be a different name, but the IP address must end in the same number. In our example, it is .5
  2. The IP address must be a /24 subnet.
  3. The /site.html must be the same across the board.
    1. For example, the site can be http://10.10.1.5/thissite.html at one location and be http://10.10.13.5/thissite.html at another.

lets look at the powershell script really quickly and break it down.

The Script

$IPaddress = (Get-NetIPConfiguration -Detailed | Where-Object {$null -ne $_.IPv4DefaultGateway})[0].ipv4address.ipaddress
Start-Process -FilePath 'C:\Program Files\Internet Explorer\iexplore.exe' -ArgumentList "https://$($IPaddress.split('.')[0]).$($IPaddress.split('.')[1]).$($IPaddress.split('.')[2]).5/login.aspx"

What we are doing with this script is grabbing a detailed report on all the network IP configurations on the local computer. By default, an active network will have a default gateway assigned to it. This can be statically assigned or it can be automatically assigned. Either way, it goes, it’s assigned.

To narrow down the results we use a where-object command to compare null to the IPv4DefaultGateway. If it is null, we don’t want it. If it isn’t, then we want it. After that, we ask for the first result using the [0]. Most companies use the first network as the main network and the second network as a backup. That’s what we do at our company. Also most of the time the second network is a wifi network.

Next, we Ask for the IPv4Address and ask for just the IP address with .IPv4Address.IpAddress. Once we have all that information, we place it into a variable.

Now it’s time to start IE. You can set the browser to whatever program you like, but for now, we are still using IE for grandpa’s coded sites. We do this using the Start-Process command. We tell start-process the file path. This is where you can set google chrome, a 64 bit, edge, etc… It doesn’t matter what the browser is, as long as you point to one.

The Argument List is an important factor here. We give the site information here. We start off with our “https:// Then we do magic. We $($IP.address.split(‘.’)[0]. The act of using two $ is very important inside a double-quoted string. The reason why is we are stating everything inside $() is it’s own thing. We can run different commands inside $() and the output will output at that point in the string. Thus we are breaking the IP address string down by the . and finding each array point. Then after we create that point, we finish it off with a . and start our next $(). We do this until we reach our last octet. Which will be whatever the default IP address is. Finally finishing off with the site name. In this example, login.aspx. Below is the start process part of the command. Take a close look at the argument list. Assume the IP address is 10.10.1.15. This would start a site looking at “https://10.10.1.5/login.aspx”.

Start-Process -FilePath 'C:\Program Files\Internet Explorer\iexplore.exe' -ArgumentList "https://$($IPaddress.split('.')[0]).$($IPaddress.split('.')[1]).$($IPaddress.split('.')[2]).5/login.aspx"

The final step is to set your lnk to open the PowerShell script with a -bypass flag. This way it will execute. Some companies do not allow PowerShell to execute. In that case, this script does not help. You can however use the match version of this script found below.

@echo off
for /f "usebackq tokens=2 delims=:" %%f in (`ipconfig ^| findstr /c:"IPv4 Address"`) do set "ip=%%f"

for /f "tokens=1-4 delims=. " %%a in ("%ip%") do (
set octetA=%%a
set octetB=%%b
set octetC=%%c
set octetD=%%d
)

start "CMD" /D "C:\Windows\System32\" /max "C:\Program Files\Internet Explorer\iexplore.exe" "https://%octetA%.%octetB%.%octetC%.5/login.aspx"
exit

Conclusion

The final idea is to have the link to point to an organic system that can read the current IP structure and point the link to the correct page. This will allow that single roaming device to find those local sites quickly assuming you have a pre-planned infrastructure in place.