[英]How do I set the binding port number for a set of IIS websites with an increment value using PowerShell?
I have an IIS back end server with many websites. 我有一个带有许多网站的IIS后端服务器。 I just created these sites using powershell. 我只是使用powershell创建了这些站点。 I need to set the binding port for each website to a unique number (starting at 9200) for the proxy. 我需要将每个网站的绑定端口设置为代理的唯一编号(从9200开始)。 I am trying to find a way to script this out, but I am having a hard time trying to find a solution to this one. 我正在尝试找到一种方法来编写此脚本,但我很难找到一种解决方案。 Currently I have a list of websites in a text file I want to set the binding port for. 目前,我在要设置绑定端口的文本文件中有一个网站列表。 Right now this is what I have: 现在,这就是我所拥有的:
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
}
}
The reason I have the first foreach loop is to get the port numbers from 9200-9310 since I have 110 websites in total. 我有第一个foreach循环的原因是因为我总共有110个网站,所以从9200-9310获得端口号。 Am I going about this the wrong way? 我会以错误的方式处理吗?
It seems the issue is your two foreach
loops. 看来问题是您的两个foreach
循环。 You have your second foreach
loop inside of the one containing the port numbers. 您在包含端口号的第二个foreach
循环中。 Therefore, your first loop starts at $number = 9200
and then sets EACH $site
equal to the port held in your $number
variable at that time and then exits that loop and increments the port number and starts again. 因此,您的第一个循环从$number = 9200
,然后将每个$site
设置为当时在$number
变量中保存的端口,然后退出该循环并递增端口号,然后重新开始。
Here is something I came up with that I believe will solve your problem. 我想出了一些可以解决您问题的方法。
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.