[英]Update Host Header in IIS with Powershell
Goal : Update an existing host header for an IIS7.5 site with powershell 目标 :使用PowerShell更新IIS7.5站点的现有主机头
Problem : Set-WebBinding
requires the name of the site which I don't have. 问题 : Set-WebBinding
需要我没有的站点名称。 I do have the HostHeader though. 我确实有HostHeader。
Scenario : I have multiple sites in IIS. 场景 :我在IIS中有多个站点。 Some of them have a host header with a particular string that I want to change. 其中一些具有一个主机头,其中包含我要更改的特定字符串。
Site1 - site1.stuff.domain.net
Site2 - site2.stuff.domain.net
Site3 - site3.domain.net
I want to change all sites that have .stuff
in their headers. 我想更改标题中带有.stuff
所有站点。
I'm using Get-WebBinding
to get a list of all sites and their bindings. 我正在使用Get-WebBinding
获取所有站点及其绑定的列表。 I then loop through them and check if bindingInformation
contains .stuff
. 然后,我遍历它们,并检查bindingInformation
包含.stuff
。 I modify the string how I please and then go to update the header with 我以自己喜欢的方式修改了字符串,然后使用
Set-WebBinding -HostHeader $originalHeader -SetProperty HostHeader -Value $newHeader
Apparently though, you have to have the site's name in order to use Set-WebBinding
, unlike Get-WebBinding
which allows you to get a binding based on the HostHeader ( Get-WebBinding -HostHeader $someValue
). 但是,显然,您必须具有站点名称才能使用Set-WebBinding
,这与Get-WebBinding
不同,后者允许您基于HostHeader Get-WebBinding -HostHeader $someValue
绑定( Get-WebBinding -HostHeader $someValue
)。 Is there a way to use Set-WebBinding
without specifing the Name
of a site? 有没有一种方法可以使用Set-WebBinding
而不指定站点Name
? Is there a way I can get the site's name from Get-WebBinding
? 有没有一种方法可以从Get-WebBinding
站点名称? Is there an alternative to Set-WebBinding
? 有替代Set-WebBinding
吗? Or is there a better way to do what I'm trying to do? 还是有更好的方法来做我想做的事情?
Give this a try: 试试看:
Get-WebBinding -HostHeader *.stuff.* | Foreach-Object{
#$NewHeader = $_.bindingInformation -replace '\.stuff\.','.staff.'
Set-WebConfigurationProperty -Filter $_.ItemXPath -PSPath IIS:\ -Name Bindings -Value @{protocol='http';bindingInformation=$NewHeader}
}
Modified Shay's answer to support multiple bindings. 修改了Shay的答案,以支持多个绑定。
Get-WebBinding -HostHeader *.stuff.* | Foreach-Object{
#$NewHeader = $_.bindingInformation -replace '\.stuff\.','.staff.'
Set-WebConfigurationProperty -Filter ($_.ItemXPath + "/bindings/binding[@protocol='http' and @bindingInformation='" + $_.bindingInformation + "']") -PSPath IIS:\ -Name bindingInformation -Value $NewHeader
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.