简体   繁体   English

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

[英]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.

相关问题 如何创建带有.jpg名称通配符值的循环脚本,该脚本使用基本url下载一组图像? - How do I create a looped script with a wildcard value for the .jpg name, that downloads a set of images using a base url? 如何打印与变量值对应的设定数量的字符? - How do I print a set amount of characters corresponding to the value of a variable? 如何检查一组文件中的行数是否不等于特定数目? (击) - How do I check if the number of lines in a set of files is not equal to a certain number? (Bash) 如何分别针对一组固定的字符循环从文件中读取的字符串,如果它们匹配,则在 powershell 中打印该字符串 - How do I loop through a string read from a file against a fixed set of characters individually and if they match print the string in powershell 如何遍历值对列表以将各自的值设置为等于新值列表? - How do I iterate over a list of value pairs to set the respective value equal to a list of new values? 如何使用cpp设置随机数生成器的语法? - How do you set up the syntax for a random number generator using cpp? 如何增加范围中的第一个数字? - how can I increment the first number in a range? 如何设置和迭代矩阵 - How do I set and iterate over a matrix 如何将此动画设置为循环播放? - How do I set this animation to loop? 如何将一组整数相乘? - how do I multiply integers in a set?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM