Catalin
Posts: 6506
Joined: Wed Jun 13, 2018 7:49 am

Local IIS Website - automatic IP assignment to Hostname

Thu Mar 11, 2021 8:53 pm

Hello guys,

Recently, one of our users has encountered some difficulties regarding the IP assignment to a hostname for an IIS website.

To be more precise, here is the question:
Would it be possible to assign the IP address to the IP address of the machine that it's installing from? Thanks.
StaticIP.png
StaticIP.png (42.77KiB)Viewed 47529 times

With that in mind, I have decided to create this How-To, hopefully it will be useful for other users facing a similar scenario as well.

Prerequisites:
  • Advanced Installer Enterprise project
  • IIS feature should be enabled on the target machine (in order to use the sample project I will provide at the end of the article)
IISFeature.png
IISFeature.png (37.42KiB)Viewed 47529 times

Before we begin, it is important to note that, in case of a locally installed website, the "hosts" file must also be update accordingly with the IP to hostname bind.

The "hosts" file is used for Name to IP Address resolution.
The hosts file is one of several system facilities that assists in addressing network nodes in a computer network. It is a common part of an operating system's Internet Protocol (IP) implementation, and serves the function of translating human-friendly hostnames into numeric protocol addresses, called IP addresses, that identify and locate a host in an IP network.

In some operating systems, the contents of the hosts file is used preferentially to other name resolution methods, such as the Domain Name System (DNS), but many systems implement name service switches, e.g., nsswitch.conf for Linux and Unix, to provide customization. Unlike remote DNS resolvers, the hosts file is under the direct control of the local computer's administrator
With that in mind, we can break our request into two smaller requests:
  • get the IP Address from the machine running the setup
  • update the "hosts" file accordingly
1) Get the IP Address from the machine running the setup:

To achieve this, we can use a custom action. The custom action can either be a PowerShell script, a VBScript, a C# .DLL, etc. In today's article, we will use a PowerShell custom action.

Even in PowerShell, there are multiple ways of retrieving the IP Address of a machine. For this task, we will make use of the "Test-Connection" cmdlet.

If we open a PowerShell prompt and run the following command:

Code: Select all

Test-Connection -ComputerName $env:ComputerName -Count 1
we will receive the following:
PS1.png
PS1.png (56.22KiB)Viewed 47529 times

As you can see, the above cmdlet returns some nice information, including the "IPV4Address", which we are interested in. To get more details about this object, we can proceed as it follows:

Code: Select all

(Test-Connection -ComputerName $env:ComputerName -Count 1).IPV4Adress
PS2.png
PS2.png (49.33KiB)Viewed 47529 times

The above object has a property that we need, namely "IPAddressToString". We will need to store this property into a variable.

Code: Select all

$ipv4Address = (Test-Connection -ComputerName $env:ComputerName -Count 1).IPV4Address.IPAddressToString
PS3.png
PS3.png (35.47KiB)Viewed 47529 times

1.1) Implementation of the script in Advanced Installer:

Basically, what we need to do here is:
  • go to "Custom Actions" page and add a "PowerShell inline script" custom action
  • schedule it as in the following screenshot:
PSCA1.png
PSCA1.png (115.1KiB)Viewed 47529 times

Here would be the content of the custom action:

Code: Select all

# Get the IPV4 address and stores it in a variable, then create a property with the value.
# We will further use this property in the IP field ("IIS" page --> your website --> "Bindings/SSL")

$ipv4Address = (Test-Connection -ComputerName $env:ComputerName -Count 1).IPV4Address.IPAddressToString

AI_SetMsiProperty IP_ADDRESS $ipv4Address
As you can see, we further use the value of our $ipv4Address variable to set a property.

This property will further be used in the "IP" field of our binding in "IIS" page --> your website --> "Bindings/SSL"
BindingsIP.png
BindingsIP.png (40.47KiB)Viewed 47529 times

This is pretty much it regarding the retrieval of the IPAddress from the machine where the setup is installed.


2) Get the path to the "hosts" file + Implementation in Advanced Installer

The default location of the "hosts" file, on the Windows OS, is:

SystemFolder\drivers\etc

which resolves on my machine to:

Code: Select all

C:\Windows\System32\drivers\etc
Here, we will need another custom action that will construct the path for us, since there is not any predefined variable / property that holds the above path.

To achieve this, we can make use of the "Environment class" and its' "SystemDirectory" property.

The PowerShell script will look as it follows:

Code: Select all

# Get the path to the "hosts" file which we will need to update with our IP + hostname bind
# This path will then be stored in a property, which we will use to create a Property Based folder.
# This folder will "hold" our "Text file update" operation

$system32 = [System.Environment]::SystemDirectory
$hostsDir = Join-Path -Path $system32 -ChildPath "\drivers\etc"

