Pregunta

Uso de Powershell 1.0 en Windows Server 2003 con IIS 6.

Tengo alrededor de 200 sitios para los que me gustaría cambiar la dirección IP (como se indica en las propiedades del sitio web en la pestaña "sitio web" en la sección "Identificación del sitio web", campo de dirección IP.

Encontré este código:

$site = [adsi]"IIS://localhost/w3svc/$siteid"
$site.ServerBindings.Insert($site.ServerBindings.Count, ":80:$hostheader")
$site.SetInfo()

¿Cómo puedo hacer algo como esto pero:

  1. Recorre todos los sitios en IIS
  2. No inserte un valor de encabezado de host, pero cambie uno existente.
¿Fue útil?

Solución

El siguiente script de PowerShell debería ayudar:

$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 bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top