gpt4 book ai didi

powershell - 使用 Powershell-(Relay,Connection) 在 Windows Server 中配置 Smtp 虚拟服务器

转载 作者:行者123 更新时间:2023-12-03 23:58:11 25 4
gpt4 key购买 nike

使用 powershell 我必须启用 SMTP 和

  • 添加中继IP
  • 添加连接 IP
  • 设置访问控制身份验证

  • 我找到了上述操作的以下代码
    $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

    上面的 powershell 代码成功执行,并在添加时列出。

    但是当我连接到 smtp 服务器时,抛出以下错误

    enter image description here

    我找到了解决上述问题的解决方案,要删除“C:\inetpub\mailroot”中的文件夹,我可以在其中启动默认的 Smtp 虚拟服务器,但在单击 smtp 虚拟服务器属性时再次遇到问题

    enter image description here

    最佳答案

    Loading Feature Installation Modules


    Import-Module ServerManager 

    Installing Features


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

    Adding Relay , connection IPs and Authentication Basic for SMTP


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

    Adding Relay and Authentication Basic 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 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()

    关于powershell - 使用 Powershell-(Relay,Connection) 在 Windows Server 中配置 Smtp 虚拟服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30031514/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com