AI_SetMsiProperty HOSTSDIR $hostsDir
PSCA2.png
PSCA2.png (115.64KiB)Viewed 47529 times

Now that we have constructed the path, we can move on to the next step which would be the creation of a property based folder (HOSTSDIR). This folder will hold the "Text file update" operation that will update the hosts file, adding the IP and the hostname to it. Here is how we can proceed:
  • go to "Install Parameters" page and create an empty property as it follows:
HOSTSDIRPROP.png
HOSTSDIRPROP.png (13.46KiB)Viewed 47529 times
  • in "Files and Folders" page, right click on "Application Folder" --> "New folder" --> "Property" based --> select the HOSTSDIR property
select the earlier created folder and add a "Text file update" operation that will update the "hosts" file by "Appending/Creating" the following to the file:

Code: Select all

[IP_ADDRESS]	www.demowebsite.com

Below, you can find attached a sample project which implements every step presented above:
Sample.zip
(32.63KiB)Downloaded 1501 times

Hope this helps! :)

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

HienHo
Posts: 13
Joined: Thu Feb 25, 2021 6:46 am

Re: Local IIS Website - automatic IP assignment to Hostname

Sun Mar 14, 2021 11:17 pm

Excellent article Catalin! Very well written indeed.

One question: would the added entry in the hosts file automatically get deleted after an un-install? Thanks.

Regards,
Hien

Catalin
Posts: 6506
Joined: Wed Jun 13, 2018 7:49 am

Re: Local IIS Website - automatic IP assignment to Hostname

Thu Mar 18, 2021 7:59 pm

Hello Hien,

Thank you once again for your kind words!
would the added entry in the hosts file automatically get deleted after an un-install? Thanks.
Unfortunately, the added entry will not be removed upon uninstall.

If you want to remove the entry, we will need to have a cleanup custom action.

The custom action can look something as it follows:

Code: Select all

$ipv4Address = (Test-Connection -ComputerName $env:ComputerName -Count 1).IPV4Address.IPAddressToString

$system32 = [System.Environment]::SystemDirectory
$hostsDir = Join-Path -Path $system32 -ChildPath "\drivers\etc"
$hostsFile = Join-Path -Path $hostsdir -ChildPath "\hosts"

$stringToBeRemoved = "$ipv4Address" + "	www.demowebsite.com"

(Get-Content -Path $hostsFile) -Replace "$stringToBeRemoved","" | Set-Content $hostsFile -Force
I have added the custom action as it follows:

- after the "Add resources" action group

- "Execution Time" set to "When the system is being modified (deferred)"

- "Execution Options" --> "Run under the LocalSystem account with full privileges (no impersonation)"

- "Execution Stage Condition" --> only "Uninstall" checked

Thank you for bringing this to my attention and hope this helps! :)

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

HienHo
Posts: 13
Joined: Thu Feb 25, 2021 6:46 am

Re: Local IIS Website - automatic IP assignment to Hostname

Fri Mar 19, 2021 8:39 am

Hello Catalin,

Wow, you’re a legend! Thank you heaps for your great help Catalin.

I am sure this article would be of big help to many users out there. Advanced Installer rocks!

Regards,
Hien

Catalin
Posts: 6506
Joined: Wed Jun 13, 2018 7:49 am

Re: Local IIS Website - automatic IP assignment to Hostname

Fri Mar 19, 2021 3:01 pm

You are always welcome, Hien!

It is always my pleasure to help and thank you very much for your kind words! :)

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

HienHo
Posts: 13
Joined: Thu Feb 25, 2021 6:46 am

Re: Local IIS Website - automatic IP assignment to Hostname

Sat Mar 20, 2021 11:30 pm

Hi Catalin,

One more thing, you mentioned one of the prerequisites was Advanced Installer enterprise version. So this solution won’t work on other versions? Thanks.

Regards,
Hien

Catalin
Posts: 6506
Joined: Wed Jun 13, 2018 7:49 am

Re: Local IIS Website - automatic IP assignment to Hostname

Tue Mar 23, 2021 3:10 pm

Hello Hien,

This article requires some features that are available starting with our Enterprise suite, therefore that is why I've mentioned it as a prerequisite.

This should work just fine in the Enterprise and Architect suites.

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

HienHo
Posts: 13
Joined: Thu Feb 25, 2021 6:46 am

Re: Local IIS Website - automatic IP assignment to Hostname

Thu Mar 25, 2021 11:27 am

Thank you Catalin for your confirm.

So I gather the Professional version won’t work. Time to upgrade. 😊

Regards,
Hien

Catalin
Posts: 6506
Joined: Wed Jun 13, 2018 7:49 am

Re: Local IIS Website - automatic IP assignment to Hostname

Thu Mar 25, 2021 5:09 pm

You are always welcome, Hien! :D

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Sample Projects”