How to configure custom "Rules" automatically using powershell

Use this forum if you have installed hMailServer and want to ask a question related to a production release of hMailServer. Before posting, please read the troubleshooting guide. A large part of all reported issues are already described in detail here.
Post Reply
naren
New user
New user
Posts: 25
Joined: 2019-04-30 09:01

How to configure custom "Rules" automatically using powershell

Post by naren » 2020-08-19 07:23

How to configure custom "Rules" automatically after installing hMail server using Powershell.

Below is the same Rule and it executes specific script function
hMailserverRule.PNG

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

Re: How to configure custom "Rules" automatically using powershell

Post by mattg » 2020-08-19 09:14

This is pretty easily done with the COM API
https://www.hmailserver.com/documentati ... om_objects

Don't know how hard that would be to convert to Powershell scripts..
Why do you wish to replicate this same rule repeatedly?
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

palinka
Senior user
Senior user
Posts: 2189
Joined: 2017-09-12 17:57

Re: How to configure custom "Rules" automatically using powershell

Post by palinka » 2020-08-19 18:52

I worked this out not long ago. This should get you started.

Code: Select all

$hMSAdminPass = "supersecretpassword"
$hMS = New-Object -COMObject hMailServer.Application
$hMS.Authenticate("Administrator", $hMSAdminPass) | Out-Null

<### Create Global Rule ###>

<## Predefined Fields ##
0 - Unknown
1 - From
2 - To
3 - CC
4 - Subject
5 - Body
6 - MessageSize
7 - RecipientList
8 - DeliveryAttempts

## Search Type ##
0 - Unknown
1 - Equals
2 - Contains
3 - LessThan
4 - GreaterThan
5 - RegExMatch
6 - NotContains
7 - NotEquals
8 - Wildcard

## Rule Actions ##
0 - Unknown
1 - DeleteEmail
2 - ForwardEmail
3 - Reply
4 - MoveToImapFolder
5 - RunScriptFunction
6 - StopRuleProcessing
7 - SetHeaderValue
8 - SendUsingRoute
9 - CreateCopy
10 - BindToAddress
#>

<#  Add global rule  #>
$hMSAddRule = $hMS.Rules.Add()

$hMSAddRule.Name = "Test Rule"
$hMSAddRule.UseAND = $True
$hMSAddRule.Active = $True
$hMSAddRule.Save()
$hMSAddRule.MoveUp()

$hMSAddRuleCriteria = $hMSAddRule.Criterias.Add()
$hMSAddRuleCriteria.RuleID = $hMSAddRule.ID
$hMSAddRuleCriteria.UsePredefined = $True
$hMSAddRuleCriteria.PredefinedField = 7              #recipientList
$hMSAddRuleCriteria.MatchType = 6                    #not Contains
$hMSAddRuleCriteria.MatchValue = "spam@domain.com"
$hMSAddRuleCriteria.Save()

$hMSAddRuleCriteria2 = $hMSAddRule.Criterias.Add()
$hMSAddRuleCriteria2.RuleID = $hMSAddRule.ID
$hMSAddRuleCriteria2.UsePredefined = $True
$hMSAddRuleCriteria2.PredefinedField = 2             #To
$hMSAddRuleCriteria2.MatchType = 2                   #Contains
$hMSAddRuleCriteria2.MatchValue = "@domain.com"
$hMSAddRuleCriteria2.Save()

$hMSAddRuleCriteria3 = $hMSAddRule.Criterias.Add()
$hMSAddRuleCriteria3.RuleID = $hMSAddRule.ID
$hMSAddRuleCriteria3.UsePredefined = $False
$hMSAddRuleCriteria3.HeaderField = "X-hMailServer-Spam"
$hMSAddRuleCriteria3.MatchType = 1                   #Equals
$hMSAddRuleCriteria3.MatchValue = "YES"
$hMSAddRuleCriteria3.Save()        

$hMSAddRuleAction = $hMSAddRule.Actions.Add()
$hMSAddRuleAction.TO = "spam@test.com"
$hMSAddRuleAction.Type = 2
$hMSAddRuleAction.RuleID = $hMSAddRule.ID
$hMSAddRuleAction.Save()

$hMSAddRuleAction2 = $hMSAddRule.Actions.Add()
$hMSAddRuleAction2.Type = 1
$hMSAddRuleAction2.RuleID = $hMSAddRule.ID
$hMSAddRuleAction2.Save()

$hMSAddRule.Save()

naren
New user
New user
Posts: 25
Joined: 2019-04-30 09:01

Re: How to configure custom "Rules" automatically using powershell

Post by naren » 2020-08-20 08:26

Thanks for the response. I will try and let you know.

Post Reply