PowerShell: Detect and Install SitecoreDockerTools automatically for adding Windows host file entry

Summary: This blog article is about SitecoreDockerTools PowerShell module: one of the pre-requisites required for Sitecore MVP site. If this module is not installed, you will encounter the error listed below.

The scenario explained in this blog article is encountered on a brand-new Windows machine (although I used Windows 11 in this case) as part of installing the Sitecore MVP site. On executing Start-Environment.ps1 without performing any of the initialisation, the Add-HostsEntry command is not recognised. Luckily, I had another machine where Add-HostsEntry was recognised. So, I compared with the same and adjusted the Start-Environment.ps1 code accordingly.

The code-fix here tracks if the necessary pre-requisites: PowerShell repository and module are present. Based on the same, installs the necessary repo and module, if not present. So, this fix and my earlier blog, although is done for Sitecore MVP site, can be generally useful for all website instances running on Sitecore Docker!

Note that although Remove-HostsEntry is part of PowerShell modules like Carbon, Add-HostsEntry is not part of any of those modules. 

This is where Show-Command comes to rescue:


With Show-Command you can find list of all PowerShell commands in a machine (with filter option) and also the associated module on hovering over the command



Based on the same, I executed Show-Command on the machine where the Add-HostsEntry command was recognised and I could find the Remove-HostsEntry and Add-HostsEntry are part of SitecoreDockerTools module. 

Get-Module -Name SitecoreDockerTools proves the same:


Get-PSRepository result in the machine that did not have SitecoreDockerTools installed:


Expected result:





I found that Install-DockerTools function is already part of C:\mvp-site\docker\tools\Init-Env.psm1 and this can do the needed job:


So, I add the above function call to Start-Environment.ps1:



After the execution of the above code, Add-HostsEntry is recognised:



Errors:



Add-HostsEntry : The term 'Add-HostsEntry' is not recognized as the name of a cmdlet, function, script file, or

operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try    again.                                                                                                                  At C:\mvp-site\docker\tools\Scripts\Functions-HostNames.ps1:14 char:5                                                   +     Add-HostsEntry "cm.$($HostDomain)"                                                                                +     ~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (Add-HostsEntry:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException


Add-HostsEntry : The term 'Add-HostsEntry' is not recognized as the name of a cmdlet, function, script file, or

operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try

again.

At C:\mvp-site\docker\tools\Scripts\Functions-HostNames.ps1:15 char:5

+     Add-HostsEntry "cd.$($HostDomain)"

+     ~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (Add-HostsEntry:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException


Add-HostsEntry : The term 'Add-HostsEntry' is not recognized as the name of a cmdlet, function, script file, or

operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try

again.

At C:\mvp-site\docker\tools\Scripts\Functions-HostNames.ps1:16 char:5

+     Add-HostsEntry "id.$($HostDomain)"

+     ~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (Add-HostsEntry:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

Comments