我正在尝试更新 IIS 6 虚拟 SMTP 服务器中继限制以仅允许 127.0.0.1。为此,我正在更新以下设置。
我可以手动完成此操作,但我想通过 PowerShell 完成。
$settings = get-wmiobject -namespace root\MicrosoftIISv2 -computername localhost -Query "Select * from IIsSmtpServerSetting"
$settings.RelayIpList += @(127,0,0,1)
$settings.Put()
如果我在 powershell 中查询设置,我添加的值就在那里,但它不会在 UI 中更新。我使用的设置正确吗?还是我遗漏了其他东西?
答案1
希望它能够对某人有所帮助。
我发现你只能做这样的事情来更新relayIPList,下面是将127.0.0.1添加到空的中继IP列表的示例:
$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()
*注意数组中的空格。它们必须存在以确保它是一个字节数组(即使使用强类型创建没有空格的字节数组也不会起作用)。另外,不要尝试修改数组的内容
因此,以下操作无效:
[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)
也可以这样做:
[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('.')) }
答案2
如果你需要从一台服务器迁移到另一台服务器,那就更简单了
#Copy the relay list from one server and migrate it to another
$COMP1 = Get-WMIObject -Namespace root/MicrosoftIISv2 -ComputerName server1 -Query "Select * From IisSmtpServerSetting"
$COMP2 = Get-WMIObject -Namespace root/MicrosoftIISv2 -ComputerName server2 -Query "Select * From IisSmtpServerSetting"
$COMP2.RelayIpList = $comp1.RelayIpList
$COMP2.Put()
答案3
这实际上是可能的,而且比人们想象的要复杂得多。这个神奇的中继 IP 列表对象有一些集合长度被硬编码到其中。
这是我弄清楚这个奇怪现象后使用的脚本的一部分。
param(
[Parameter(ValueFromRemainingArguments=$true)][object[]]$AllowedIPs
)
$SMTPServerWmi = Get-WmiObject IISSmtpServerSetting -namespace "ROOT\MicrosoftIISv2" | Where-Object { $_.name -like "SmtpSVC/1" }
$SMTPServerWmi.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,
$AllowedIPs.Count,0,0,0,
2,0,0,0,
($AllowedIPs.Count + 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) + $AllowedIPs.ForEach({ $_.Split(".")})
$SMTPServerWmi.Put()
如果这些值不正确,UI 可能会显示您的 IP 和大量随机垃圾、崩溃或损坏,以致您无法使用它通过 UI 从列表中删除项目。