Como posso alterar o endereço IP de todos os sites em IIS6 usando o PowerShell 1.0?
-
06-07-2019 - |
Pergunta
Usando PowerShell 1.0 no Windows Server 2003 com o IIS 6.
Eu tenho cerca de 200 sites que eu gostaria de mudar o endereço IP (conforme listado nas propriedades de site na guia "website", no "site identificação" seção de campo "endereço IP".
Eu encontrei este código:
$site = [adsi]"IIS://localhost/w3svc/$siteid"
$site.ServerBindings.Insert($site.ServerBindings.Count, ":80:$hostheader")
$site.SetInfo()
Como posso fazer algo como isso, mas:
- percorrer todos os locais em IIS
- Não insira um valor de cabeçalho de anfitrião, mas alterar um já existente.
Solução
O seguinte script PowerShell deve ajudar:
$oldIp = "172.16.3.214"
$newIp = "172.16.3.215"
# Get all objects at IIS://Localhost/W3SVC
$iisObjects = new-object `
System.DirectoryServices.DirectoryEntry("IIS://Localhost/W3SVC")
foreach($site in $iisObjects.psbase.Children)
{
# Is object a website?
if($site.psbase.SchemaClassName -eq "IIsWebServer")
{
$siteID = $site.psbase.Name
# Grab bindings and cast to array
$bindings = [array]$site.psbase.Properties["ServerBindings"].Value
$hasChanged = $false
$c = 0
foreach($binding in $bindings)
{
# Only change if IP address is one we're interested in
if($binding.IndexOf($oldIp) -gt -1)
{
$newBinding = $binding.Replace($oldIp, $newIp)
Write-Output "$siteID: $binding -> $newBinding"
$bindings[$c] = $newBinding
$hasChanged = $true
}
$c++
}
if($hasChanged)
{
# Only update if something changed
$site.psbase.Properties["ServerBindings"].Value = $bindings
# Comment out this line to simulate updates.
$site.psbase.CommitChanges()
Write-Output "Committed change for $siteID"
Write-Output "========================="
}
}
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow