Windows – Updating IIS SMTP Relay Restrictions with Powershell

iispowershellsmtpwindows

I'm trying to update the IIS 6 Virtual SMTP server relay restrictions to only allow 127.0.0.1. To do this I'm updating the following setting.

enter image description here

I can do this manually but I'd like to do it from PowerShell.

$settings = get-wmiobject -namespace root\MicrosoftIISv2 -computername localhost -Query "Select * from IIsSmtpServerSetting"
$settings.RelayIpList += @(127,0,0,1)
$settings.Put()

If I query the setting in powershell the value I've added is there, but it doesn't update in the UI. Am I using the correct setting? Or am I missing something else?

Best Answer

Hope it will help someone.

I found out that you can only do something like this to update the relayIPList, below is an example to add 127.0.0.1 to an empty relay ip list:

$SmtpConfig = Get-WMIObject -Namespace root/MicrosoftIISv2 -ComputerName localhost -Query "Select * From IisSmtpServerSetting"

$RelayIpList = @( 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, 1, 0, 0, 0, 2, 0, 0, 0, 2, 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, 127, 0, 0, 1 )

$SmtpConfig.RelayIPList = $RelayIPList

$SmtpConfig.Put()

*Note the spaces in the array. They need to be there to ensure it's a byte array (it will not work even when you use strong type to create a byte array without the spaces). Also do not try to modify the content of the array

So, following won't work:

[Byte[]]$RelayIpList = @(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,127,0,0,1)

either will this:

[Byte[]]$RelayIPList = @( 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, 1, 0, 0, 0, 2, 0, 0, 0, 2, 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, 127, 0, 0, 1 )
$IPs | ForEach-Object { $RelayIPList = $RelayIPList + ($_.split('.')) }