繁体   English   中英

如何使用PowerShell使用增量值设置一组IIS网站的绑定端口号?

[英]How do I set the binding port number for a set of IIS websites with an increment value using PowerShell?

我有一个带有许多网站的IIS后端服务器。 我只是使用powershell创建了这些站点。 我需要将每个网站的绑定端口设置为代理的唯一编号(从9200开始)。 我正在尝试找到一种方法来编写此脚本,但我很难找到一种解决方案。 目前,我在要设置绑定端口的文本文件中有一个网站列表。 现在,这就是我所拥有的:

Import-Module WebAdministration

$endpoints = Get-Content C:\scripts\endpoints.txt

foreach ($number in 9200..9310)
{
    foreach ($site in $endpoints)
        {
    Set-WebBinding -Name '$site' -BindingInformation "*:80:" -PropertyName Port -Value $number
    }
  }

我有第一个foreach循环的原因是因为我总共有110个网站,所以从9200-9310获得端口号。 我会以错误的方式处理吗?

看来问题是您的两个foreach循环。 您在包含端口号的第二个foreach循环中。 因此,您的第一个循环从$number = 9200 ,然后将每个$site设置为当时在$number变量中保存的端口,然后退出该循环并递增端口号,然后重新开始。

我想出了一些可以解决您问题的方法。

Import-Module WebAdministration
$endpoints = Get-Content C:\scripts\endpoints.txt

$ports = New-Object System.Collections.ArrayList
[int] $port = 9200
[int] $counter = 0

while($port -le 9310) {
  $ports.Add($port)
  $port++
}

foreach ($site in $endpoints)
{
   Set-WebBinding -Name '$site' -BindingInformation "*:80:" -PropertyName Port -Value $ports[$counter]
   $counter++
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM