Configuring a Smtp virtual server on a Windows server using Powershell- (Relay, Connection)

Using powershell, I have to enable SMTP and

  • Add Ip Relay
  • Add IP Addresses to Connect
  • Set Access Control Authentication

I found the following code for the above operation

$GetSMTP = Get-CimInstance -Namespace "root\MicrosoftIISv2" -Class "IISSMTPServerSetting" -Filter "Name ='SmtpSvc/1'" $RelayIps = @(10,92,32,83,127,0,0,1) $GetSMTP.RelayIpList = $RelayIps Set-CimInstance -InputObject $GetSMTP $GetSMTP $GetSMTP = Get-CimInstance -Namespace "root\MicrosoftIISv2" -Class "IIsIPSecuritySetting" -Filter "Name ='SmtpSvc/1'" $NewConnectionIps = @( "10.92.32.80, 10.92.32.81"; "10.92.32.83,127.0.0.1"; ) $GetSMTP.ipgrant += $NewConnectionIps Set-CimInstance -InputObject $GetSMTP $GetSMTP 

The above powershell code executed successfully and it is displayed when it is added.

But when I connect to the smtp server, the following error throws

enter image description here

I have a solution to solve the above problem. To delete folders inside "C: \ inetpub \ mailroot" and where I can run the default Smtp Virtual Server, but again run into a problem, and then click smtp vitrual Server Properties

enter image description here

+6
source share
2 answers

Download Feature Installation Modules

 Import-Module ServerManager 

Function setting

 Add-WindowsFeature SMTP-Server,Web-Mgmt-Console,WEB-WMI 

Adding Relays, IP Connections, and Basic Authentication for SMTP

 $Networkip =@ () $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName localhost | ? {$_.IPEnabled} foreach($Network in $Networks) { $Networkip = $Network.IpAddress[0] } 

Adding Relays and Basic Authentication for SMTP

 $ipblock= @(24,0,0,128, 32,0,0,128, 60,0,0,128, 68,0,0,128, 1,0,0,0, 76,0,0,0, 0,0,0,0, 0,0,0,0, 1,0,0,0, 0,0,0,0, 2,0,0,0, 1,0,0,0, 4,0,0,0, 0,0,0,0, 76,0,0,128, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 255,255,255,255) $ipList = @() $octet = @() $connectionips=$arg[0] $ipList = "127.0.0.1" $octet += $ipList.Split(".") $octet += $Networkip.Split(".") $ipblock[36] +=2 $ipblock[44] +=2; $smtpserversetting = get-wmiobject -namespace root\MicrosoftIISv2 -computername localhost -Query "Select * from IIsSmtpServerSetting" $ipblock += $octet $smtpserversetting.AuthBasic=1 $smtpserversetting.RelayIpList = $ipblock $smtpserversetting.put() 

Adding a connection for SMTP

 $connectionips="10.10.10.10" $checkArray =$connectionips.split(",") if($checkArray -notcontains $Networkip) { $connectionips += ","+$Networkip } $connectionipbuild=@ () $ipArray=$connectionips.split(",") foreach ($ip in $ipArray) { $connectionipbuild +=$ip+",255.255.255.255;" } $iisObject = new-object System.DirectoryServices.DirectoryEntry("IIS://localhost/SmtpSvc/1") $ipSec = $iisObject.Properties["IPSecurity"].Value # We need to pass values as one element object arrays [Object[]] $grantByDefault = @() $grantByDefault += , $false # <<< We're setting it to false $ipSec.GetType().InvokeMember("GrantByDefault", $bindingFlags, $null, $ipSec, $grantByDefault); $iisObject.Properties["IPSecurity"].Value = $ipSec $iisObject.CommitChanges() $iisObject = new-object System.DirectoryServices.DirectoryEntry("IIS://localhost/SmtpSvc/1") $ipSec = $iisObject.Properties["IPSecurity"].Value $bindingFlags = [Reflection.BindingFlags] "Public, Instance, GetProperty" $isGrantByDefault = $ipSec.GetType().InvokeMember("GrantByDefault", $bindingFlags, $null, $ipSec, $null); # to set an iplist we need to get it first if($isGrantByDefault) { $ipList = $ipSec.GetType().InvokeMember("IPDeny", $bindingFlags, $null, $ipSec, $null); } else { $ipList = $ipSec.GetType().InvokeMember("IPGrant", $bindingFlags, $null, $ipSec, $null); } # Add a single computer to the list: $ipList = $ipList + $connectionipbuild # This is important, we need to pass an object array of one element containing our ipList array [Object[]] $ipArray = @() $ipArray += , $ipList # Now update $bindingFlags = [Reflection.BindingFlags] "Public, Instance, SetProperty" if($isGrantByDefault) { $ipList = $ipSec.GetType().InvokeMember("IPDeny", $bindingFlags, $null, $ipSec, $ipArray); } else { $ipList = $ipSec.GetType().InvokeMember("IPGrant", $bindingFlags, $null, $ipSec, $ipArray); } $iisObject.Properties["IPSecurity"].Value = $ipSec $iisObject.CommitChanges() 
+9
source

In addition to the PonVimal answer :

This solution uses the Get-NetworkAddress function of Indented.NetworkTools

If you want to specify a relay for a group of computers instead of a single address, you can use System.DirectoryServices as follows (thanks to this answer ):

 $IpRelayList = @("192.168.1.0, 255.255.0.0", "127.3.4.0, 255.255.255.192") #adding relays $iisObject = new-object System.DirectoryServices.DirectoryEntry("IIS://localhost/smtpsvc/1") $relays = $iisObject.Properties["RelayIpList"].Value $bindingFlags = [Reflection.BindingFlags] "Public, Instance, GetProperty" $ipList = $relays.GetType().InvokeMember("IPGrant", $bindingFlags, $null, $relays, $null); #if relay list is empty we are retrieving host subnets and adding to relay $Networkip =@ () if($IpRelayList.Count -eq 0) { $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName localhost | ? {$_.IPEnabled} foreach($Network in $Networks) { $line = Get-NetworkAddress $Network.IpAddress[0] $Network.IpSubnet[0] $line = $line + ", " + $Network.IpSubnet[0] $Networkip += $line } } $ipList = $Networkip + $IpRelayList # This is important, we need to pass an object array of one element containing our ipList array [Object[]] $ipArray = @() $ipArray += , $ipList # Now update $bindingFlags = [Reflection.BindingFlags] "Public, Instance, SetProperty" $ipList = $relays.GetType().InvokeMember("IPGrant", $bindingFlags, $null, $relays, $ipArray); $iisObject.Properties["RelayIpList"].Value = $relays $iisObject.CommitChanges() 
0
source

Source: https://habr.com/ru/post/986506/


All Articles