Powershell script to install HMS on Server 2016 core

This section contains user-submitted tutorials.
Post Reply
Freescape
New user
New user
Posts: 4
Joined: 2017-06-25 22:30

Powershell script to install HMS on Server 2016 core

Post by Freescape » 2017-08-08 02:01

Also optionally install mysql. Thought others may find this useful for a low memory footprint server build.

Will open all ports for the hmailserver.exe process to allow RPC access from the HMailServer admin console running on another machine.

You'll need to set the URLs for the external downloads mysqlliburl, vcredisturl, mysqlredisturl and hmsurl

***

$InstallMySql = $false
$CreateNewHMSDb = $false # if false, connect to existing db

$dbhost = "server2016" # localhost if you are installing mysql
$dbname = "mail"
$dbport = "3306"
$dbuser = "dbuser"
$dbpass = "dbuserpass"
$hmailadminpw = "mailadminpass"
$datafolder = "c:\data"
$maildomain = "maildomain.com"

$mysqlliburl = "http://server2016/libmysql.dll" # get from mysql x86 download package bin dir
$vcredisturl = "http://server2016/vcredist_x64.exe" # https://download.microsoft.com/download ... st_x64.exe

$mysqlredisturl= "http://server2016/mysql-5.7.19-winx64.zip" #https://dev.mysql.com/get/Downloads/MyS ... winx64.zip
# MySQL zip file format will have everything in one subdirectory internally

$HMSUrl = "http://server2016/hMailServer-5.6.6-B2383.exe" # https://www.hmailserver.com/download_fi ... loadid=256
$mysqldir = "c:\mysql"

# assuming you already have remote desktop access this is commented out
# $params = "$($env:windir)\system32\SCRegEdit.wsf /ar 0"
# & "cscript" ($params -split " ")

function InstallMySQL($mysqlredisturl, $vcredisturl, $mysqldir, $dbuser, $dbpass)
{
write-host "Install VC runtime 2013 x64..."
Invoke-WebRequest $vcredisturl -OutFile ".\vcredist_x64.exe"
.\vcredist_x64.exe /silent

write-host "Install MySQL..."
Invoke-WebRequest $mysqlredisturl -OutFile ".\mysql.zip"

$tempdir = (join-path "." ([System.Guid]::NewGuid().ToString()))
mkdir $tempdir | Out-Null

write-host "Extracting to $tempdir..."
Expand-Archive -Path ".\mysql.zip" -DestinationPath $tempdir

$subdir = (dir $tempdir -Directory | select -First 1).FullName
write-host "Moving $subdir to $mysqldir..."
move-item $subdir $mysqldir
remove-item ".\mysql.zip"

$env:path += “;$(join-path $mysqldir "bin”)"
setx PATH $env:path /M | Out-Null

write-host "Initialize MySQL data dir..."
mysqld --initialize-insecure

write-host "Install MySQL service..."
mysqld --install

write-host "Start MySQL service..."
Start-Service MySQL

write-host "Create MySQL HMS user..."
$sql = "`"CREATE USER '$dbuser'@'%' IDENTIFIED BY '$dbpass';"
$sql += "GRANT ALL PRIVILEGES ON *.* TO '$dbuser'@'%' WITH GRANT OPTION;"
$sql += "FLUSH PRIVILEGES;`""
mysql -u root --password="" -Bse $sql

write-host "Set MySQL root@localhost password..."
mysql -u root --password="" -Bse "`"ALTER USER 'root'@'localhost' IDENTIFIED BY '$dbpass';`""

write-host "Create MySQL Firewall rule..."
New-NetFirewallRule -DisplayName 'MySQL' -Profile Any -Direction Inbound -Action Allow -Protocol TCP -LocalPort @('3306') | out-null
}

function InstallHMS($HMSUrl, $hmailadminpw)
{
write-host "Downloading HMS..."
Invoke-WebRequest $HMSUrl -OutFile ".\hms.exe"

write-host "Installing HMS..."
& ".\hms.exe" "/verysilent"

Start-Sleep -Seconds 2

$hmsService = $null
$tries = 0
while ($tries++ -lt 10 -and ($hmsService -eq $null))
{
write-host "Waiting for hMailServer service to exist..."
Start-sleep -Seconds 2
($hmsService = get-service -Name hmailserver -ErrorAction SilentlyContinue) | Out-Null
}

$tries = 0
while ($tries++ -lt 10 -and ($hmsService -eq $null -or $hmsService.Status -ne "Running"))
{
write-host "Waiting for hMailServer service to start..."
if ($hmsService -ne $null -and $hmsService.Status -eq "Stopped") { $hmsService | Start-Service }
Start-sleep -Seconds 2
($hmsService = get-service -Name hmailserver -ErrorAction SilentlyContinue) | Out-Null
}

$hm = $null
$tries = 0
while (($hm -eq $null) -and ($tries++ -lt 5))
{
write-host "Trying to create hMailServer COM interface..."
$hm = New-Object -ComObject hMailServer.Application
Start-sleep -seconds 2
}

$hm.Authenticate("administrator", "") | Out-Null

write-host "Set HMS admin password..."
$hm.Settings.SetAdministratorPassword($hmailadminpw)

$inifile = $hm.InitializationFile
$installpath = Split-Path $inifile -Parent

write-host "Downloading mysql x86 lib"
Invoke-WebRequest $mysqlliburl -OutFile (Join-Path $installpath "libmysql.dll")

Restart-Service -Name "hmailserver"

write-host "Open all firewall ports for HMS service process only (RPC req)"
New-NetFirewallRule -Profile Any -Name hMailServerRule -Protocol TCP -LocalPort "0-65535" -Program (Join-Path $installpath "hMailServer.exe") -Enabled True -Action Allow -DisplayName "All ports for hMailServer" | out-null
New-NetFirewallRule -Profile Any -Name DCOMIncoming -Protocol TCP -LocalPort 135 -Enabled True -Action Allow -DisplayName "Incoming DCOM" | out-null
}

function CreateHMSDatabase($hm, $dbhost, $dbport, $dbname, $dbuser, $dbpass)
{
write-host "Creating HMS database..."

$db = $hm.Database
$db.CreateExternalDatabase(1, $dbhost, $dbport, $dbname, $dbuser, $dbpass)
}

function SetHMSDatabase($hm, $dbhost, $dbport, $dbname, $dbuser, $dbpass)
{
write-host "Setting HMS database..."

$db = $hm.Database
$db.SetDefaultDatabase(1, $dbhost, $dbport, $dbname, $dbuser, $dbpass)
}

function ConfigureHMS($hm, $datafolder, $domain)
{
if (!(test-path $datafolder)) { mkdir $datafolder | out-null }

write-host "Set HMS data directory $datafolder ..."
$hm.Settings.Directories.DataDirectory = $datafolder

write-host "Setting hostname $domain ..."
$hm.Settings.HostName = $domain

# $hm.Reinitialize()
# Restart-Service -Name "hmailserver"
}

function InstallNetfx()
{
write-host "Install .Net Framework 3.5 (2.0)"
Enable-WindowsOptionalFeature -FeatureName netfx3 -All -Online
}

InstallNetfx

InstallHMS $HMSUrl $hmailadminpw

$hm = New-Object -ComObject hMailServer.Application
$hm.Authenticate("administrator", $hmailadminpw) | Out-Null

if ($InstallMySql) {
InstallMySQL $mysqlredisturl $vcredisturl $mysqldir $dbuser $dbpass }

if ($CreateNewHMSDb) {
CreateHMSDatabase $hm $dbhost $dbport $dbname $dbuser $dbpass }
else {
SetHMSDatabase $hm $dbhost $dbport $dbname $dbuser $dbpass }

ConfigureHMS $hm $datafolder $maildomain

Restart-Service -Name hmailserver

User avatar
mattg
Moderator
Moderator
Posts: 19994
Joined: 2007-06-14 05:12
Location: 'The Outback' Australia

Re: Powershell script to install HMS on Server 2016 core

Post by mattg » 2017-08-08 02:05

Great write-up. thanks for sharing.

I'd also expect this to work on the 'no cost to download' HyperV Core Server. I'd previously installed hMailserver on HyperV Core Server 2012R2...and it worked fine.
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

Freescape
New user
New user
Posts: 4
Joined: 2017-06-25 22:30

Re: Powershell script to install HMS on Server 2016 core

Post by Freescape » 2017-08-08 02:12

Yeah, seems to work fine. Saves a chunk of disk and memory.

Now getting it up on Nano is more of an issue... (how's that x64 build coming along?) :-)

User avatar
mattg
Moderator
Moderator
Posts: 19994
Joined: 2007-06-14 05:12
Location: 'The Outback' Australia

Re: Powershell script to install HMS on Server 2016 core

Post by mattg » 2017-08-08 02:58

https://build.hmailserver.com/ (I see that the SSL is out of ate by 2 days...)

5.7 is the version with 64 bit installer
Check the artifacts pages for installers.

These should be considered ALPHA builds, but I'd expect that Martin uses them himself on a production machine
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

User avatar
Dravion
Senior user
Senior user
Posts: 1411
Joined: 2015-09-26 11:50
Location: Germany
Contact:

Re: Powershell script to install HMS on Server 2016 core

Post by Dravion » 2017-08-08 05:19

Great work.

If you need a uptodate hMailServer x64 Version you can download my personal build (its from the 5.6.7 DevBranch)
https://github.com/Dravion/hmailserver/releases

To your questions about Nano-Server.I have written a manual step by step guide howto install hMailServer on Windows Server Core (take a look at it
viewtopic.php?f=21&t=28708) but Nano-Server is a complete diffrent beast.

I also messed arround with Nano-Server but it wasnt verry fruitfull...

However, here are my findings:
I started with a DVD Copy of Windows Server 2016. On the Installation DVD there exists a Powershell-Script to create a new NanoVM for MS HyperV.At this step you need to provide a lot of things like IP-Address, OEM-Driver packages, Windows filesharing components stuff, ect. Ok, using Qemu i was able to convert the MS only VHD Format in Virtual Box Format and i was able to start the Nano-Server VM in Virtualbox. You get a login dialog but after login you have no shell, just a char gui for basicly enabling
Powershell remoting (there is no local, interactive login into NanoServer VM possible, all steps has to be done from a remote Powershell console). The next Problem is, you cant run any 32-Bit Programs and no GUI-Program at all or at least you cannot click somewhere because there is no Graphical Subsystem avaiable on Nanoserver as for instance on Server Core.

I was able to configure Fileshares, IIS with PHP5 and even MySQL5, accepting connections from local PHPMysqlAdmin
running on top of NanoServers IIS+PHP5 Installation, but there was no way to get hMailServer completeley running.
I was able to Register hMailServer as Service and register the Typelib but due the lack of DCOM and .NET in NanoServer, you cannot use hMailAdmin.exe, DBQuick.exe, DBSetup.exe, DataSyncronizer or even the normal
Setup Program. Ok, i preconfigured a MySQL5 DB with a hMailserver DB-Scheme, exported it as dump, reimported it to MySQL5 running on NanoServer side by side with hMailServer, replaced the NanoServers hMailServer.ini file with the peconfigured hMailServer Inifile and correct Database Settings, but even after restart i was not able to figure out howto to make hMailServer work. I was stuck and had other things to do, but maybe you find a way.

Post Reply