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.
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)
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.
With that in mind, we can break our request into two smaller requests: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
- get the IP Address from the machine running the setup
- update the "hosts" file accordingly
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
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
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
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:
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
This property will further be used in the "IP" field of our binding in "IIS" page --> your website --> "Bindings/SSL"
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
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
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:
- in "Files and Folders" page, right click on "Application Folder" --> "New folder" --> "Property" based --> select the HOSTSDIR property
Code: Select all
[IP_ADDRESS] www.demowebsite.com
Below, you can find attached a sample project which implements every step presented above:
Hope this helps!
Best regards,
Catalin